summaryrefslogtreecommitdiff
path: root/support/texlab/crates/completion/src/providers/command.rs
blob: f404d42ab39363846788ae18edab9e84dd6e0cc9 (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
use base_db::{semantics::Span, DocumentData};
use rowan::{TextRange, TextSize};
use syntax::{bibtex, latex};

use crate::{
    util::{included_packages, CompletionBuilder, ProviderContext},
    CommandData, CompletionItem, CompletionItemData, CompletionParams,
};

static DELIMITERS: &[(&str, &str)] = &[("(", ")"), ("[", "]"), ("{", "\\}")];

pub fn complete_commands<'a>(
    params: &'a CompletionParams<'a>,
    builder: &mut CompletionBuilder<'a>,
) -> Option<()> {
    let cursor = find_command_name(params)?;

    let mut proc = Processor(ProviderContext {
        builder,
        params,
        cursor,
    });

    proc.add_begin_snippet();
    proc.add_delimiters();
    proc.add_library();
    proc.add_user();
    Some(())
}

struct Processor<'a, 'b>(ProviderContext<'a, 'b>);

impl<'a, 'b> Processor<'a, 'b> {
    pub fn add_begin_snippet(&mut self) -> Option<()> {
        let score = self.0.builder.matcher.score("begin", &self.0.cursor.text)?;
        let data = CompletionItemData::BeginEnvironment;
        self.0
            .builder
            .items
            .push(CompletionItem::new_simple(score, self.0.cursor.range, data));

        Some(())
    }

    pub fn add_delimiters(&mut self) {
        for (left, right) in DELIMITERS {
            let Some(score) = self.0.builder.matcher.score(&left, &self.0.cursor.text) else {
                continue;
            };

            let data = CompletionItemData::CommandLikeDelimiter(left, right);
            self.0
                .builder
                .items
                .push(CompletionItem::new_simple(score, self.0.cursor.range, data));
        }
    }

    pub fn add_library(&mut self) -> Option<()> {
        for package in included_packages(&self.0.params.feature) {
            let commands_with_score = package.commands.iter().filter_map(|command| {
                let matcher = &self.0.builder.matcher;
                let score = matcher.score(&command.name, &self.0.cursor.text)?;
                Some((command, score))
            });

            for (command, score) in commands_with_score {
                let data = CompletionItemData::Command(CommandData {
                    name: &command.name,
                    glyph: command.glyph.as_deref(),
                    image: command.image.as_deref(),
                    package: Some(package),
                });

                self.0.builder.items.push(CompletionItem::new_simple(
                    score,
                    self.0.cursor.range,
                    data,
                ));
            }
        }

        Some(())
    }

    fn add_user(&mut self) {
        let documents = self.0.params.feature.project.documents.iter();
        for data in documents.filter_map(|document| document.data.as_tex()) {
            let commands = data
                .semantics
                .commands
                .iter()
                .filter(|name| name.range != self.0.cursor.range);

            let commands_with_score = commands.filter_map(|command| {
                let matcher = &self.0.builder.matcher;
                let score = matcher.score(&command.text, &self.0.cursor.text)?;
                Some((command, score))
            });

            for (command, score) in commands_with_score {
                let data = CompletionItemData::Command(CommandData {
                    name: &command.text,
                    glyph: None,
                    image: None,
                    package: None,
                });

                self.0.builder.items.push(CompletionItem::new_simple(
                    score,
                    self.0.cursor.range,
                    data,
                ));
            }
        }
    }
}

fn find_command_name(params: &CompletionParams) -> Option<Span> {
    let offset = params.offset;
    match &params.feature.document.data {
        DocumentData::Tex(data) => {
            let root = data.root_node();
            find_command_name_ast(&root, latex::COMMAND_NAME, offset)
        }
        DocumentData::Bib(data) => {
            let root = data.root_node();
            find_command_name_ast(&root, bibtex::COMMAND_NAME, offset)
                .or_else(|| find_command_name_ast(&root, bibtex::ACCENT_NAME, offset))
        }
        _ => None,
    }
}

fn find_command_name_ast<L: rowan::Language>(
    root: &rowan::SyntaxNode<L>,
    kind: L::Kind,
    offset: TextSize,
) -> Option<Span> {
    let token = root
        .token_at_offset(offset)
        .filter(|token| token.text_range().start() != offset)
        .find(|token| token.kind() == kind)?;

    let full_range = token.text_range();
    let text = String::from(&token.text()[1..]);
    Some(Span::new(
        text,
        TextRange::new(full_range.start() + TextSize::of('\\'), full_range.end()),
    ))
}