Skip to content

Commit

Permalink
Fix crash when hovering over theorem labels
Browse files Browse the repository at this point in the history
  • Loading branch information
pfoerster committed Apr 11, 2020
1 parent bade5b8 commit bf97694
Show file tree
Hide file tree
Showing 3 changed files with 95 additions and 8 deletions.
16 changes: 8 additions & 8 deletions src/outline.rs
Original file line number Diff line number Diff line change
Expand Up @@ -279,16 +279,16 @@ impl OutlineContext {

fn find_theorem(
view: &DocumentView,
table: &latex::SymbolTable,
main_table: &latex::SymbolTable,
label: latex::Label,
) -> Option<Self> {
let label_range = table.tree.range(label.parent);
let env = table
let label_range = main_table.tree.range(label.parent);
let env = main_table
.environments
.iter()
.find(|env| env.range(&table.tree).contains(label_range.start))?;
.find(|env| env.range(&main_table.tree).contains(label_range.start))?;

let env_name = env.left.name(&table.tree).map(latex::Token::text)?;
let env_name = env.left.name(&main_table.tree).map(latex::Token::text)?;

for doc in &view.related {
if let DocumentContent::Latex(table) = &doc.content {
Expand All @@ -303,15 +303,15 @@ impl OutlineContext {
)
.unwrap_or_else(|| titlecase(&env_name));

let description = table.tree.print_group_content(
let description = main_table.tree.print_group_content(
env.left.parent,
latex::GroupKind::Options,
0,
);

return Some(Self {
range: env.range(&table.tree),
number: Self::find_number(view, table, label),
range: env.range(&main_table.tree),
number: Self::find_number(view, main_table, label),
item: Theorem { kind, description },
});
}
Expand Down
86 changes: 86 additions & 0 deletions tests/integration/hover.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,86 @@
use indoc::indoc;
use texlab::{
protocol::{HoverContents, MarkupContent, MarkupKind},
test::{TestBedBuilder, PULL_CAPABILITIES},
};

#[tokio::test]
async fn label_theorem_child_file() {
let mut test_bed = TestBedBuilder::new()
.file(
"main.tex",
indoc!(
r#"
\documentclass{article}
\newtheorem{lemma}{Lemma}
\include{child}
\ref{thm:foo}
"#
),
)
.file(
"child.tex",
indoc!(
r#"
\begin{lemma}\label{thm:foo}
1 + 1 = 2
\end{lemma}
"#
),
)
.build()
.await;
test_bed.spawn();
test_bed.initialize(PULL_CAPABILITIES.clone()).await;
test_bed.open("main.tex").await;

let actual_hover = test_bed.hover("main.tex", 3, 8).await.unwrap().unwrap();
assert_eq!(
actual_hover.contents,
HoverContents::Markup(MarkupContent {
kind: MarkupKind::PlainText,
value: "Lemma".into()
})
);
}

#[tokio::test]
async fn label_theorem_child_file_number() {
let mut test_bed = TestBedBuilder::new()
.file(
"main.tex",
indoc!(
r#"
\documentclass{article}
\newtheorem{lemma}{Lemma}
\include{child}
\ref{thm:foo}
"#
),
)
.file(
"child.tex",
indoc!(
r#"
\begin{lemma}[Foo]\label{thm:foo}
1 + 1 = 2
\end{lemma}
"#
),
)
.file("child.aux", r#"\newlabel{thm:foo}{{1}{1}{Foo}{lemma.1}{}}"#)
.build()
.await;
test_bed.spawn();
test_bed.initialize(PULL_CAPABILITIES.clone()).await;
test_bed.open("main.tex").await;

let actual_hover = test_bed.hover("main.tex", 3, 8).await.unwrap().unwrap();
assert_eq!(
actual_hover.contents,
HoverContents::Markup(MarkupContent {
kind: MarkupKind::PlainText,
value: "Lemma 1 (Foo)".into()
})
);
}
1 change: 1 addition & 0 deletions tests/integration/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ mod definition;
mod document_symbol;
mod folding;
mod highlight;
mod hover;
mod issues;
mod link;
mod prepare_rename;
Expand Down

0 comments on commit bf97694

Please sign in to comment.