-
Notifications
You must be signed in to change notification settings - Fork 260
/
Copy pathauto_import.rs
102 lines (88 loc) · 3.71 KB
/
auto_import.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
use lsp_types::{Position, Range, TextEdit};
use noirc_frontend::macros_api::ModuleDefId;
use crate::modules::{get_parent_module_id, module_full_path, module_id_path};
use super::{
kinds::{FunctionCompletionKind, FunctionKind, RequestedItems},
name_matches,
sort_text::auto_import_sort_text,
NodeFinder,
};
impl<'a> NodeFinder<'a> {
pub(super) fn complete_auto_imports(
&mut self,
prefix: &str,
requested_items: RequestedItems,
function_completion_kind: FunctionCompletionKind,
) {
let current_module_parent_id = get_parent_module_id(self.def_maps, self.module_id);
for (name, entries) in self.interner.get_auto_import_names() {
if !name_matches(name, prefix) {
continue;
}
for (module_def_id, visibility, defining_module) in entries {
if self.suggested_module_def_ids.contains(module_def_id) {
continue;
}
let Some(mut completion_item) = self.module_def_id_completion_item(
*module_def_id,
name.clone(),
function_completion_kind,
FunctionKind::Any,
requested_items,
) else {
continue;
};
let module_full_path = if let Some(defining_module) = defining_module {
module_id_path(
*defining_module,
&self.module_id,
current_module_parent_id,
self.interner,
)
} else {
let Some(module_full_path) = module_full_path(
*module_def_id,
*visibility,
self.module_id,
current_module_parent_id,
self.interner,
self.def_maps,
) else {
continue;
};
module_full_path
};
let full_path = if defining_module.is_some()
|| !matches!(module_def_id, ModuleDefId::ModuleId(..))
{
format!("{}::{}", module_full_path, name)
} else {
module_full_path
};
let mut label_details = completion_item.label_details.unwrap();
label_details.detail = Some(format!("(use {})", full_path));
completion_item.label_details = Some(label_details);
let line = self.auto_import_line as u32;
let character = (self.nesting * 4) as u32;
let indent = " ".repeat(self.nesting * 4);
let mut newlines = "\n";
// If the line we are inserting into is not an empty line, insert an extra line to make some room
if let Some(line_text) = self.lines.get(line as usize) {
if !line_text.trim().is_empty() {
newlines = "\n\n";
}
}
completion_item.additional_text_edits = Some(vec![TextEdit {
range: Range {
start: Position { line, character },
end: Position { line, character },
},
new_text: format!("use {};{}{}", full_path, newlines, indent),
}]);
completion_item.sort_text = Some(auto_import_sort_text());
self.completion_items.push(completion_item);
self.suggested_module_def_ids.insert(*module_def_id);
}
}
}
}