summaryrefslogtreecommitdiff
path: root/support/texlab/crates/completion/src/lib.rs
blob: 6acec28d4996d71e737df8824daeb53d54761ea2 (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
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
mod providers;
mod util;

use base_db::{
    data::{BibtexEntryType, BibtexFieldType},
    semantics::bib,
    util::RenderedObject,
    Document, FeatureParams,
};
use rowan::{TextRange, TextSize};
use util::CompletionBuilder;

pub const LIMIT: usize = 50;

#[derive(Debug)]
pub struct CompletionParams<'a> {
    pub feature: FeatureParams<'a>,
    pub offset: TextSize,
}

#[derive(Debug, Default)]
pub struct CompletionResult<'a> {
    pub items: Vec<CompletionItem<'a>>,
}

#[derive(Debug, PartialEq, Eq)]
pub struct CompletionItem<'a> {
    pub score: i32,
    pub range: TextRange,
    pub preselect: bool,
    pub data: CompletionItemData<'a>,
}

impl<'a> CompletionItem<'a> {
    pub fn new_simple(score: i32, range: TextRange, data: CompletionItemData<'a>) -> Self {
        Self {
            score,
            range,
            preselect: false,
            data,
        }
    }
}

#[derive(Debug, PartialEq, Eq)]
pub enum CompletionItemData<'a> {
    Command(CommandData<'a>),
    BeginEnvironment,
    Citation(CitationData<'a>),
    Environment(EnvironmentData<'a>),
    GlossaryEntry(GlossaryEntryData),
    Label(LabelData<'a>),
    Color(&'a str),
    ColorModel(&'a str),
    File(String),
    Directory(String),
    Argument(ArgumentData<'a>),
    Package(&'a str),
    DocumentClass(&'a str),
    EntryType(EntryTypeData<'a>),
    Field(FieldTypeData<'a>),
    TikzLibrary(&'a str),
}

impl<'a> CompletionItemData<'a> {
    pub fn label<'b: 'a>(&'b self) -> &'a str {
        match self {
            Self::Command(data) => data.name,
            Self::BeginEnvironment => "begin",
            Self::Citation(data) => &data.entry.name.text,
            Self::Environment(data) => data.name,
            Self::GlossaryEntry(data) => &data.name,
            Self::Label(data) => data.name,
            Self::Color(name) => name,
            Self::ColorModel(name) => name,
            Self::File(name) => &name,
            Self::Directory(name) => &name,
            Self::Argument(data) => &data.0.name,
            Self::Package(name) => name,
            Self::DocumentClass(name) => name,
            Self::EntryType(data) => data.0.name,
            Self::Field(data) => data.0.name,
            Self::TikzLibrary(name) => name,
        }
    }
}

#[derive(PartialEq, Eq)]
pub struct CommandData<'a> {
    pub name: &'a str,
    pub glyph: Option<&'a str>,
    pub image: Option<&'a str>,
    pub package: Option<&'a completion_data::Package<'a>>,
}

impl<'a> std::fmt::Debug for CommandData<'a> {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        f.debug_struct("CommandData")
            .field("name", &self.name)
            .field("package", self.package.map_or(&"<user>", |p| &p.file_names))
            .finish()
    }
}

#[derive(PartialEq, Eq)]
pub struct EnvironmentData<'a> {
    pub name: &'a str,
    pub package: Option<&'a completion_data::Package<'a>>,
}

impl<'a> std::fmt::Debug for EnvironmentData<'a> {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        f.debug_struct("EnvironmentData")
            .field("name", &self.name)
            .field("package", self.package.map_or(&"<user>", |p| &p.file_names))
            .finish()
    }
}

#[derive(PartialEq, Eq)]
pub struct ArgumentData<'a>(pub &'a completion_data::Argument<'a>);

impl<'a> std::fmt::Debug for ArgumentData<'a> {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        f.debug_tuple("ArgumentData").field(&self.0.name).finish()
    }
}

#[derive(Debug, PartialEq, Eq)]
pub struct CitationData<'a> {
    pub document: &'a Document,
    pub entry: &'a bib::Entry,
}

#[derive(Debug, PartialEq, Eq)]
pub struct GlossaryEntryData {
    pub name: String,
}

#[derive(Debug, PartialEq, Eq)]
pub struct LabelData<'a> {
    pub name: &'a str,
    pub header: Option<String>,
    pub footer: Option<&'a str>,
    pub object: Option<RenderedObject<'a>>,
    pub keywords: String,
}

#[derive(PartialEq, Eq)]
pub struct EntryTypeData<'a>(pub BibtexEntryType<'a>);

impl<'a> std::fmt::Debug for EntryTypeData<'a> {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        f.debug_tuple("EntryTypeData").field(&self.0.name).finish()
    }
}

#[derive(PartialEq, Eq)]
pub struct FieldTypeData<'a>(pub BibtexFieldType<'a>);

impl<'a> std::fmt::Debug for FieldTypeData<'a> {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        f.debug_tuple("FieldTypeData").field(&self.0.name).finish()
    }
}

pub fn complete<'a>(params: &'a CompletionParams<'a>) -> CompletionResult<'a> {
    let mut builder = CompletionBuilder::from(params.feature.workspace);
    providers::complete_commands(params, &mut builder);
    providers::complete_environments(params, &mut builder);
    providers::complete_citations(params, &mut builder);
    providers::complete_acronyms(params, &mut builder);
    providers::complete_glossaries(params, &mut builder);
    providers::complete_labels(params, &mut builder);
    providers::complete_colors(params, &mut builder);
    providers::complete_color_models(params, &mut builder);
    providers::complete_includes(params, &mut builder);
    providers::complete_arguments(params, &mut builder);
    providers::complete_imports(params, &mut builder);
    providers::complete_entry_types(params, &mut builder);
    providers::complete_fields(params, &mut builder);
    providers::complete_tikz_libraries(params, &mut builder);
    builder.finish()
}

#[cfg(test)]
mod tests;