summaryrefslogtreecommitdiff
path: root/support/texlab/src/completion/latex/user.rs
blob: 890a75882ba2309f18a81cb6eda2c004a724120f (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
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
use super::combinators;
use crate::completion::factory::{self, LatexComponentId};
use crate::syntax::*;
use crate::workspace::*;
use futures_boxed::boxed;
use itertools::Itertools;
use lsp_types::*;

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

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

    #[boxed]
    async fn execute<'a>(&'a self, request: &'a FeatureRequest<Self::Params>) -> Self::Output {
        combinators::command(request, |current_command| {
            async move {
                let mut items = Vec::new();
                for document in request.related_documents() {
                    if let SyntaxTree::Latex(tree) = &document.tree {
                        tree.commands
                            .iter()
                            .filter(|command| command.range() != current_command.range())
                            .map(|command| &command.name.text()[1..])
                            .unique()
                            .map(|command| {
                                let text_edit = TextEdit::new(
                                    current_command.short_name_range(),
                                    command.to_owned(),
                                );
                                factory::command(
                                    request,
                                    command.to_owned(),
                                    None,
                                    None,
                                    text_edit,
                                    &LatexComponentId::User,
                                )
                            })
                            .for_each(|item| items.push(item));
                    }
                }
                items
            }
        })
        .await
    }
}

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

impl FeatureProvider for LatexUserEnvironmentCompletionProvider {
    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 environment in &tree.env.environments {
                            if environment.left.command == context.command
                                || environment.right.command == context.command
                            {
                                continue;
                            }

                            if let Some(item) =
                                Self::make_item(request, &environment.left, context.range)
                            {
                                items.push(item);
                            }

                            if let Some(item) =
                                Self::make_item(request, &environment.right, context.range)
                            {
                                items.push(item);
                            }
                        }
                    }
                }
                items
            }
        })
        .await
    }
}

impl LatexUserEnvironmentCompletionProvider {
    fn make_item(
        request: &FeatureRequest<CompletionParams>,
        delimiter: &LatexEnvironmentDelimiter,
        name_range: Range,
    ) -> Option<CompletionItem> {
        if let Some(name) = delimiter.name() {
            let text = name.text().to_owned();
            let text_edit = TextEdit::new(name_range, text.clone());
            let item = factory::environment(request, text, text_edit, &LatexComponentId::User);
            return Some(item);
        }
        None
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    use lsp_types::Position;

    #[test]
    fn test_command() {
        let items = test_feature(
            LatexUserCommandCompletionProvider,
            FeatureSpec {
                files: vec![
                    FeatureSpec::file("foo.tex", "\\include{bar.tex}\n\\foo"),
                    FeatureSpec::file("bar.tex", "\\bar"),
                    FeatureSpec::file("baz.tex", "\\baz"),
                ],
                main_file: "foo.tex",
                position: Position::new(1, 2),
                ..FeatureSpec::default()
            },
        );
        let labels: Vec<&str> = items.iter().map(|item| item.label.as_ref()).collect();
        assert_eq!(labels, vec!["include", "bar"]);
    }

    #[test]
    fn test_environment() {
        let items = test_feature(
            LatexUserEnvironmentCompletionProvider,
            FeatureSpec {
                files: vec![
                    FeatureSpec::file("foo.tex", "\\include{bar.tex}\n\\begin{foo}"),
                    FeatureSpec::file("bar.tex", "\\begin{bar}\\end{bar}"),
                    FeatureSpec::file("baz.tex", "\\begin{baz}\\end{baz}"),
                ],
                main_file: "foo.tex",
                position: Position::new(1, 9),
                ..FeatureSpec::default()
            },
        );
        let labels: Vec<&str> = items
            .iter()
            .map(|item| item.label.as_ref())
            .unique()
            .collect();
        assert_eq!(labels, vec!["bar"]);
    }
}