Skip to content

Commit

Permalink
Include theorem environments in completion
Browse files Browse the repository at this point in the history
  • Loading branch information
pfoerster committed Jul 1, 2019
1 parent 48b1a81 commit da0b4c3
Show file tree
Hide file tree
Showing 4 changed files with 72 additions and 1 deletion.
2 changes: 1 addition & 1 deletion src/completion/factory.rs
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ impl<'a> LatexComponentId<'a> {
pub fn detail(&self) -> Cow<'static, str> {
match self {
LatexComponentId::Kernel => "built-in".into(),
LatexComponentId::User => "unknown".into(),
LatexComponentId::User => "user-defined".into(),
LatexComponentId::Component(files) => files.join(", ").into(),
}
}
Expand Down
1 change: 1 addition & 0 deletions src/completion/latex/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,5 +9,6 @@ pub mod include;
pub mod kernel;
pub mod label;
pub mod symbol;
pub mod theorem;
pub mod tikz;
pub mod user;
68 changes: 68 additions & 0 deletions src/completion/latex/theorem.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
use crate::completion::factory::{self, LatexComponentId};
use crate::completion::latex::combinators;
use crate::feature::{FeatureProvider, FeatureRequest};
use crate::syntax::SyntaxTree;
use futures_boxed::boxed;
use lsp_types::{CompletionItem, CompletionParams, TextEdit};

#[derive(Debug, PartialEq, Eq, Clone, Copy)]
pub struct LatexTheoremEnvironmentCompletionProvider;

impl FeatureProvider for LatexTheoremEnvironmentCompletionProvider {
type Params = CompletionParams;
type Output = Vec<CompletionItem>;

#[boxed]
async fn execute<'a>(&'a self, request: &'a FeatureRequest<Self::Params>) -> Self::Output {
combinators::environment(request, async move |_, name_range| {
let mut items = Vec::new();
for document in request.related_documents() {
if let SyntaxTree::Latex(tree) = &document.tree {
for theorem in &tree.theorem_definitions {
let name = theorem.name().text().to_owned();
let text_edit = TextEdit::new(name_range, name.clone().into());
let item = factory::environment(
request,
name.into(),
text_edit,
&LatexComponentId::User,
);
items.push(item);
}
}
}
items
})
.await
}
}

#[cfg(test)]
mod tests {
use super::*;
use crate::feature::{test_feature, FeatureSpec};
use lsp_types::{Position, Range};
use std::borrow::Cow;

#[test]
fn test() {
let items = test_feature(
LatexTheoremEnvironmentCompletionProvider,
FeatureSpec {
files: vec![FeatureSpec::file(
"foo.tex",
"\\newtheorem{theorem}{Theorem}\n\\begin{th}",
)],
main_file: "foo.tex",
position: Position::new(1, 8),
..FeatureSpec::default()
},
);
assert_eq!(items.len(), 1);
assert_eq!(items[0].label, Cow::from("theorem"));
assert_eq!(
items[0].text_edit.as_ref().map(|edit| edit.range),
Some(Range::new_simple(1, 7, 1, 9))
);
}
}
2 changes: 2 additions & 0 deletions src/completion/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ use self::latex::include::LatexIncludeCompletionProvider;
use self::latex::kernel::*;
use self::latex::label::LatexLabelCompletionProvider;
use self::latex::symbol::*;
use self::latex::theorem::LatexTheoremEnvironmentCompletionProvider;
use self::latex::tikz::*;
use self::latex::user::*;
use self::quality::OrderByQualityCompletionProvider;
Expand Down Expand Up @@ -45,6 +46,7 @@ impl CompletionProvider {
Box::new(LatexTikzLibraryCompletionProvider),
Box::new(LatexColorCompletionProvider),
Box::new(LatexColorModelCompletionProvider),
Box::new(LatexTheoremEnvironmentCompletionProvider),
Box::new(LatexComponentEnvironmentProvider),
Box::new(LatexKernelEnvironmentCompletionProvider),
Box::new(LatexLabelCompletionProvider),
Expand Down

0 comments on commit da0b4c3

Please sign in to comment.