forked from SpaceManiac/SpacemanDMM
-
Notifications
You must be signed in to change notification settings - Fork 0
/
completion.rs
438 lines (405 loc) · 14.8 KB
/
completion.rs
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
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
//! Supporting functions for completion and go-to-definition.
use std::collections::HashSet;
use langserver::*;
use dm::ast::PathOp;
use dm::annotation::Annotation;
use dm::objtree::{TypeRef, TypeVar, TypeProc, ProcValue};
use {Engine, Span, is_constructor_name, ignore_root};
use symbol_search::contains;
pub fn item_var(ty: TypeRef, name: &str, var: &TypeVar) -> CompletionItem {
let mut detail = ty.pretty_path().to_owned();
if let Some(ref decl) = var.declaration {
if decl.var_type.is_const {
if let Some(ref constant) = var.value.constant {
if ty.is_root() {
detail = constant.to_string();
} else {
detail = format!("{} - {}", constant, detail);
}
}
}
}
CompletionItem {
label: name.to_owned(),
kind: Some(CompletionItemKind::Field),
detail: Some(detail),
documentation: item_documentation(&var.value.docs),
.. Default::default()
}
}
pub fn item_proc(ty: TypeRef, name: &str, proc: &TypeProc) -> CompletionItem {
CompletionItem {
label: name.to_owned(),
kind: Some(if ty.is_root() {
CompletionItemKind::Function
} else if is_constructor_name(name) {
CompletionItemKind::Constructor
} else {
CompletionItemKind::Method
}),
detail: Some(ty.pretty_path().to_owned()),
documentation: item_documentation(&proc.main_value().docs),
..Default::default()
}
}
pub fn item_documentation(docs: &dm::docs::DocCollection) -> Option<Documentation> {
if docs.is_empty() {
return None;
}
Some(Documentation::MarkupContent(MarkupContent {
kind: MarkupKind::Markdown,
value: docs.text(),
}))
}
pub fn items_ty<'a>(
results: &mut Vec<CompletionItem>,
skip: &mut HashSet<(&str, &'a String)>,
ty: TypeRef<'a>,
query: &str,
) {
// type variables
for (name, var) in ty.get().vars.iter() {
if !skip.insert(("var", name)) {
continue;
}
if contains(name, query) {
results.push(item_var(ty, name, var));
}
}
// procs
for (name, proc) in ty.get().procs.iter() {
if !skip.insert(("proc", name)) {
continue;
}
if contains(name, query) {
results.push(CompletionItem {
insert_text: Some(name.to_owned()),
.. item_proc(ty, name, proc)
});
}
}
}
pub fn combine_tree_path<'a, I>(iter: &I, mut absolute: bool, mut parts: &'a [String]) -> impl Iterator<Item=&'a str>
where I: Iterator<Item=(Span, &'a Annotation)> + Clone
{
// cut off the part of the path we haven't selected
if_annotation! { Annotation::InSequence(idx) in iter; {
parts = &parts[..::std::cmp::min(idx+1, parts.len())];
}}
// if we're on the right side of a 'var/', start the lookup there
if let Some(i) = parts.iter().position(|x| x == "var") {
parts = &parts[i + 1..];
absolute = true;
}
// if we're on the right side of a 'list/', start the lookup there
while let Some((first, rest)) = parts.split_first() {
if first == "list" {
parts = rest;
} else {
break;
}
}
let mut prefix_parts = &[][..];
if !absolute {
if_annotation! { Annotation::TreeBlock(parts) in iter; {
prefix_parts = parts;
if let Some(i) = prefix_parts.iter().position(|x| x == "var") {
// if we're inside a 'var' block, start the lookup there
prefix_parts = &prefix_parts[i+1..];
}
}}
}
prefix_parts.iter().chain(parts).map(|x| &**x)
}
impl<'a> Engine<'a> {
pub fn follow_type_path<'b, I>(&'b self, iter: &I, mut parts: &'b [(PathOp, String)]) -> Option<TypePathResult<'b>>
where
I: Iterator<Item = (Span, &'a Annotation)> + Clone,
{
// cut off the part of the path we haven't selected
if_annotation! { Annotation::InSequence(idx) in iter; {
parts = &parts[..::std::cmp::min(idx+1, parts.len())];
}}
// if we're on the right side of a 'list/', start the lookup there
match parts.split_first() {
Some(((PathOp::Slash, kwd), rest)) if kwd == "list" && !rest.is_empty() => parts = rest,
_ => {}
}
// use the first path op to select the starting type of the lookup
if parts.is_empty() {
return Some(TypePathResult {
ty: self.objtree.root(),
decl: None,
proc: None,
});
}
let mut ty = match parts[0].0 {
PathOp::Colon => return None, // never finds anything, apparently?
PathOp::Slash => self.objtree.root(),
PathOp::Dot => match self.find_type_context(iter) {
(Some(base), _) => base,
(None, _) => self.objtree.root(),
},
};
// follow the path ops until we hit 'proc' or 'verb'
let mut iter = parts.iter();
let mut decl = None;
while let Some(&(op, ref name)) = iter.next() {
if name == "proc" {
decl = Some("proc");
break;
} else if name == "verb" {
decl = Some("verb");
break;
}
if let Some(next) = ty.navigate(op, name) {
ty = next;
} else {
break;
}
}
let mut proc = None;
if decl.is_some() {
if let Some((_, proc_name)) = iter.next() {
// '/datum/proc/proc_name'
if let Some(proc_ref) = ty.get_proc(proc_name) {
proc = Some((proc_name.as_str(), proc_ref.get()));
}
}
// else '/datum/proc', no results
}
// '/datum'
Some(TypePathResult { ty, decl, proc })
}
pub fn tree_completions(&self, results: &mut Vec<CompletionItem>, exact: bool, ty: TypeRef, query: &str) {
// path keywords
for &name in ["proc", "var", "verb"].iter() {
if contains(name, query) {
results.push(CompletionItem {
label: name.to_owned(),
kind: Some(CompletionItemKind::Keyword),
.. Default::default()
})
}
}
if exact {
// child types
for child in ty.children() {
if contains(&child.name, query) {
results.push(CompletionItem {
label: child.name.to_owned(),
kind: Some(CompletionItemKind::Class),
documentation: item_documentation(&child.docs),
.. Default::default()
});
}
}
}
let mut next = Some(ty);
let mut skip = HashSet::new();
while let Some(ty) = ignore_root(next) {
// override a parent's var
for (name, var) in ty.get().vars.iter() {
if !skip.insert(("var", name)) {
continue;
}
if contains(name, query) {
results.push(CompletionItem {
insert_text: Some(format!("{} = ", name)),
.. item_var(ty, name, var)
});
}
}
// override a parent's proc
for (name, proc) in ty.get().procs.iter() {
if !skip.insert(("proc", name)) {
continue;
}
if contains(name, query) {
use std::fmt::Write;
let mut completion = format!("{}(", name);
let mut sep = "";
for param in proc.main_value().parameters.iter() {
for each in param.var_type.type_path.iter() {
let _ = write!(completion, "{}{}", sep, each);
sep = "/";
}
let _ = write!(completion, "{}{}", sep, param.name);
sep = ", ";
}
let _ = write!(completion, ")\n\t. = ..()\n\t");
results.push(CompletionItem {
insert_text: Some(completion),
.. item_proc(ty, name, proc)
});
}
}
next = ty.parent_type();
}
}
pub fn path_completions<'b, I>(
&'b self,
results: &mut Vec<CompletionItem>,
iter: &I,
parts: &'b [(PathOp, String)],
_last_op: PathOp,
query: &str,
) where
I: Iterator<Item = (Span, &'b Annotation)> + Clone,
{
// TODO: take last_op into account
match self.follow_type_path(iter, parts) {
// '/datum/<complete types>'
Some(TypePathResult {
ty,
decl: None,
proc: None,
}) => {
// path keywords
for &name in ["proc", "verb"].iter() {
if contains(name, query) {
results.push(CompletionItem {
label: name.to_owned(),
kind: Some(CompletionItemKind::Keyword),
.. Default::default()
})
}
}
// child types
for child in ty.children() {
if contains(&child.name, query) {
results.push(CompletionItem {
label: child.name.to_owned(),
kind: Some(CompletionItemKind::Class),
documentation: item_documentation(&child.docs),
.. Default::default()
});
}
}
},
// '/datum/proc/<complete procs>'
// TODO: take the path op into acocunt (`/proc` vs `.proc`)
Some(TypePathResult {
ty,
decl: Some(decl),
proc: None,
}) => {
let mut next = Some(ty);
let mut skip = HashSet::new();
while let Some(ty) = next {
// reference a declared proc
for (name, proc) in ty.get().procs.iter() {
if !skip.insert(("proc", name)) {
continue;
}
// declarations only
let proc_decl = match proc.declaration.as_ref() {
Some(decl) => decl,
None => continue,
};
if proc_decl.kind.is_verb() != (decl == "verb") {
continue;
}
if contains(name, query) {
results.push(item_proc(ty, name, proc));
}
}
next = ignore_root(ty.parent_type());
}
},
_ => {}
}
}
pub fn unscoped_completions<'b, I>(&'b self, results: &mut Vec<CompletionItem>, iter: &I, query: &str)
where
I: Iterator<Item = (Span, &'b Annotation)> + Clone,
{
let (ty, proc_name) = self.find_type_context(iter);
// implicit proc vars
if proc_name.is_some() {
for &name in ["args", "global", "src", "usr"].iter() {
if contains(name, query) {
results.push(CompletionItem {
label: name.to_owned(),
kind: Some(CompletionItemKind::Keyword),
.. Default::default()
});
}
}
}
// local variables
for (_, annotation) in iter.clone() {
if let Annotation::LocalVarScope(_var_type, name) = annotation {
if contains(name, query) {
results.push(CompletionItem {
label: name.clone(),
kind: Some(CompletionItemKind::Variable),
detail: Some("(local)".to_owned()),
.. Default::default()
});
}
}
}
// proc parameters
let ty = ty.unwrap_or(self.objtree.root());
if let Some((proc_name, idx)) = proc_name {
if let Some(proc) = ty.get().procs.get(proc_name) {
if let Some(value) = proc.value.get(idx) {
for param in value.parameters.iter() {
if contains(¶m.name, query) {
results.push(CompletionItem {
label: param.name.clone(),
kind: Some(CompletionItemKind::Variable),
detail: Some("(parameter)".to_owned()),
.. Default::default()
});
}
}
}
}
}
// macros
if let Some(ref preprocessor) = self.preprocessor {
// TODO: verify that the macro is in scope at the location
for (_, &(ref name, ref define)) in preprocessor.history().iter() {
if contains(name, query) {
results.push(CompletionItem {
label: name.to_owned(),
kind: Some(CompletionItemKind::Constant),
detail: Some(format!("{}", define)),
documentation: item_documentation(define.docs()),
.. Default::default()
});
}
}
}
// fields
let mut next = Some(ty);
let mut skip = HashSet::new();
while let Some(ty) = next {
items_ty(results, &mut skip, ty, query);
next = ty.parent_type();
}
}
pub fn scoped_completions<'b, I>(
&'b self,
results: &mut Vec<CompletionItem>,
iter: &I,
priors: &[String],
query: &str,
) where
I: Iterator<Item = (Span, &'b Annotation)> + Clone,
{
let mut next = self.find_scoped_type(iter, priors);
let mut skip = HashSet::new();
while let Some(ty) = next {
items_ty(results, &mut skip, ty, query);
next = ignore_root(ty.parent_type());
}
}
}
pub struct TypePathResult<'a> {
pub ty: TypeRef<'a>,
pub decl: Option<&'static str>,
pub proc: Option<(&'a str, &'a ProcValue)>,
}