summaryrefslogtreecommitdiff
path: root/support/texlab/src/completion/latex/glossary.rs
blob: ca6600b6904da41cebdef4e8347f3f8d0fd3be16 (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
use super::combinators::{self, Parameter};
use crate::completion::factory;
use crate::syntax::LatexGlossaryEntryKind::*;
use crate::syntax::*;
use crate::workspace::*;
use futures_boxed::boxed;
use lsp_types::{CompletionItem, CompletionParams, TextEdit};

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

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

    #[boxed]
    async fn execute<'a>(&'a self, request: &'a FeatureRequest<Self::Params>) -> Self::Output {
        let parameters = LANGUAGE_DATA
            .glossary_entry_reference_commands
            .iter()
            .map(|cmd| Parameter::new(&cmd.name, cmd.index));

        combinators::argument(request, parameters, |context| {
            async move {
                let cmd_kind = LANGUAGE_DATA
                    .glossary_entry_reference_commands
                    .iter()
                    .find(|cmd| cmd.name == context.parameter.name)
                    .unwrap()
                    .kind;

                let mut items = Vec::new();
                for document in request.related_documents() {
                    if let SyntaxTree::Latex(tree) = &document.tree {
                        for entry in &tree.glossary.entries {
                            match (cmd_kind, entry.kind) {
                                (Acronym, Acronym) | (General, General) | (General, Acronym) => {
                                    let label = entry.label().text().to_owned();
                                    let text_edit = TextEdit::new(context.range, label.clone());
                                    let item = factory::glossary_entry(request, label, text_edit);
                                    items.push(item);
                                }
                                (Acronym, General) => {}
                            }
                        }
                    }
                }
                items
            }
        })
        .await
    }
}