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