diff options
Diffstat (limited to 'crates/ide_completion/src/context.rs')
-rw-r--r-- | crates/ide_completion/src/context.rs | 370 |
1 files changed, 199 insertions, 171 deletions
diff --git a/crates/ide_completion/src/context.rs b/crates/ide_completion/src/context.rs index 1ec59ff80..8d6440cb2 100644 --- a/crates/ide_completion/src/context.rs +++ b/crates/ide_completion/src/context.rs | |||
@@ -1,29 +1,34 @@ | |||
1 | //! See `CompletionContext` structure. | 1 | //! See `CompletionContext` structure. |
2 | 2 | ||
3 | use hir::{Local, ScopeDef, Semantics, SemanticsScope, Type}; | 3 | use hir::{Local, ScopeDef, Semantics, SemanticsScope, Type}; |
4 | use ide_db::base_db::{FilePosition, SourceDatabase}; | 4 | use ide_db::{ |
5 | use ide_db::{call_info::ActiveParameter, RootDatabase}; | 5 | base_db::{FilePosition, SourceDatabase}, |
6 | call_info::ActiveParameter, | ||
7 | RootDatabase, | ||
8 | }; | ||
6 | use syntax::{ | 9 | use syntax::{ |
7 | algo::find_node_at_offset, | 10 | algo::find_node_at_offset, |
8 | ast::{self, NameOrNameRef, NameOwner}, | 11 | ast::{self, NameOrNameRef, NameOwner}, |
9 | match_ast, AstNode, NodeOrToken, | 12 | match_ast, AstNode, NodeOrToken, |
10 | SyntaxKind::*, | 13 | SyntaxKind::{self, *}, |
11 | SyntaxNode, SyntaxToken, TextRange, TextSize, | 14 | SyntaxNode, SyntaxToken, TextRange, TextSize, T, |
12 | }; | 15 | }; |
13 | |||
14 | use text_edit::Indel; | 16 | use text_edit::Indel; |
15 | 17 | ||
16 | use crate::{ | 18 | use crate::{ |
17 | patterns::{ | 19 | patterns::{ |
18 | fn_is_prev, for_is_prev2, has_bind_pat_parent, has_block_expr_parent, | 20 | determine_location, determine_prev_sibling, for_is_prev2, inside_impl_trait_block, |
19 | has_field_list_parent, has_impl_as_prev_sibling, has_impl_parent, | 21 | is_in_loop_body, is_match_arm, previous_token, ImmediateLocation, ImmediatePrevSibling, |
20 | has_item_list_or_source_file_parent, has_ref_parent, has_trait_as_prev_sibling, | ||
21 | has_trait_parent, if_is_prev, inside_impl_trait_block, is_in_loop_body, is_match_arm, | ||
22 | unsafe_is_prev, | ||
23 | }, | 22 | }, |
24 | CompletionConfig, | 23 | CompletionConfig, |
25 | }; | 24 | }; |
26 | 25 | ||
26 | #[derive(Copy, Clone, Debug, PartialEq, Eq)] | ||
27 | pub(crate) enum PatternRefutability { | ||
28 | Refutable, | ||
29 | Irrefutable, | ||
30 | } | ||
31 | |||
27 | /// `CompletionContext` is created early during completion to figure out, where | 32 | /// `CompletionContext` is created early during completion to figure out, where |
28 | /// exactly is the cursor, syntax-wise. | 33 | /// exactly is the cursor, syntax-wise. |
29 | #[derive(Debug)] | 34 | #[derive(Debug)] |
@@ -41,28 +46,38 @@ pub(crate) struct CompletionContext<'a> { | |||
41 | pub(super) expected_name: Option<NameOrNameRef>, | 46 | pub(super) expected_name: Option<NameOrNameRef>, |
42 | pub(super) expected_type: Option<Type>, | 47 | pub(super) expected_type: Option<Type>, |
43 | pub(super) name_ref_syntax: Option<ast::NameRef>, | 48 | pub(super) name_ref_syntax: Option<ast::NameRef>, |
44 | pub(super) lifetime_syntax: Option<ast::Lifetime>, | 49 | |
45 | pub(super) lifetime_param_syntax: Option<ast::LifetimeParam>, | ||
46 | pub(super) function_syntax: Option<ast::Fn>, | ||
47 | pub(super) use_item_syntax: Option<ast::Use>, | 50 | pub(super) use_item_syntax: Option<ast::Use>, |
51 | |||
52 | /// The parent function of the cursor position if it exists. | ||
53 | pub(super) function_def: Option<ast::Fn>, | ||
54 | /// The parent impl of the cursor position if it exists. | ||
55 | pub(super) impl_def: Option<ast::Impl>, | ||
56 | |||
57 | /// RecordExpr the token is a field of | ||
48 | pub(super) record_lit_syntax: Option<ast::RecordExpr>, | 58 | pub(super) record_lit_syntax: Option<ast::RecordExpr>, |
59 | /// RecordPat the token is a field of | ||
49 | pub(super) record_pat_syntax: Option<ast::RecordPat>, | 60 | pub(super) record_pat_syntax: Option<ast::RecordPat>, |
50 | pub(super) record_field_syntax: Option<ast::RecordExprField>, | 61 | |
51 | pub(super) impl_def: Option<ast::Impl>, | 62 | // potentially set if we are completing a lifetime |
63 | pub(super) lifetime_syntax: Option<ast::Lifetime>, | ||
64 | pub(super) lifetime_param_syntax: Option<ast::LifetimeParam>, | ||
52 | pub(super) lifetime_allowed: bool, | 65 | pub(super) lifetime_allowed: bool, |
66 | pub(super) is_label_ref: bool, | ||
67 | |||
68 | // potentially set if we are completing a name | ||
69 | pub(super) is_pat_or_const: Option<PatternRefutability>, | ||
70 | pub(super) is_param: bool, | ||
71 | |||
72 | pub(super) completion_location: Option<ImmediateLocation>, | ||
73 | pub(super) prev_sibling: Option<ImmediatePrevSibling>, | ||
74 | |||
53 | /// FIXME: `ActiveParameter` is string-based, which is very very wrong | 75 | /// FIXME: `ActiveParameter` is string-based, which is very very wrong |
54 | pub(super) active_parameter: Option<ActiveParameter>, | 76 | pub(super) active_parameter: Option<ActiveParameter>, |
55 | pub(super) is_param: bool, | ||
56 | pub(super) is_label_ref: bool, | ||
57 | /// If a name-binding or reference to a const in a pattern. | ||
58 | /// Irrefutable patterns (like let) are excluded. | ||
59 | pub(super) is_pat_binding_or_const: bool, | ||
60 | pub(super) is_irrefutable_pat_binding: bool, | ||
61 | /// A single-indent path, like `foo`. `::foo` should not be considered a trivial path. | 77 | /// A single-indent path, like `foo`. `::foo` should not be considered a trivial path. |
62 | pub(super) is_trivial_path: bool, | 78 | pub(super) is_trivial_path: bool, |
63 | /// If not a trivial path, the prefix (qualifier). | 79 | /// If not a trivial path, the prefix (qualifier). |
64 | pub(super) path_qual: Option<ast::Path>, | 80 | pub(super) path_qual: Option<ast::Path>, |
65 | pub(super) after_if: bool, | ||
66 | /// `true` if we are a statement or a last expr in the block. | 81 | /// `true` if we are a statement or a last expr in the block. |
67 | pub(super) can_be_stmt: bool, | 82 | pub(super) can_be_stmt: bool, |
68 | /// `true` if we expect an expression at the cursor position. | 83 | /// `true` if we expect an expression at the cursor position. |
@@ -82,24 +97,15 @@ pub(crate) struct CompletionContext<'a> { | |||
82 | pub(super) has_type_args: bool, | 97 | pub(super) has_type_args: bool, |
83 | pub(super) attribute_under_caret: Option<ast::Attr>, | 98 | pub(super) attribute_under_caret: Option<ast::Attr>, |
84 | pub(super) mod_declaration_under_caret: Option<ast::Module>, | 99 | pub(super) mod_declaration_under_caret: Option<ast::Module>, |
85 | pub(super) unsafe_is_prev: bool, | 100 | pub(super) locals: Vec<(String, Local)>, |
86 | pub(super) if_is_prev: bool, | 101 | |
87 | pub(super) block_expr_parent: bool, | 102 | // keyword patterns |
88 | pub(super) bind_pat_parent: bool, | 103 | pub(super) previous_token: Option<SyntaxToken>, |
89 | pub(super) ref_pat_parent: bool, | ||
90 | pub(super) in_loop_body: bool, | 104 | pub(super) in_loop_body: bool, |
91 | pub(super) has_trait_parent: bool, | ||
92 | pub(super) has_impl_parent: bool, | ||
93 | pub(super) inside_impl_trait_block: bool, | ||
94 | pub(super) has_field_list_parent: bool, | ||
95 | pub(super) trait_as_prev_sibling: bool, | ||
96 | pub(super) impl_as_prev_sibling: bool, | ||
97 | pub(super) is_match_arm: bool, | 105 | pub(super) is_match_arm: bool, |
98 | pub(super) has_item_list_or_source_file_parent: bool, | ||
99 | pub(super) for_is_prev2: bool, | ||
100 | pub(super) fn_is_prev: bool, | ||
101 | pub(super) incomplete_let: bool, | 106 | pub(super) incomplete_let: bool, |
102 | pub(super) locals: Vec<(String, Local)>, | 107 | |
108 | no_completion_required: bool, | ||
103 | } | 109 | } |
104 | 110 | ||
105 | impl<'a> CompletionContext<'a> { | 111 | impl<'a> CompletionContext<'a> { |
@@ -149,20 +155,17 @@ impl<'a> CompletionContext<'a> { | |||
149 | name_ref_syntax: None, | 155 | name_ref_syntax: None, |
150 | lifetime_syntax: None, | 156 | lifetime_syntax: None, |
151 | lifetime_param_syntax: None, | 157 | lifetime_param_syntax: None, |
152 | function_syntax: None, | 158 | function_def: None, |
153 | use_item_syntax: None, | 159 | use_item_syntax: None, |
154 | record_lit_syntax: None, | 160 | record_lit_syntax: None, |
155 | record_pat_syntax: None, | 161 | record_pat_syntax: None, |
156 | record_field_syntax: None, | ||
157 | impl_def: None, | 162 | impl_def: None, |
158 | active_parameter: ActiveParameter::at(db, position), | 163 | active_parameter: ActiveParameter::at(db, position), |
159 | is_label_ref: false, | 164 | is_label_ref: false, |
160 | is_param: false, | 165 | is_param: false, |
161 | is_pat_binding_or_const: false, | 166 | is_pat_or_const: None, |
162 | is_irrefutable_pat_binding: false, | ||
163 | is_trivial_path: false, | 167 | is_trivial_path: false, |
164 | path_qual: None, | 168 | path_qual: None, |
165 | after_if: false, | ||
166 | can_be_stmt: false, | 169 | can_be_stmt: false, |
167 | is_expr: false, | 170 | is_expr: false, |
168 | is_new_item: false, | 171 | is_new_item: false, |
@@ -175,67 +178,57 @@ impl<'a> CompletionContext<'a> { | |||
175 | has_type_args: false, | 178 | has_type_args: false, |
176 | attribute_under_caret: None, | 179 | attribute_under_caret: None, |
177 | mod_declaration_under_caret: None, | 180 | mod_declaration_under_caret: None, |
178 | unsafe_is_prev: false, | 181 | previous_token: None, |
179 | if_is_prev: false, | ||
180 | block_expr_parent: false, | ||
181 | bind_pat_parent: false, | ||
182 | ref_pat_parent: false, | ||
183 | in_loop_body: false, | 182 | in_loop_body: false, |
184 | has_trait_parent: false, | 183 | completion_location: None, |
185 | has_impl_parent: false, | 184 | prev_sibling: None, |
186 | inside_impl_trait_block: false, | ||
187 | has_field_list_parent: false, | ||
188 | trait_as_prev_sibling: false, | ||
189 | impl_as_prev_sibling: false, | ||
190 | is_match_arm: false, | 185 | is_match_arm: false, |
191 | has_item_list_or_source_file_parent: false, | 186 | no_completion_required: false, |
192 | for_is_prev2: false, | ||
193 | fn_is_prev: false, | ||
194 | incomplete_let: false, | 187 | incomplete_let: false, |
195 | locals, | 188 | locals, |
196 | }; | 189 | }; |
197 | 190 | ||
198 | let mut original_file = original_file.syntax().clone(); | 191 | let mut original_file = original_file.syntax().clone(); |
199 | let mut hypothetical_file = file_with_fake_ident.syntax().clone(); | 192 | let mut speculative_file = file_with_fake_ident.syntax().clone(); |
200 | let mut offset = position.offset; | 193 | let mut offset = position.offset; |
201 | let mut fake_ident_token = fake_ident_token; | 194 | let mut fake_ident_token = fake_ident_token; |
202 | 195 | ||
203 | // Are we inside a macro call? | 196 | // Are we inside a macro call? |
204 | while let (Some(actual_macro_call), Some(macro_call_with_fake_ident)) = ( | 197 | while let (Some(actual_macro_call), Some(macro_call_with_fake_ident)) = ( |
205 | find_node_at_offset::<ast::MacroCall>(&original_file, offset), | 198 | find_node_at_offset::<ast::MacroCall>(&original_file, offset), |
206 | find_node_at_offset::<ast::MacroCall>(&hypothetical_file, offset), | 199 | find_node_at_offset::<ast::MacroCall>(&speculative_file, offset), |
207 | ) { | 200 | ) { |
208 | if actual_macro_call.path().as_ref().map(|s| s.syntax().text()) | 201 | if actual_macro_call.path().as_ref().map(|s| s.syntax().text()) |
209 | != macro_call_with_fake_ident.path().as_ref().map(|s| s.syntax().text()) | 202 | != macro_call_with_fake_ident.path().as_ref().map(|s| s.syntax().text()) |
210 | { | 203 | { |
211 | break; | 204 | break; |
212 | } | 205 | } |
213 | let hypothetical_args = match macro_call_with_fake_ident.token_tree() { | 206 | let speculative_args = match macro_call_with_fake_ident.token_tree() { |
214 | Some(tt) => tt, | 207 | Some(tt) => tt, |
215 | None => break, | 208 | None => break, |
216 | }; | 209 | }; |
217 | if let (Some(actual_expansion), Some(hypothetical_expansion)) = ( | 210 | if let (Some(actual_expansion), Some(speculative_expansion)) = ( |
218 | ctx.sema.expand(&actual_macro_call), | 211 | ctx.sema.expand(&actual_macro_call), |
219 | ctx.sema.speculative_expand( | 212 | ctx.sema.speculative_expand( |
220 | &actual_macro_call, | 213 | &actual_macro_call, |
221 | &hypothetical_args, | 214 | &speculative_args, |
222 | fake_ident_token, | 215 | fake_ident_token, |
223 | ), | 216 | ), |
224 | ) { | 217 | ) { |
225 | let new_offset = hypothetical_expansion.1.text_range().start(); | 218 | let new_offset = speculative_expansion.1.text_range().start(); |
226 | if new_offset > actual_expansion.text_range().end() { | 219 | if new_offset > actual_expansion.text_range().end() { |
227 | break; | 220 | break; |
228 | } | 221 | } |
229 | original_file = actual_expansion; | 222 | original_file = actual_expansion; |
230 | hypothetical_file = hypothetical_expansion.0; | 223 | speculative_file = speculative_expansion.0; |
231 | fake_ident_token = hypothetical_expansion.1; | 224 | fake_ident_token = speculative_expansion.1; |
232 | offset = new_offset; | 225 | offset = new_offset; |
233 | } else { | 226 | } else { |
234 | break; | 227 | break; |
235 | } | 228 | } |
236 | } | 229 | } |
237 | ctx.fill_keyword_patterns(&hypothetical_file, offset); | 230 | ctx.fill_keyword_patterns(&speculative_file, offset); |
238 | ctx.fill(&original_file, hypothetical_file, offset); | 231 | ctx.fill(&original_file, speculative_file, offset); |
239 | Some(ctx) | 232 | Some(ctx) |
240 | } | 233 | } |
241 | 234 | ||
@@ -245,7 +238,7 @@ impl<'a> CompletionContext<'a> { | |||
245 | /// Exception for this case is `impl Trait for Foo`, where we would like to hint trait method names. | 238 | /// Exception for this case is `impl Trait for Foo`, where we would like to hint trait method names. |
246 | /// - `for _ i$0` -- obviously, it'll be "in" keyword. | 239 | /// - `for _ i$0` -- obviously, it'll be "in" keyword. |
247 | pub(crate) fn no_completion_required(&self) -> bool { | 240 | pub(crate) fn no_completion_required(&self) -> bool { |
248 | (self.fn_is_prev && !self.inside_impl_trait_block) || self.for_is_prev2 | 241 | self.no_completion_required |
249 | } | 242 | } |
250 | 243 | ||
251 | /// The range of the identifier that is being completed. | 244 | /// The range of the identifier that is being completed. |
@@ -264,33 +257,85 @@ impl<'a> CompletionContext<'a> { | |||
264 | } | 257 | } |
265 | } | 258 | } |
266 | 259 | ||
260 | pub(crate) fn previous_token_is(&self, kind: SyntaxKind) -> bool { | ||
261 | self.previous_token.as_ref().map_or(false, |tok| tok.kind() == kind) | ||
262 | } | ||
263 | |||
264 | pub(crate) fn expects_assoc_item(&self) -> bool { | ||
265 | matches!( | ||
266 | self.completion_location, | ||
267 | Some(ImmediateLocation::Trait) | Some(ImmediateLocation::Impl) | ||
268 | ) | ||
269 | } | ||
270 | |||
271 | pub(crate) fn expects_use_tree(&self) -> bool { | ||
272 | matches!(self.completion_location, Some(ImmediateLocation::Use)) | ||
273 | } | ||
274 | |||
275 | pub(crate) fn expects_non_trait_assoc_item(&self) -> bool { | ||
276 | matches!(self.completion_location, Some(ImmediateLocation::Impl)) | ||
277 | } | ||
278 | |||
279 | pub(crate) fn expects_item(&self) -> bool { | ||
280 | matches!(self.completion_location, Some(ImmediateLocation::ItemList)) | ||
281 | } | ||
282 | |||
283 | pub(crate) fn expects_expression(&self) -> bool { | ||
284 | self.is_expr | ||
285 | } | ||
286 | |||
287 | pub(crate) fn has_block_expr_parent(&self) -> bool { | ||
288 | matches!(self.completion_location, Some(ImmediateLocation::BlockExpr)) | ||
289 | } | ||
290 | |||
291 | pub(crate) fn expects_ident_pat_or_ref_expr(&self) -> bool { | ||
292 | matches!( | ||
293 | self.completion_location, | ||
294 | Some(ImmediateLocation::IdentPat) | Some(ImmediateLocation::RefExpr) | ||
295 | ) | ||
296 | } | ||
297 | |||
298 | pub(crate) fn expect_record_field(&self) -> bool { | ||
299 | matches!(self.completion_location, Some(ImmediateLocation::RecordField)) | ||
300 | } | ||
301 | |||
302 | pub(crate) fn has_impl_or_trait_prev_sibling(&self) -> bool { | ||
303 | matches!( | ||
304 | self.prev_sibling, | ||
305 | Some(ImmediatePrevSibling::ImplDefType) | Some(ImmediatePrevSibling::TraitDefName) | ||
306 | ) | ||
307 | } | ||
308 | |||
309 | pub(crate) fn after_if(&self) -> bool { | ||
310 | matches!(self.prev_sibling, Some(ImmediatePrevSibling::IfExpr)) | ||
311 | } | ||
312 | |||
313 | pub(crate) fn is_path_disallowed(&self) -> bool { | ||
314 | self.record_lit_syntax.is_some() | ||
315 | || self.record_pat_syntax.is_some() | ||
316 | || self.attribute_under_caret.is_some() | ||
317 | || self.mod_declaration_under_caret.is_some() | ||
318 | } | ||
319 | |||
267 | fn fill_keyword_patterns(&mut self, file_with_fake_ident: &SyntaxNode, offset: TextSize) { | 320 | fn fill_keyword_patterns(&mut self, file_with_fake_ident: &SyntaxNode, offset: TextSize) { |
268 | let fake_ident_token = file_with_fake_ident.token_at_offset(offset).right_biased().unwrap(); | 321 | let fake_ident_token = file_with_fake_ident.token_at_offset(offset).right_biased().unwrap(); |
269 | let syntax_element = NodeOrToken::Token(fake_ident_token); | 322 | let syntax_element = NodeOrToken::Token(fake_ident_token); |
270 | self.block_expr_parent = has_block_expr_parent(syntax_element.clone()); | 323 | self.previous_token = previous_token(syntax_element.clone()); |
271 | self.unsafe_is_prev = unsafe_is_prev(syntax_element.clone()); | ||
272 | self.if_is_prev = if_is_prev(syntax_element.clone()); | ||
273 | self.bind_pat_parent = has_bind_pat_parent(syntax_element.clone()); | ||
274 | self.ref_pat_parent = has_ref_parent(syntax_element.clone()); | ||
275 | self.in_loop_body = is_in_loop_body(syntax_element.clone()); | 324 | self.in_loop_body = is_in_loop_body(syntax_element.clone()); |
276 | self.has_trait_parent = has_trait_parent(syntax_element.clone()); | ||
277 | self.has_impl_parent = has_impl_parent(syntax_element.clone()); | ||
278 | self.inside_impl_trait_block = inside_impl_trait_block(syntax_element.clone()); | ||
279 | self.has_field_list_parent = has_field_list_parent(syntax_element.clone()); | ||
280 | self.impl_as_prev_sibling = has_impl_as_prev_sibling(syntax_element.clone()); | ||
281 | self.trait_as_prev_sibling = has_trait_as_prev_sibling(syntax_element.clone()); | ||
282 | self.is_match_arm = is_match_arm(syntax_element.clone()); | 325 | self.is_match_arm = is_match_arm(syntax_element.clone()); |
283 | self.has_item_list_or_source_file_parent = | 326 | |
284 | has_item_list_or_source_file_parent(syntax_element.clone()); | ||
285 | self.mod_declaration_under_caret = | 327 | self.mod_declaration_under_caret = |
286 | find_node_at_offset::<ast::Module>(&file_with_fake_ident, offset) | 328 | find_node_at_offset::<ast::Module>(&file_with_fake_ident, offset) |
287 | .filter(|module| module.item_list().is_none()); | 329 | .filter(|module| module.item_list().is_none()); |
288 | self.for_is_prev2 = for_is_prev2(syntax_element.clone()); | ||
289 | self.fn_is_prev = fn_is_prev(syntax_element.clone()); | ||
290 | self.incomplete_let = | 330 | self.incomplete_let = |
291 | syntax_element.ancestors().take(6).find_map(ast::LetStmt::cast).map_or(false, |it| { | 331 | syntax_element.ancestors().take(6).find_map(ast::LetStmt::cast).map_or(false, |it| { |
292 | it.syntax().text_range().end() == syntax_element.text_range().end() | 332 | it.syntax().text_range().end() == syntax_element.text_range().end() |
293 | }); | 333 | }); |
334 | |||
335 | let inside_impl_trait_block = inside_impl_trait_block(syntax_element.clone()); | ||
336 | let fn_is_prev = self.previous_token_is(T![fn]); | ||
337 | let for_is_prev2 = for_is_prev2(syntax_element.clone()); | ||
338 | self.no_completion_required = (fn_is_prev && !inside_impl_trait_block) || for_is_prev2; | ||
294 | } | 339 | } |
295 | 340 | ||
296 | fn fill_impl_def(&mut self) { | 341 | fn fill_impl_def(&mut self) { |
@@ -412,67 +457,21 @@ impl<'a> CompletionContext<'a> { | |||
412 | self.expected_type = expected_type; | 457 | self.expected_type = expected_type; |
413 | self.expected_name = expected_name; | 458 | self.expected_name = expected_name; |
414 | self.attribute_under_caret = find_node_at_offset(&file_with_fake_ident, offset); | 459 | self.attribute_under_caret = find_node_at_offset(&file_with_fake_ident, offset); |
415 | 460 | let name_like = match find_node_at_offset(&&file_with_fake_ident, offset) { | |
416 | if let Some(lifetime) = find_node_at_offset::<ast::Lifetime>(&file_with_fake_ident, offset) | 461 | Some(it) => it, |
417 | { | 462 | None => return, |
418 | self.classify_lifetime(original_file, lifetime, offset); | 463 | }; |
419 | } | 464 | self.completion_location = determine_location(&name_like); |
420 | 465 | self.prev_sibling = determine_prev_sibling(&name_like); | |
421 | // First, let's try to complete a reference to some declaration. | 466 | match name_like { |
422 | if let Some(name_ref) = find_node_at_offset::<ast::NameRef>(&file_with_fake_ident, offset) { | 467 | ast::NameLike::Lifetime(lifetime) => { |
423 | // Special case, `trait T { fn foo(i_am_a_name_ref) {} }`. | 468 | self.classify_lifetime(original_file, lifetime, offset); |
424 | // See RFC#1685. | ||
425 | if is_node::<ast::Param>(name_ref.syntax()) { | ||
426 | self.is_param = true; | ||
427 | return; | ||
428 | } | ||
429 | // FIXME: remove this (V) duplication and make the check more precise | ||
430 | if name_ref.syntax().ancestors().find_map(ast::RecordPatFieldList::cast).is_some() { | ||
431 | self.record_pat_syntax = | ||
432 | self.sema.find_node_at_offset_with_macros(&original_file, offset); | ||
433 | } | ||
434 | self.classify_name_ref(original_file, name_ref, offset); | ||
435 | } | ||
436 | |||
437 | // Otherwise, see if this is a declaration. We can use heuristics to | ||
438 | // suggest declaration names, see `CompletionKind::Magic`. | ||
439 | if let Some(name) = find_node_at_offset::<ast::Name>(&file_with_fake_ident, offset) { | ||
440 | if let Some(bind_pat) = name.syntax().ancestors().find_map(ast::IdentPat::cast) { | ||
441 | self.is_pat_binding_or_const = true; | ||
442 | if bind_pat.at_token().is_some() | ||
443 | || bind_pat.ref_token().is_some() | ||
444 | || bind_pat.mut_token().is_some() | ||
445 | { | ||
446 | self.is_pat_binding_or_const = false; | ||
447 | } | ||
448 | if bind_pat.syntax().parent().and_then(ast::RecordPatFieldList::cast).is_some() { | ||
449 | self.is_pat_binding_or_const = false; | ||
450 | } | ||
451 | if let Some(Some(pat)) = bind_pat.syntax().ancestors().find_map(|node| { | ||
452 | match_ast! { | ||
453 | match node { | ||
454 | ast::LetStmt(it) => Some(it.pat()), | ||
455 | ast::Param(it) => Some(it.pat()), | ||
456 | _ => None, | ||
457 | } | ||
458 | } | ||
459 | }) { | ||
460 | if pat.syntax().text_range().contains_range(bind_pat.syntax().text_range()) { | ||
461 | self.is_pat_binding_or_const = false; | ||
462 | self.is_irrefutable_pat_binding = true; | ||
463 | } | ||
464 | } | ||
465 | |||
466 | self.fill_impl_def(); | ||
467 | } | 469 | } |
468 | if is_node::<ast::Param>(name.syntax()) { | 470 | ast::NameLike::NameRef(name_ref) => { |
469 | self.is_param = true; | 471 | self.classify_name_ref(original_file, name_ref, offset); |
470 | return; | ||
471 | } | 472 | } |
472 | // FIXME: remove this (^) duplication and make the check more precise | 473 | ast::NameLike::Name(name) => { |
473 | if name.syntax().ancestors().find_map(ast::RecordPatFieldList::cast).is_some() { | 474 | self.classify_name(original_file, name, offset); |
474 | self.record_pat_syntax = | ||
475 | self.sema.find_node_at_offset_with_macros(&original_file, offset); | ||
476 | } | 475 | } |
477 | } | 476 | } |
478 | } | 477 | } |
@@ -506,22 +505,71 @@ impl<'a> CompletionContext<'a> { | |||
506 | } | 505 | } |
507 | } | 506 | } |
508 | 507 | ||
508 | fn classify_name(&mut self, original_file: &SyntaxNode, name: ast::Name, offset: TextSize) { | ||
509 | if let Some(bind_pat) = name.syntax().parent().and_then(ast::IdentPat::cast) { | ||
510 | self.is_pat_or_const = Some(PatternRefutability::Refutable); | ||
511 | // if any of these is here our bind pat can't be a const pat anymore | ||
512 | let complex_ident_pat = bind_pat.at_token().is_some() | ||
513 | || bind_pat.ref_token().is_some() | ||
514 | || bind_pat.mut_token().is_some(); | ||
515 | if complex_ident_pat { | ||
516 | self.is_pat_or_const = None; | ||
517 | } else { | ||
518 | let irrefutable_pat = bind_pat.syntax().ancestors().find_map(|node| { | ||
519 | match_ast! { | ||
520 | match node { | ||
521 | ast::LetStmt(it) => Some(it.pat()), | ||
522 | ast::Param(it) => Some(it.pat()), | ||
523 | _ => None, | ||
524 | } | ||
525 | } | ||
526 | }); | ||
527 | if let Some(Some(pat)) = irrefutable_pat { | ||
528 | // This check is here since we could be inside a pattern in the initializer expression of the let statement. | ||
529 | if pat.syntax().text_range().contains_range(bind_pat.syntax().text_range()) { | ||
530 | self.is_pat_or_const = Some(PatternRefutability::Irrefutable); | ||
531 | } | ||
532 | } | ||
533 | |||
534 | let is_name_in_field_pat = bind_pat | ||
535 | .syntax() | ||
536 | .parent() | ||
537 | .and_then(ast::RecordPatField::cast) | ||
538 | .map_or(false, |pat_field| pat_field.name_ref().is_none()); | ||
539 | if is_name_in_field_pat { | ||
540 | self.is_pat_or_const = None; | ||
541 | } | ||
542 | } | ||
543 | |||
544 | self.fill_impl_def(); | ||
545 | } | ||
546 | self.is_param |= is_node::<ast::Param>(name.syntax()); | ||
547 | if ast::RecordPatField::for_field_name(&name).is_some() { | ||
548 | self.record_pat_syntax = | ||
549 | self.sema.find_node_at_offset_with_macros(&original_file, offset); | ||
550 | } | ||
551 | } | ||
552 | |||
509 | fn classify_name_ref( | 553 | fn classify_name_ref( |
510 | &mut self, | 554 | &mut self, |
511 | original_file: &SyntaxNode, | 555 | original_file: &SyntaxNode, |
512 | name_ref: ast::NameRef, | 556 | name_ref: ast::NameRef, |
513 | offset: TextSize, | 557 | offset: TextSize, |
514 | ) { | 558 | ) { |
515 | self.name_ref_syntax = | 559 | self.fill_impl_def(); |
516 | find_node_at_offset(original_file, name_ref.syntax().text_range().start()); | ||
517 | let name_range = name_ref.syntax().text_range(); | ||
518 | if ast::RecordExprField::for_field_name(&name_ref).is_some() { | 560 | if ast::RecordExprField::for_field_name(&name_ref).is_some() { |
519 | self.record_lit_syntax = | 561 | self.record_lit_syntax = |
520 | self.sema.find_node_at_offset_with_macros(original_file, offset); | 562 | self.sema.find_node_at_offset_with_macros(original_file, offset); |
521 | } | 563 | } |
564 | if ast::RecordPatField::for_field_name_ref(&name_ref).is_some() { | ||
565 | self.record_pat_syntax = | ||
566 | self.sema.find_node_at_offset_with_macros(&original_file, offset); | ||
567 | } | ||
522 | 568 | ||
523 | self.fill_impl_def(); | 569 | self.name_ref_syntax = |
570 | find_node_at_offset(original_file, name_ref.syntax().text_range().start()); | ||
524 | 571 | ||
572 | let name_range = name_ref.syntax().text_range(); | ||
525 | let top_node = name_ref | 573 | let top_node = name_ref |
526 | .syntax() | 574 | .syntax() |
527 | .ancestors() | 575 | .ancestors() |
@@ -529,31 +577,20 @@ impl<'a> CompletionContext<'a> { | |||
529 | .last() | 577 | .last() |
530 | .unwrap(); | 578 | .unwrap(); |
531 | 579 | ||
532 | match top_node.parent().map(|it| it.kind()) { | 580 | if matches!(top_node.parent().map(|it| it.kind()), Some(SOURCE_FILE) | Some(ITEM_LIST)) { |
533 | Some(SOURCE_FILE) | Some(ITEM_LIST) => { | 581 | self.is_new_item = true; |
534 | self.is_new_item = true; | 582 | return; |
535 | return; | ||
536 | } | ||
537 | _ => (), | ||
538 | } | 583 | } |
539 | 584 | ||
540 | self.use_item_syntax = | 585 | self.use_item_syntax = |
541 | self.sema.token_ancestors_with_macros(self.token.clone()).find_map(ast::Use::cast); | 586 | self.sema.token_ancestors_with_macros(self.token.clone()).find_map(ast::Use::cast); |
542 | 587 | ||
543 | self.function_syntax = self | 588 | self.function_def = self |
544 | .sema | 589 | .sema |
545 | .token_ancestors_with_macros(self.token.clone()) | 590 | .token_ancestors_with_macros(self.token.clone()) |
546 | .take_while(|it| it.kind() != SOURCE_FILE && it.kind() != MODULE) | 591 | .take_while(|it| it.kind() != SOURCE_FILE && it.kind() != MODULE) |
547 | .find_map(ast::Fn::cast); | 592 | .find_map(ast::Fn::cast); |
548 | 593 | ||
549 | self.record_field_syntax = self | ||
550 | .sema | ||
551 | .token_ancestors_with_macros(self.token.clone()) | ||
552 | .take_while(|it| { | ||
553 | it.kind() != SOURCE_FILE && it.kind() != MODULE && it.kind() != CALL_EXPR | ||
554 | }) | ||
555 | .find_map(ast::RecordExprField::cast); | ||
556 | |||
557 | let parent = match name_ref.syntax().parent() { | 594 | let parent = match name_ref.syntax().parent() { |
558 | Some(it) => it, | 595 | Some(it) => it, |
559 | None => return, | 596 | None => return, |
@@ -614,18 +651,8 @@ impl<'a> CompletionContext<'a> { | |||
614 | }) | 651 | }) |
615 | .unwrap_or(false); | 652 | .unwrap_or(false); |
616 | self.is_expr = path.syntax().parent().and_then(ast::PathExpr::cast).is_some(); | 653 | self.is_expr = path.syntax().parent().and_then(ast::PathExpr::cast).is_some(); |
617 | |||
618 | if let Some(off) = name_ref.syntax().text_range().start().checked_sub(2.into()) { | ||
619 | if let Some(if_expr) = | ||
620 | self.sema.find_node_at_offset_with_macros::<ast::IfExpr>(original_file, off) | ||
621 | { | ||
622 | if if_expr.syntax().text_range().end() < name_ref.syntax().text_range().start() | ||
623 | { | ||
624 | self.after_if = true; | ||
625 | } | ||
626 | } | ||
627 | } | ||
628 | } | 654 | } |
655 | |||
629 | if let Some(field_expr) = ast::FieldExpr::cast(parent.clone()) { | 656 | if let Some(field_expr) = ast::FieldExpr::cast(parent.clone()) { |
630 | // The receiver comes before the point of insertion of the fake | 657 | // The receiver comes before the point of insertion of the fake |
631 | // ident, so it should have the same range in the non-modified file | 658 | // ident, so it should have the same range in the non-modified file |
@@ -643,6 +670,7 @@ impl<'a> CompletionContext<'a> { | |||
643 | false | 670 | false |
644 | }; | 671 | }; |
645 | } | 672 | } |
673 | |||
646 | if let Some(method_call_expr) = ast::MethodCallExpr::cast(parent) { | 674 | if let Some(method_call_expr) = ast::MethodCallExpr::cast(parent) { |
647 | // As above | 675 | // As above |
648 | self.dot_receiver = method_call_expr | 676 | self.dot_receiver = method_call_expr |