summaryrefslogtreecommitdiff
path: root/support/texlab/src/completion/latex/theorem.rs
blob: 993ef5c7e8a1b17cbda050ade37a6e8d6ca265de (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
use super::combinators;
use crate::completion::factory::{self, LatexComponentId};
use crate::syntax::*;
use crate::workspace::*;
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, |context| {
            async move {
                let mut items = Vec::new();
                for document in request.related_documents() {
                    if let SyntaxTree::Latex(tree) = &document.tree {
                        for theorem in &tree.math.theorem_definitions {
                            let name = theorem.name().text().to_owned();
                            let text_edit = TextEdit::new(context.range, name.clone());
                            let item = factory::environment(
                                request,
                                name,
                                text_edit,
                                &LatexComponentId::User,
                            );
                            items.push(item);
                        }
                    }
                }
                items
            }
        })
        .await
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    use crate::range::RangeExt;
    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))
        );
    }
}