-
Notifications
You must be signed in to change notification settings - Fork 463
/
completions.rs
367 lines (343 loc) · 13.6 KB
/
completions.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
use cairo_lang_defs::ids::{
FunctionWithBodyId, ImplItemId, LanguageElementId, LookupItemId, ModuleFileId, ModuleId,
ModuleItemId, NamedLanguageElementId, TopLevelLanguageElementId, TraitFunctionId,
};
use cairo_lang_filesystem::ids::FileId;
use cairo_lang_filesystem::span::TextOffset;
use cairo_lang_semantic::db::SemanticGroup;
use cairo_lang_semantic::diagnostic::{NotFoundItemType, SemanticDiagnostics};
use cairo_lang_semantic::expr::inference::infers::InferenceEmbeddings;
use cairo_lang_semantic::expr::inference::solver::SolutionSet;
use cairo_lang_semantic::expr::inference::InferenceId;
use cairo_lang_semantic::items::function_with_body::SemanticExprLookup;
use cairo_lang_semantic::items::structure::SemanticStructEx;
use cairo_lang_semantic::items::us::SemanticUseEx;
use cairo_lang_semantic::lookup_item::{HasResolverData, LookupItemEx};
use cairo_lang_semantic::lsp_helpers::TypeFilter;
use cairo_lang_semantic::resolve::{ResolvedConcreteItem, ResolvedGenericItem, Resolver};
use cairo_lang_semantic::types::peel_snapshots;
use cairo_lang_semantic::{ConcreteTypeId, Pattern, TypeLongId};
use cairo_lang_syntax::node::ast::PathSegment;
use cairo_lang_syntax::node::{ast, TypedStablePtr, TypedSyntaxNode};
use cairo_lang_utils::LookupIntern;
use tower_lsp::lsp_types::{CompletionItem, CompletionItemKind, Position, Range, TextEdit};
use tracing::debug;
use crate::lang::lsp::ToLsp;
use crate::lang::semantic::LsSemanticGroup;
#[tracing::instrument(level = "trace", skip_all)]
pub fn generic_completions(
db: &(dyn SemanticGroup + 'static),
module_file_id: ModuleFileId,
lookup_items: Vec<LookupItemId>,
) -> Vec<CompletionItem> {
let mut completions = vec![];
// Crates.
completions.extend(db.crate_configs().keys().map(|crate_id| CompletionItem {
label: crate_id.lookup_intern(db).name().into(),
kind: Some(CompletionItemKind::MODULE),
..CompletionItem::default()
}));
// Module completions.
completions.extend(db.module_items(module_file_id.0).unwrap_or_default().iter().map(|item| {
CompletionItem {
label: item.name(db.upcast()).to_string(),
kind: ResolvedGenericItem::from_module_item(db, *item)
.ok()
.map(resolved_generic_item_completion_kind),
..CompletionItem::default()
}
}));
// Local variables and params.
let Some(lookup_item_id) = lookup_items.into_iter().next() else {
return completions;
};
let function_id = match lookup_item_id {
LookupItemId::ModuleItem(ModuleItemId::FreeFunction(free_function_id)) => {
FunctionWithBodyId::Free(free_function_id)
}
LookupItemId::ImplItem(ImplItemId::Function(impl_function_id)) => {
FunctionWithBodyId::Impl(impl_function_id)
}
_ => {
return completions;
}
};
let Ok(signature) = db.function_with_body_signature(function_id) else {
return completions;
};
for param in &signature.params {
completions.push(CompletionItem {
label: param.name.clone().into(),
kind: Some(CompletionItemKind::VARIABLE),
..CompletionItem::default()
});
}
let Ok(body) = db.function_body(function_id) else {
return completions;
};
for (_id, pat) in &body.patterns {
if let Pattern::Variable(var) = pat {
completions.push(CompletionItem {
label: var.name.clone().into(),
kind: Some(CompletionItemKind::VARIABLE),
..CompletionItem::default()
});
}
}
completions
}
fn resolved_generic_item_completion_kind(item: ResolvedGenericItem) -> CompletionItemKind {
match item {
ResolvedGenericItem::Constant(_) => CompletionItemKind::CONSTANT,
ResolvedGenericItem::Module(_) => CompletionItemKind::MODULE,
ResolvedGenericItem::GenericFunction(_) | ResolvedGenericItem::TraitFunction(_) => {
CompletionItemKind::FUNCTION
}
ResolvedGenericItem::GenericType(_) | ResolvedGenericItem::GenericTypeAlias(_) => {
CompletionItemKind::CLASS
}
ResolvedGenericItem::Impl(_) | ResolvedGenericItem::GenericImplAlias(_) => {
CompletionItemKind::CLASS
}
ResolvedGenericItem::Variant(_) => CompletionItemKind::ENUM_MEMBER,
ResolvedGenericItem::Trait(_) => CompletionItemKind::INTERFACE,
ResolvedGenericItem::Variable(_) => CompletionItemKind::VARIABLE,
}
}
#[tracing::instrument(level = "trace", skip_all)]
pub fn colon_colon_completions(
db: &(dyn SemanticGroup + 'static),
module_file_id: ModuleFileId,
lookup_items: Vec<LookupItemId>,
segments: Vec<PathSegment>,
) -> Option<Vec<CompletionItem>> {
// Get a resolver in the current context.
let resolver_data = match lookup_items.into_iter().next() {
Some(item) => {
(*item.resolver_data(db).ok()?).clone_with_inference_id(db, InferenceId::NoContext)
}
None => Resolver::new(db, module_file_id, InferenceId::NoContext).data,
};
let mut resolver = Resolver::with_data(db, resolver_data);
let mut diagnostics = SemanticDiagnostics::default();
let item = resolver
.resolve_concrete_path(&mut diagnostics, segments, NotFoundItemType::Identifier)
.ok()?;
Some(match item {
ResolvedConcreteItem::Module(module_id) => db
.module_items(module_id)
.unwrap_or_default()
.iter()
.map(|item| CompletionItem {
label: item.name(db.upcast()).to_string(),
kind: ResolvedGenericItem::from_module_item(db, *item)
.ok()
.map(resolved_generic_item_completion_kind),
..CompletionItem::default()
})
.collect(),
ResolvedConcreteItem::Trait(item) => db
.trait_functions(item.trait_id(db))
.unwrap_or_default()
.iter()
.map(|(name, _)| CompletionItem {
label: name.to_string(),
kind: Some(CompletionItemKind::FUNCTION),
..CompletionItem::default()
})
.collect(),
ResolvedConcreteItem::Impl(item) => item
.concrete_trait(db)
.map(|trait_id| {
db.trait_functions(trait_id.trait_id(db))
.unwrap_or_default()
.iter()
.map(|(name, _)| CompletionItem {
label: name.to_string(),
kind: Some(CompletionItemKind::FUNCTION),
..CompletionItem::default()
})
.collect()
})
.unwrap_or_default(),
ResolvedConcreteItem::Type(ty) => match ty.lookup_intern(db) {
TypeLongId::Concrete(ConcreteTypeId::Enum(enum_id)) => db
.enum_variants(enum_id.enum_id(db))
.unwrap_or_default()
.iter()
.map(|(name, _)| CompletionItem {
label: name.to_string(),
kind: Some(CompletionItemKind::ENUM_MEMBER),
..CompletionItem::default()
})
.collect(),
_ => vec![],
},
_ => vec![],
})
}
#[tracing::instrument(level = "trace", skip_all)]
pub fn dot_completions(
db: &(dyn SemanticGroup + 'static),
file_id: FileId,
lookup_items: Vec<LookupItemId>,
expr: ast::ExprBinary,
) -> Option<Vec<CompletionItem>> {
let syntax_db = db.upcast();
// Get a resolver in the current context.
let lookup_item_id = lookup_items.into_iter().next()?;
let function_with_body = lookup_item_id.function_with_body()?;
let module_id = function_with_body.module_file_id(db.upcast()).0;
let resolver_data = lookup_item_id.resolver_data(db).ok()?;
let resolver = Resolver::with_data(
db,
resolver_data.as_ref().clone_with_inference_id(db, InferenceId::NoContext),
);
// Extract lhs node.
let node = expr.lhs(syntax_db);
let stable_ptr = node.stable_ptr().untyped();
// Get its semantic model.
let expr_id = db.lookup_expr_by_ptr(function_with_body, node.stable_ptr()).ok()?;
let semantic_expr = db.expr_semantic(function_with_body, expr_id);
// Get the type.
let ty = semantic_expr.ty();
if ty.is_missing(db) {
debug!("type is missing");
return None;
}
// Find relevant methods for type.
let offset = if let Some(ModuleId::Submodule(submodule_id)) =
db.find_module_containing_node(&expr.as_syntax_node())
{
let module_def_ast = submodule_id.stable_ptr(db.upcast()).lookup(syntax_db);
if let ast::MaybeModuleBody::Some(body) = module_def_ast.body(syntax_db) {
body.items(syntax_db).as_syntax_node().span_start_without_trivia(syntax_db)
} else {
TextOffset::default()
}
} else {
TextOffset::default()
};
let position = offset.position_in_file(db.upcast(), file_id).unwrap().to_lsp();
let relevant_methods = find_methods_for_type(db, resolver, ty, stable_ptr);
let mut completions = Vec::new();
for trait_function in relevant_methods {
let Some(completion) = completion_for_method(db, module_id, trait_function, position)
else {
continue;
};
completions.push(completion);
}
// Find members of the type.
let (_, long_ty) = peel_snapshots(db, ty);
if let TypeLongId::Concrete(ConcreteTypeId::Struct(concrete_struct_id)) = long_ty {
db.concrete_struct_members(concrete_struct_id).ok()?.into_iter().for_each(
|(name, member)| {
let completion = CompletionItem {
label: name.to_string(),
detail: Some(member.ty.format(db.upcast())),
kind: Some(CompletionItemKind::FIELD),
..CompletionItem::default()
};
completions.push(completion);
},
);
}
Some(completions)
}
/// Returns a completion item for a method.
#[tracing::instrument(level = "trace", skip_all)]
fn completion_for_method(
db: &dyn SemanticGroup,
module_id: ModuleId,
trait_function: TraitFunctionId,
position: Position,
) -> Option<CompletionItem> {
let trait_id = trait_function.trait_id(db.upcast());
let name = trait_function.name(db.upcast());
db.trait_function_signature(trait_function).ok()?;
// TODO(spapini): Add signature.
let detail = trait_id.full_path(db.upcast());
let trait_full_path = trait_id.full_path(db.upcast());
let mut additional_text_edits = vec![];
// If the trait is not in scope, add a use statement.
if !module_has_trait(db, module_id, trait_id)? {
additional_text_edits.push(TextEdit {
range: Range::new(position, position),
new_text: format!("use {trait_full_path};\n"),
});
}
let completion = CompletionItem {
label: format!("{}()", name),
insert_text: Some(format!("{}(", name)),
detail: Some(detail),
kind: Some(CompletionItemKind::METHOD),
additional_text_edits: Some(additional_text_edits),
..CompletionItem::default()
};
Some(completion)
}
/// Checks if a module has a trait in scope.
#[tracing::instrument(level = "trace", skip_all)]
fn module_has_trait(
db: &dyn SemanticGroup,
module_id: ModuleId,
trait_id: cairo_lang_defs::ids::TraitId,
) -> Option<bool> {
if db.module_traits_ids(module_id).ok()?.contains(&trait_id) {
return Some(true);
}
for use_id in db.module_uses_ids(module_id).ok()?.iter().copied() {
if db.use_resolved_item(use_id) == Ok(ResolvedGenericItem::Trait(trait_id)) {
return Some(true);
}
}
Some(false)
}
/// Finds all methods that can be called on a type.
#[tracing::instrument(level = "trace", skip_all)]
fn find_methods_for_type(
db: &dyn SemanticGroup,
mut resolver: Resolver<'_>,
ty: cairo_lang_semantic::TypeId,
stable_ptr: cairo_lang_syntax::node::ids::SyntaxStablePtrId,
) -> Vec<TraitFunctionId> {
let type_filter = match ty.head(db) {
Some(head) => TypeFilter::TypeHead(head),
None => TypeFilter::NoFilter,
};
let mut relevant_methods = Vec::new();
// Find methods on type.
// TODO(spapini): Look only in current crate dependencies.
for crate_id in db.crates() {
let methods = db.methods_in_crate(crate_id, type_filter.clone());
for trait_function in methods.iter().copied() {
let clone_data =
&mut resolver.inference().clone_with_inference_id(db, InferenceId::NoContext);
let mut inference = clone_data.inference(db);
let lookup_context = resolver.impl_lookup_context();
// Check if trait function signature's first param can fit our expr type.
let Some((concrete_trait_id, _)) = inference.infer_concrete_trait_by_self(
trait_function,
ty,
&lookup_context,
Some(stable_ptr),
|_| {},
) else {
debug!("can't fit");
continue;
};
// Find impls for it.
// ignore the result as nothing can be done with the error, if any.
inference.solve().ok();
if !matches!(
inference.trait_solution_set(concrete_trait_id, lookup_context),
Ok(SolutionSet::Unique(_) | SolutionSet::Ambiguous(_))
) {
continue;
}
relevant_methods.push(trait_function);
}
}
relevant_methods
}