aboutsummaryrefslogtreecommitdiff
path: root/crates/ide/src/completion/completion_context.rs
diff options
context:
space:
mode:
Diffstat (limited to 'crates/ide/src/completion/completion_context.rs')
-rw-r--r--crates/ide/src/completion/completion_context.rs523
1 files changed, 0 insertions, 523 deletions
diff --git a/crates/ide/src/completion/completion_context.rs b/crates/ide/src/completion/completion_context.rs
deleted file mode 100644
index d9f90477c..000000000
--- a/crates/ide/src/completion/completion_context.rs
+++ /dev/null
@@ -1,523 +0,0 @@
1//! FIXME: write short doc here
2
3use base_db::SourceDatabase;
4use hir::{Local, ScopeDef, Semantics, SemanticsScope, Type};
5use ide_db::RootDatabase;
6use syntax::{
7 algo::{find_covering_element, find_node_at_offset},
8 ast, match_ast, AstNode, NodeOrToken,
9 SyntaxKind::*,
10 SyntaxNode, SyntaxToken, TextRange, TextSize,
11};
12use test_utils::mark;
13use text_edit::Indel;
14
15use crate::{
16 call_info::ActiveParameter,
17 completion::{
18 patterns::{
19 fn_is_prev, for_is_prev2, has_bind_pat_parent, has_block_expr_parent,
20 has_field_list_parent, has_impl_as_prev_sibling, has_impl_parent,
21 has_item_list_or_source_file_parent, has_ref_parent, has_trait_as_prev_sibling,
22 has_trait_parent, if_is_prev, inside_impl_trait_block, is_in_loop_body, is_match_arm,
23 unsafe_is_prev,
24 },
25 CompletionConfig,
26 },
27 FilePosition,
28};
29
30/// `CompletionContext` is created early during completion to figure out, where
31/// exactly is the cursor, syntax-wise.
32#[derive(Debug)]
33pub(crate) struct CompletionContext<'a> {
34 pub(super) sema: Semantics<'a, RootDatabase>,
35 pub(super) scope: SemanticsScope<'a>,
36 pub(super) db: &'a RootDatabase,
37 pub(super) config: &'a CompletionConfig,
38 pub(super) position: FilePosition,
39 /// The token before the cursor, in the original file.
40 pub(super) original_token: SyntaxToken,
41 /// The token before the cursor, in the macro-expanded file.
42 pub(super) token: SyntaxToken,
43 pub(super) krate: Option<hir::Crate>,
44 pub(super) expected_type: Option<Type>,
45 pub(super) name_ref_syntax: Option<ast::NameRef>,
46 pub(super) function_syntax: Option<ast::Fn>,
47 pub(super) use_item_syntax: Option<ast::Use>,
48 pub(super) record_lit_syntax: Option<ast::RecordExpr>,
49 pub(super) record_pat_syntax: Option<ast::RecordPat>,
50 pub(super) record_field_syntax: Option<ast::RecordExprField>,
51 pub(super) impl_def: Option<ast::Impl>,
52 /// FIXME: `ActiveParameter` is string-based, which is very very wrong
53 pub(super) active_parameter: Option<ActiveParameter>,
54 pub(super) is_param: bool,
55 /// If a name-binding or reference to a const in a pattern.
56 /// Irrefutable patterns (like let) are excluded.
57 pub(super) is_pat_binding_or_const: bool,
58 /// A single-indent path, like `foo`. `::foo` should not be considered a trivial path.
59 pub(super) is_trivial_path: bool,
60 /// If not a trivial path, the prefix (qualifier).
61 pub(super) path_qual: Option<ast::Path>,
62 pub(super) after_if: bool,
63 /// `true` if we are a statement or a last expr in the block.
64 pub(super) can_be_stmt: bool,
65 /// `true` if we expect an expression at the cursor position.
66 pub(super) is_expr: bool,
67 /// Something is typed at the "top" level, in module or impl/trait.
68 pub(super) is_new_item: bool,
69 /// The receiver if this is a field or method access, i.e. writing something.<|>
70 pub(super) dot_receiver: Option<ast::Expr>,
71 pub(super) dot_receiver_is_ambiguous_float_literal: bool,
72 /// If this is a call (method or function) in particular, i.e. the () are already there.
73 pub(super) is_call: bool,
74 /// Like `is_call`, but for tuple patterns.
75 pub(super) is_pattern_call: bool,
76 /// If this is a macro call, i.e. the () are already there.
77 pub(super) is_macro_call: bool,
78 pub(super) is_path_type: bool,
79 pub(super) has_type_args: bool,
80 pub(super) attribute_under_caret: Option<ast::Attr>,
81 pub(super) mod_declaration_under_caret: Option<ast::Module>,
82 pub(super) unsafe_is_prev: bool,
83 pub(super) if_is_prev: bool,
84 pub(super) block_expr_parent: bool,
85 pub(super) bind_pat_parent: bool,
86 pub(super) ref_pat_parent: bool,
87 pub(super) in_loop_body: bool,
88 pub(super) has_trait_parent: bool,
89 pub(super) has_impl_parent: bool,
90 pub(super) inside_impl_trait_block: bool,
91 pub(super) has_field_list_parent: bool,
92 pub(super) trait_as_prev_sibling: bool,
93 pub(super) impl_as_prev_sibling: bool,
94 pub(super) is_match_arm: bool,
95 pub(super) has_item_list_or_source_file_parent: bool,
96 pub(super) for_is_prev2: bool,
97 pub(super) fn_is_prev: bool,
98 pub(super) locals: Vec<(String, Local)>,
99}
100
101impl<'a> CompletionContext<'a> {
102 pub(super) fn new(
103 db: &'a RootDatabase,
104 position: FilePosition,
105 config: &'a CompletionConfig,
106 ) -> Option<CompletionContext<'a>> {
107 let sema = Semantics::new(db);
108
109 let original_file = sema.parse(position.file_id);
110
111 // Insert a fake ident to get a valid parse tree. We will use this file
112 // to determine context, though the original_file will be used for
113 // actual completion.
114 let file_with_fake_ident = {
115 let parse = db.parse(position.file_id);
116 let edit = Indel::insert(position.offset, "intellijRulezz".to_string());
117 parse.reparse(&edit).tree()
118 };
119 let fake_ident_token =
120 file_with_fake_ident.syntax().token_at_offset(position.offset).right_biased().unwrap();
121
122 let krate = sema.to_module_def(position.file_id).map(|m| m.krate());
123 let original_token =
124 original_file.syntax().token_at_offset(position.offset).left_biased()?;
125 let token = sema.descend_into_macros(original_token.clone());
126 let scope = sema.scope_at_offset(&token.parent(), position.offset);
127 let mut locals = vec![];
128 scope.process_all_names(&mut |name, scope| {
129 if let ScopeDef::Local(local) = scope {
130 locals.push((name.to_string(), local));
131 }
132 });
133 let mut ctx = CompletionContext {
134 sema,
135 scope,
136 db,
137 config,
138 original_token,
139 token,
140 position,
141 krate,
142 expected_type: None,
143 name_ref_syntax: None,
144 function_syntax: None,
145 use_item_syntax: None,
146 record_lit_syntax: None,
147 record_pat_syntax: None,
148 record_field_syntax: None,
149 impl_def: None,
150 active_parameter: ActiveParameter::at(db, position),
151 is_param: false,
152 is_pat_binding_or_const: false,
153 is_trivial_path: false,
154 path_qual: None,
155 after_if: false,
156 can_be_stmt: false,
157 is_expr: false,
158 is_new_item: false,
159 dot_receiver: None,
160 is_call: false,
161 is_pattern_call: false,
162 is_macro_call: false,
163 is_path_type: false,
164 has_type_args: false,
165 dot_receiver_is_ambiguous_float_literal: false,
166 attribute_under_caret: None,
167 mod_declaration_under_caret: None,
168 unsafe_is_prev: false,
169 in_loop_body: false,
170 ref_pat_parent: false,
171 bind_pat_parent: false,
172 block_expr_parent: false,
173 has_trait_parent: false,
174 has_impl_parent: false,
175 inside_impl_trait_block: false,
176 has_field_list_parent: false,
177 trait_as_prev_sibling: false,
178 impl_as_prev_sibling: false,
179 if_is_prev: false,
180 is_match_arm: false,
181 has_item_list_or_source_file_parent: false,
182 for_is_prev2: false,
183 fn_is_prev: false,
184 locals,
185 };
186
187 let mut original_file = original_file.syntax().clone();
188 let mut hypothetical_file = file_with_fake_ident.syntax().clone();
189 let mut offset = position.offset;
190 let mut fake_ident_token = fake_ident_token;
191
192 // Are we inside a macro call?
193 while let (Some(actual_macro_call), Some(macro_call_with_fake_ident)) = (
194 find_node_at_offset::<ast::MacroCall>(&original_file, offset),
195 find_node_at_offset::<ast::MacroCall>(&hypothetical_file, offset),
196 ) {
197 if actual_macro_call.path().as_ref().map(|s| s.syntax().text())
198 != macro_call_with_fake_ident.path().as_ref().map(|s| s.syntax().text())
199 {
200 break;
201 }
202 let hypothetical_args = match macro_call_with_fake_ident.token_tree() {
203 Some(tt) => tt,
204 None => break,
205 };
206 if let (Some(actual_expansion), Some(hypothetical_expansion)) = (
207 ctx.sema.expand(&actual_macro_call),
208 ctx.sema.speculative_expand(
209 &actual_macro_call,
210 &hypothetical_args,
211 fake_ident_token,
212 ),
213 ) {
214 let new_offset = hypothetical_expansion.1.text_range().start();
215 if new_offset > actual_expansion.text_range().end() {
216 break;
217 }
218 original_file = actual_expansion;
219 hypothetical_file = hypothetical_expansion.0;
220 fake_ident_token = hypothetical_expansion.1;
221 offset = new_offset;
222 } else {
223 break;
224 }
225 }
226 ctx.fill_keyword_patterns(&hypothetical_file, offset);
227 ctx.fill(&original_file, hypothetical_file, offset);
228 Some(ctx)
229 }
230
231 /// Checks whether completions in that particular case don't make much sense.
232 /// Examples:
233 /// - `fn <|>` -- we expect function name, it's unlikely that "hint" will be helpful.
234 /// Exception for this case is `impl Trait for Foo`, where we would like to hint trait method names.
235 /// - `for _ i<|>` -- obviously, it'll be "in" keyword.
236 pub(crate) fn no_completion_required(&self) -> bool {
237 (self.fn_is_prev && !self.inside_impl_trait_block) || self.for_is_prev2
238 }
239
240 /// The range of the identifier that is being completed.
241 pub(crate) fn source_range(&self) -> TextRange {
242 // check kind of macro-expanded token, but use range of original token
243 let kind = self.token.kind();
244 if kind == IDENT || kind == UNDERSCORE || kind.is_keyword() {
245 mark::hit!(completes_if_prefix_is_keyword);
246 self.original_token.text_range()
247 } else {
248 TextRange::empty(self.position.offset)
249 }
250 }
251
252 fn fill_keyword_patterns(&mut self, file_with_fake_ident: &SyntaxNode, offset: TextSize) {
253 let fake_ident_token = file_with_fake_ident.token_at_offset(offset).right_biased().unwrap();
254 let syntax_element = NodeOrToken::Token(fake_ident_token);
255 self.block_expr_parent = has_block_expr_parent(syntax_element.clone());
256 self.unsafe_is_prev = unsafe_is_prev(syntax_element.clone());
257 self.if_is_prev = if_is_prev(syntax_element.clone());
258 self.bind_pat_parent = has_bind_pat_parent(syntax_element.clone());
259 self.ref_pat_parent = has_ref_parent(syntax_element.clone());
260 self.in_loop_body = is_in_loop_body(syntax_element.clone());
261 self.has_trait_parent = has_trait_parent(syntax_element.clone());
262 self.has_impl_parent = has_impl_parent(syntax_element.clone());
263 self.inside_impl_trait_block = inside_impl_trait_block(syntax_element.clone());
264 self.has_field_list_parent = has_field_list_parent(syntax_element.clone());
265 self.impl_as_prev_sibling = has_impl_as_prev_sibling(syntax_element.clone());
266 self.trait_as_prev_sibling = has_trait_as_prev_sibling(syntax_element.clone());
267 self.is_match_arm = is_match_arm(syntax_element.clone());
268 self.has_item_list_or_source_file_parent =
269 has_item_list_or_source_file_parent(syntax_element.clone());
270 self.mod_declaration_under_caret =
271 find_node_at_offset::<ast::Module>(&file_with_fake_ident, offset)
272 .filter(|module| module.item_list().is_none());
273 self.for_is_prev2 = for_is_prev2(syntax_element.clone());
274 self.fn_is_prev = fn_is_prev(syntax_element.clone());
275 }
276
277 fn fill(
278 &mut self,
279 original_file: &SyntaxNode,
280 file_with_fake_ident: SyntaxNode,
281 offset: TextSize,
282 ) {
283 // FIXME: this is wrong in at least two cases:
284 // * when there's no token `foo(<|>)`
285 // * when there is a token, but it happens to have type of it's own
286 self.expected_type = self
287 .token
288 .ancestors()
289 .find_map(|node| {
290 let ty = match_ast! {
291 match node {
292 ast::Pat(it) => self.sema.type_of_pat(&it),
293 ast::Expr(it) => self.sema.type_of_expr(&it),
294 _ => return None,
295 }
296 };
297 Some(ty)
298 })
299 .flatten();
300 self.attribute_under_caret = find_node_at_offset(&file_with_fake_ident, offset);
301
302 // First, let's try to complete a reference to some declaration.
303 if let Some(name_ref) = find_node_at_offset::<ast::NameRef>(&file_with_fake_ident, offset) {
304 // Special case, `trait T { fn foo(i_am_a_name_ref) {} }`.
305 // See RFC#1685.
306 if is_node::<ast::Param>(name_ref.syntax()) {
307 self.is_param = true;
308 return;
309 }
310 // FIXME: remove this (V) duplication and make the check more precise
311 if name_ref.syntax().ancestors().find_map(ast::RecordPatFieldList::cast).is_some() {
312 self.record_pat_syntax =
313 self.sema.find_node_at_offset_with_macros(&original_file, offset);
314 }
315 self.classify_name_ref(original_file, name_ref, offset);
316 }
317
318 // Otherwise, see if this is a declaration. We can use heuristics to
319 // suggest declaration names, see `CompletionKind::Magic`.
320 if let Some(name) = find_node_at_offset::<ast::Name>(&file_with_fake_ident, offset) {
321 if let Some(bind_pat) = name.syntax().ancestors().find_map(ast::IdentPat::cast) {
322 self.is_pat_binding_or_const = true;
323 if bind_pat.at_token().is_some()
324 || bind_pat.ref_token().is_some()
325 || bind_pat.mut_token().is_some()
326 {
327 self.is_pat_binding_or_const = false;
328 }
329 if bind_pat.syntax().parent().and_then(ast::RecordPatFieldList::cast).is_some() {
330 self.is_pat_binding_or_const = false;
331 }
332 if let Some(let_stmt) = bind_pat.syntax().ancestors().find_map(ast::LetStmt::cast) {
333 if let Some(pat) = let_stmt.pat() {
334 if pat.syntax().text_range().contains_range(bind_pat.syntax().text_range())
335 {
336 self.is_pat_binding_or_const = false;
337 }
338 }
339 }
340 }
341 if is_node::<ast::Param>(name.syntax()) {
342 self.is_param = true;
343 return;
344 }
345 // FIXME: remove this (^) duplication and make the check more precise
346 if name.syntax().ancestors().find_map(ast::RecordPatFieldList::cast).is_some() {
347 self.record_pat_syntax =
348 self.sema.find_node_at_offset_with_macros(&original_file, offset);
349 }
350 }
351 }
352
353 fn classify_name_ref(
354 &mut self,
355 original_file: &SyntaxNode,
356 name_ref: ast::NameRef,
357 offset: TextSize,
358 ) {
359 self.name_ref_syntax =
360 find_node_at_offset(&original_file, name_ref.syntax().text_range().start());
361 let name_range = name_ref.syntax().text_range();
362 if ast::RecordExprField::for_field_name(&name_ref).is_some() {
363 self.record_lit_syntax =
364 self.sema.find_node_at_offset_with_macros(&original_file, offset);
365 }
366
367 self.impl_def = self
368 .sema
369 .ancestors_with_macros(self.token.parent())
370 .take_while(|it| it.kind() != SOURCE_FILE && it.kind() != MODULE)
371 .find_map(ast::Impl::cast);
372
373 let top_node = name_ref
374 .syntax()
375 .ancestors()
376 .take_while(|it| it.text_range() == name_range)
377 .last()
378 .unwrap();
379
380 match top_node.parent().map(|it| it.kind()) {
381 Some(SOURCE_FILE) | Some(ITEM_LIST) => {
382 self.is_new_item = true;
383 return;
384 }
385 _ => (),
386 }
387
388 self.use_item_syntax =
389 self.sema.ancestors_with_macros(self.token.parent()).find_map(ast::Use::cast);
390
391 self.function_syntax = self
392 .sema
393 .ancestors_with_macros(self.token.parent())
394 .take_while(|it| it.kind() != SOURCE_FILE && it.kind() != MODULE)
395 .find_map(ast::Fn::cast);
396
397 self.record_field_syntax = self
398 .sema
399 .ancestors_with_macros(self.token.parent())
400 .take_while(|it| {
401 it.kind() != SOURCE_FILE && it.kind() != MODULE && it.kind() != CALL_EXPR
402 })
403 .find_map(ast::RecordExprField::cast);
404
405 let parent = match name_ref.syntax().parent() {
406 Some(it) => it,
407 None => return,
408 };
409
410 if let Some(segment) = ast::PathSegment::cast(parent.clone()) {
411 let path = segment.parent_path();
412 self.is_call = path
413 .syntax()
414 .parent()
415 .and_then(ast::PathExpr::cast)
416 .and_then(|it| it.syntax().parent().and_then(ast::CallExpr::cast))
417 .is_some();
418 self.is_macro_call = path.syntax().parent().and_then(ast::MacroCall::cast).is_some();
419 self.is_pattern_call =
420 path.syntax().parent().and_then(ast::TupleStructPat::cast).is_some();
421
422 self.is_path_type = path.syntax().parent().and_then(ast::PathType::cast).is_some();
423 self.has_type_args = segment.generic_arg_list().is_some();
424
425 if let Some(path) = path_or_use_tree_qualifier(&path) {
426 self.path_qual = path
427 .segment()
428 .and_then(|it| {
429 find_node_with_range::<ast::PathSegment>(
430 original_file,
431 it.syntax().text_range(),
432 )
433 })
434 .map(|it| it.parent_path());
435 return;
436 }
437
438 if let Some(segment) = path.segment() {
439 if segment.coloncolon_token().is_some() {
440 return;
441 }
442 }
443
444 self.is_trivial_path = true;
445
446 // Find either enclosing expr statement (thing with `;`) or a
447 // block. If block, check that we are the last expr.
448 self.can_be_stmt = name_ref
449 .syntax()
450 .ancestors()
451 .find_map(|node| {
452 if let Some(stmt) = ast::ExprStmt::cast(node.clone()) {
453 return Some(stmt.syntax().text_range() == name_ref.syntax().text_range());
454 }
455 if let Some(block) = ast::BlockExpr::cast(node) {
456 return Some(
457 block.expr().map(|e| e.syntax().text_range())
458 == Some(name_ref.syntax().text_range()),
459 );
460 }
461 None
462 })
463 .unwrap_or(false);
464 self.is_expr = path.syntax().parent().and_then(ast::PathExpr::cast).is_some();
465
466 if let Some(off) = name_ref.syntax().text_range().start().checked_sub(2.into()) {
467 if let Some(if_expr) =
468 self.sema.find_node_at_offset_with_macros::<ast::IfExpr>(original_file, off)
469 {
470 if if_expr.syntax().text_range().end() < name_ref.syntax().text_range().start()
471 {
472 self.after_if = true;
473 }
474 }
475 }
476 }
477 if let Some(field_expr) = ast::FieldExpr::cast(parent.clone()) {
478 // The receiver comes before the point of insertion of the fake
479 // ident, so it should have the same range in the non-modified file
480 self.dot_receiver = field_expr
481 .expr()
482 .map(|e| e.syntax().text_range())
483 .and_then(|r| find_node_with_range(original_file, r));
484 self.dot_receiver_is_ambiguous_float_literal =
485 if let Some(ast::Expr::Literal(l)) = &self.dot_receiver {
486 match l.kind() {
487 ast::LiteralKind::FloatNumber { .. } => l.token().text().ends_with('.'),
488 _ => false,
489 }
490 } else {
491 false
492 };
493 }
494 if let Some(method_call_expr) = ast::MethodCallExpr::cast(parent) {
495 // As above
496 self.dot_receiver = method_call_expr
497 .receiver()
498 .map(|e| e.syntax().text_range())
499 .and_then(|r| find_node_with_range(original_file, r));
500 self.is_call = true;
501 }
502 }
503}
504
505fn find_node_with_range<N: AstNode>(syntax: &SyntaxNode, range: TextRange) -> Option<N> {
506 find_covering_element(syntax, range).ancestors().find_map(N::cast)
507}
508
509fn is_node<N: AstNode>(node: &SyntaxNode) -> bool {
510 match node.ancestors().find_map(N::cast) {
511 None => false,
512 Some(n) => n.syntax().text_range() == node.text_range(),
513 }
514}
515
516fn path_or_use_tree_qualifier(path: &ast::Path) -> Option<ast::Path> {
517 if let Some(qual) = path.qualifier() {
518 return Some(qual);
519 }
520 let use_tree_list = path.syntax().ancestors().find_map(ast::UseTreeList::cast)?;
521 let use_tree = use_tree_list.syntax().parent().and_then(ast::UseTree::cast)?;
522 use_tree.path()
523}