diff options
Diffstat (limited to 'crates')
-rw-r--r-- | crates/ide/src/syntax_highlighting/inject.rs | 2 | ||||
-rw-r--r-- | crates/ide_completion/src/completions.rs | 29 | ||||
-rw-r--r-- | crates/ide_completion/src/completions/lifetime.rs | 181 | ||||
-rw-r--r-- | crates/ide_completion/src/completions/pattern.rs | 2 | ||||
-rw-r--r-- | crates/ide_completion/src/context.rs | 72 | ||||
-rw-r--r-- | crates/ide_completion/src/lib.rs | 1 | ||||
-rw-r--r-- | crates/ide_completion/src/render.rs | 5 | ||||
-rw-r--r-- | crates/ide_db/src/call_info.rs | 14 | ||||
-rw-r--r-- | crates/syntax/src/ast/node_ext.rs | 9 |
9 files changed, 277 insertions, 38 deletions
diff --git a/crates/ide/src/syntax_highlighting/inject.rs b/crates/ide/src/syntax_highlighting/inject.rs index 00493a6b5..8e0940184 100644 --- a/crates/ide/src/syntax_highlighting/inject.rs +++ b/crates/ide/src/syntax_highlighting/inject.rs | |||
@@ -23,7 +23,7 @@ pub(super) fn ra_fixture( | |||
23 | expanded: SyntaxToken, | 23 | expanded: SyntaxToken, |
24 | ) -> Option<()> { | 24 | ) -> Option<()> { |
25 | let active_parameter = ActiveParameter::at_token(&sema, expanded)?; | 25 | let active_parameter = ActiveParameter::at_token(&sema, expanded)?; |
26 | if !active_parameter.name.starts_with("ra_fixture") { | 26 | if !active_parameter.ident().map_or(false, |name| name.text().starts_with("ra_fixture")) { |
27 | return None; | 27 | return None; |
28 | } | 28 | } |
29 | let value = literal.value()?; | 29 | let value = literal.value()?; |
diff --git a/crates/ide_completion/src/completions.rs b/crates/ide_completion/src/completions.rs index 09882c4f3..cdac4e41a 100644 --- a/crates/ide_completion/src/completions.rs +++ b/crates/ide_completion/src/completions.rs | |||
@@ -2,25 +2,27 @@ | |||
2 | 2 | ||
3 | pub(crate) mod attribute; | 3 | pub(crate) mod attribute; |
4 | pub(crate) mod dot; | 4 | pub(crate) mod dot; |
5 | pub(crate) mod record; | 5 | pub(crate) mod flyimport; |
6 | pub(crate) mod pattern; | ||
7 | pub(crate) mod fn_param; | 6 | pub(crate) mod fn_param; |
8 | pub(crate) mod keyword; | 7 | pub(crate) mod keyword; |
9 | pub(crate) mod snippet; | 8 | pub(crate) mod lifetime; |
10 | pub(crate) mod qualified_path; | ||
11 | pub(crate) mod unqualified_path; | ||
12 | pub(crate) mod postfix; | ||
13 | pub(crate) mod macro_in_item_position; | 9 | pub(crate) mod macro_in_item_position; |
14 | pub(crate) mod trait_impl; | ||
15 | pub(crate) mod mod_; | 10 | pub(crate) mod mod_; |
16 | pub(crate) mod flyimport; | 11 | pub(crate) mod pattern; |
12 | pub(crate) mod postfix; | ||
13 | pub(crate) mod qualified_path; | ||
14 | pub(crate) mod record; | ||
15 | pub(crate) mod snippet; | ||
16 | pub(crate) mod trait_impl; | ||
17 | pub(crate) mod unqualified_path; | ||
17 | 18 | ||
18 | use std::iter; | 19 | use std::iter; |
19 | 20 | ||
20 | use hir::{known, ModPath, ScopeDef, Type}; | 21 | use hir::{known, ModPath, ScopeDef, Type}; |
22 | use ide_db::SymbolKind; | ||
21 | 23 | ||
22 | use crate::{ | 24 | use crate::{ |
23 | item::Builder, | 25 | item::{Builder, CompletionKind}, |
24 | render::{ | 26 | render::{ |
25 | const_::render_const, | 27 | const_::render_const, |
26 | enum_variant::render_variant, | 28 | enum_variant::render_variant, |
@@ -31,7 +33,7 @@ use crate::{ | |||
31 | type_alias::render_type_alias, | 33 | type_alias::render_type_alias, |
32 | RenderContext, | 34 | RenderContext, |
33 | }, | 35 | }, |
34 | CompletionContext, CompletionItem, | 36 | CompletionContext, CompletionItem, CompletionItemKind, |
35 | }; | 37 | }; |
36 | 38 | ||
37 | /// Represents an in-progress set of completions being built. | 39 | /// Represents an in-progress set of completions being built. |
@@ -77,6 +79,13 @@ impl Completions { | |||
77 | self.add(item); | 79 | self.add(item); |
78 | } | 80 | } |
79 | 81 | ||
82 | pub(crate) fn add_static_lifetime(&mut self, ctx: &CompletionContext) { | ||
83 | let mut item = | ||
84 | CompletionItem::new(CompletionKind::Reference, ctx.source_range(), "'static"); | ||
85 | item.kind(CompletionItemKind::SymbolKind(SymbolKind::LifetimeParam)); | ||
86 | self.add(item.build()); | ||
87 | } | ||
88 | |||
80 | pub(crate) fn add_resolution( | 89 | pub(crate) fn add_resolution( |
81 | &mut self, | 90 | &mut self, |
82 | ctx: &CompletionContext, | 91 | ctx: &CompletionContext, |
diff --git a/crates/ide_completion/src/completions/lifetime.rs b/crates/ide_completion/src/completions/lifetime.rs new file mode 100644 index 000000000..74eb23360 --- /dev/null +++ b/crates/ide_completion/src/completions/lifetime.rs | |||
@@ -0,0 +1,181 @@ | |||
1 | //! Completes lifetimes. | ||
2 | use hir::ScopeDef; | ||
3 | |||
4 | use crate::{completions::Completions, context::CompletionContext}; | ||
5 | |||
6 | /// Completes lifetimes. | ||
7 | pub(crate) fn complete_lifetime(acc: &mut Completions, ctx: &CompletionContext) { | ||
8 | if !ctx.lifetime_allowed { | ||
9 | return; | ||
10 | } | ||
11 | let param_lifetime = match ( | ||
12 | &ctx.lifetime_syntax, | ||
13 | ctx.lifetime_param_syntax.as_ref().and_then(|lp| lp.lifetime()), | ||
14 | ) { | ||
15 | (Some(lt), Some(lp)) if lp == lt.clone() => return, | ||
16 | (Some(_), Some(lp)) => Some(lp.to_string()), | ||
17 | _ => None, | ||
18 | }; | ||
19 | |||
20 | ctx.scope.process_all_names(&mut |name, res| { | ||
21 | if let ScopeDef::GenericParam(hir::GenericParam::LifetimeParam(_)) = res { | ||
22 | if param_lifetime != Some(name.to_string()) { | ||
23 | acc.add_resolution(ctx, name.to_string(), &res); | ||
24 | } | ||
25 | } | ||
26 | }); | ||
27 | if param_lifetime.is_none() { | ||
28 | acc.add_static_lifetime(ctx); | ||
29 | } | ||
30 | } | ||
31 | |||
32 | #[cfg(test)] | ||
33 | mod tests { | ||
34 | use expect_test::{expect, Expect}; | ||
35 | |||
36 | use crate::{ | ||
37 | test_utils::{check_edit, completion_list_with_config, TEST_CONFIG}, | ||
38 | CompletionConfig, CompletionKind, | ||
39 | }; | ||
40 | |||
41 | fn check(ra_fixture: &str, expect: Expect) { | ||
42 | check_with_config(TEST_CONFIG, ra_fixture, expect); | ||
43 | } | ||
44 | |||
45 | fn check_with_config(config: CompletionConfig, ra_fixture: &str, expect: Expect) { | ||
46 | let actual = completion_list_with_config(config, ra_fixture, CompletionKind::Reference); | ||
47 | expect.assert_eq(&actual) | ||
48 | } | ||
49 | |||
50 | #[test] | ||
51 | fn check_lifetime_edit() { | ||
52 | check_edit( | ||
53 | "'lifetime", | ||
54 | r#" | ||
55 | fn func<'lifetime>(foo: &'li$0) {} | ||
56 | "#, | ||
57 | r#" | ||
58 | fn func<'lifetime>(foo: &'lifetime) {} | ||
59 | "#, | ||
60 | ); | ||
61 | } | ||
62 | |||
63 | #[test] | ||
64 | fn complete_lifetime_in_ref() { | ||
65 | check( | ||
66 | r#" | ||
67 | fn foo<'lifetime>(foo: &'a$0 usize) {} | ||
68 | "#, | ||
69 | expect![[r#" | ||
70 | lt 'lifetime | ||
71 | lt 'static | ||
72 | "#]], | ||
73 | ); | ||
74 | } | ||
75 | |||
76 | #[test] | ||
77 | fn complete_lifetime_in_ref_missing_ty() { | ||
78 | check( | ||
79 | r#" | ||
80 | fn foo<'lifetime>(foo: &'a$0) {} | ||
81 | "#, | ||
82 | expect![[r#" | ||
83 | lt 'lifetime | ||
84 | lt 'static | ||
85 | "#]], | ||
86 | ); | ||
87 | } | ||
88 | #[test] | ||
89 | fn complete_lifetime_in_self_ref() { | ||
90 | check( | ||
91 | r#" | ||
92 | struct Foo; | ||
93 | impl<'impl> Foo { | ||
94 | fn foo<'func>(&'a$0 self) {} | ||
95 | } | ||
96 | "#, | ||
97 | expect![[r#" | ||
98 | lt 'func | ||
99 | lt 'impl | ||
100 | lt 'static | ||
101 | "#]], | ||
102 | ); | ||
103 | } | ||
104 | |||
105 | #[test] | ||
106 | fn complete_lifetime_in_arg_list() { | ||
107 | check( | ||
108 | r#" | ||
109 | struct Foo<'lt>; | ||
110 | fn foo<'lifetime>(_: Foo<'a$0>) {} | ||
111 | "#, | ||
112 | expect![[r#" | ||
113 | lt 'lifetime | ||
114 | lt 'static | ||
115 | "#]], | ||
116 | ); | ||
117 | } | ||
118 | |||
119 | #[test] | ||
120 | fn complete_lifetime_in_where_pred() { | ||
121 | check( | ||
122 | r#" | ||
123 | fn foo2<'lifetime, T>() where 'a$0 {} | ||
124 | "#, | ||
125 | expect![[r#" | ||
126 | lt 'lifetime | ||
127 | lt 'static | ||
128 | "#]], | ||
129 | ); | ||
130 | } | ||
131 | |||
132 | #[test] | ||
133 | fn complete_lifetime_in_ty_bound() { | ||
134 | check( | ||
135 | r#" | ||
136 | fn foo2<'lifetime, T>() where T: 'a$0 {} | ||
137 | "#, | ||
138 | expect![[r#" | ||
139 | lt 'lifetime | ||
140 | lt 'static | ||
141 | "#]], | ||
142 | ); | ||
143 | check( | ||
144 | r#" | ||
145 | fn foo2<'lifetime, T>() where T: Trait<'a$0> {} | ||
146 | "#, | ||
147 | expect![[r#" | ||
148 | lt 'lifetime | ||
149 | lt 'static | ||
150 | "#]], | ||
151 | ); | ||
152 | } | ||
153 | |||
154 | #[test] | ||
155 | fn dont_complete_lifetime_in_assoc_ty_bound() { | ||
156 | check( | ||
157 | r#" | ||
158 | fn foo2<'lifetime, T>() where T: Trait<Item = 'a$0> {} | ||
159 | "#, | ||
160 | expect![[r#""#]], | ||
161 | ); | ||
162 | } | ||
163 | |||
164 | #[test] | ||
165 | fn complete_lifetime_in_param_list() { | ||
166 | check( | ||
167 | r#" | ||
168 | fn foo<'a$0>() {} | ||
169 | "#, | ||
170 | expect![[r#""#]], | ||
171 | ); | ||
172 | check( | ||
173 | r#" | ||
174 | fn foo<'footime, 'lifetime: 'a$0>() {} | ||
175 | "#, | ||
176 | expect![[r#" | ||
177 | lt 'footime | ||
178 | "#]], | ||
179 | ); | ||
180 | } | ||
181 | } | ||
diff --git a/crates/ide_completion/src/completions/pattern.rs b/crates/ide_completion/src/completions/pattern.rs index 476eecff0..b06498e6d 100644 --- a/crates/ide_completion/src/completions/pattern.rs +++ b/crates/ide_completion/src/completions/pattern.rs | |||
@@ -1,4 +1,4 @@ | |||
1 | //! Completes constats and paths in patterns. | 1 | //! Completes constants and paths in patterns. |
2 | 2 | ||
3 | use crate::{CompletionContext, Completions}; | 3 | use crate::{CompletionContext, Completions}; |
4 | 4 | ||
diff --git a/crates/ide_completion/src/context.rs b/crates/ide_completion/src/context.rs index 6d57da06a..4c2b31084 100644 --- a/crates/ide_completion/src/context.rs +++ b/crates/ide_completion/src/context.rs | |||
@@ -4,8 +4,11 @@ use hir::{Local, ScopeDef, Semantics, SemanticsScope, Type}; | |||
4 | use ide_db::base_db::{FilePosition, SourceDatabase}; | 4 | use ide_db::base_db::{FilePosition, SourceDatabase}; |
5 | use ide_db::{call_info::ActiveParameter, RootDatabase}; | 5 | use ide_db::{call_info::ActiveParameter, RootDatabase}; |
6 | use syntax::{ | 6 | use syntax::{ |
7 | algo::find_node_at_offset, ast, match_ast, AstNode, NodeOrToken, SyntaxKind::*, SyntaxNode, | 7 | algo::find_node_at_offset, |
8 | SyntaxToken, TextRange, TextSize, | 8 | ast::{self, NameOrNameRef, NameOwner}, |
9 | match_ast, AstNode, NodeOrToken, | ||
10 | SyntaxKind::*, | ||
11 | SyntaxNode, SyntaxToken, TextRange, TextSize, | ||
9 | }; | 12 | }; |
10 | 13 | ||
11 | use text_edit::Indel; | 14 | use text_edit::Indel; |
@@ -35,15 +38,18 @@ pub(crate) struct CompletionContext<'a> { | |||
35 | /// The token before the cursor, in the macro-expanded file. | 38 | /// The token before the cursor, in the macro-expanded file. |
36 | pub(super) token: SyntaxToken, | 39 | pub(super) token: SyntaxToken, |
37 | pub(super) krate: Option<hir::Crate>, | 40 | pub(super) krate: Option<hir::Crate>, |
38 | pub(super) expected_name: Option<String>, | 41 | pub(super) expected_name: Option<NameOrNameRef>, |
39 | pub(super) expected_type: Option<Type>, | 42 | pub(super) expected_type: Option<Type>, |
40 | pub(super) name_ref_syntax: Option<ast::NameRef>, | 43 | pub(super) name_ref_syntax: Option<ast::NameRef>, |
44 | pub(super) lifetime_syntax: Option<ast::Lifetime>, | ||
45 | pub(super) lifetime_param_syntax: Option<ast::LifetimeParam>, | ||
41 | pub(super) function_syntax: Option<ast::Fn>, | 46 | pub(super) function_syntax: Option<ast::Fn>, |
42 | pub(super) use_item_syntax: Option<ast::Use>, | 47 | pub(super) use_item_syntax: Option<ast::Use>, |
43 | pub(super) record_lit_syntax: Option<ast::RecordExpr>, | 48 | pub(super) record_lit_syntax: Option<ast::RecordExpr>, |
44 | pub(super) record_pat_syntax: Option<ast::RecordPat>, | 49 | pub(super) record_pat_syntax: Option<ast::RecordPat>, |
45 | pub(super) record_field_syntax: Option<ast::RecordExprField>, | 50 | pub(super) record_field_syntax: Option<ast::RecordExprField>, |
46 | pub(super) impl_def: Option<ast::Impl>, | 51 | pub(super) impl_def: Option<ast::Impl>, |
52 | pub(super) lifetime_allowed: bool, | ||
47 | /// FIXME: `ActiveParameter` is string-based, which is very very wrong | 53 | /// FIXME: `ActiveParameter` is string-based, which is very very wrong |
48 | pub(super) active_parameter: Option<ActiveParameter>, | 54 | pub(super) active_parameter: Option<ActiveParameter>, |
49 | pub(super) is_param: bool, | 55 | pub(super) is_param: bool, |
@@ -136,9 +142,12 @@ impl<'a> CompletionContext<'a> { | |||
136 | original_token, | 142 | original_token, |
137 | token, | 143 | token, |
138 | krate, | 144 | krate, |
145 | lifetime_allowed: false, | ||
139 | expected_name: None, | 146 | expected_name: None, |
140 | expected_type: None, | 147 | expected_type: None, |
141 | name_ref_syntax: None, | 148 | name_ref_syntax: None, |
149 | lifetime_syntax: None, | ||
150 | lifetime_param_syntax: None, | ||
142 | function_syntax: None, | 151 | function_syntax: None, |
143 | use_item_syntax: None, | 152 | use_item_syntax: None, |
144 | record_lit_syntax: None, | 153 | record_lit_syntax: None, |
@@ -241,7 +250,7 @@ impl<'a> CompletionContext<'a> { | |||
241 | pub(crate) fn source_range(&self) -> TextRange { | 250 | pub(crate) fn source_range(&self) -> TextRange { |
242 | // check kind of macro-expanded token, but use range of original token | 251 | // check kind of macro-expanded token, but use range of original token |
243 | let kind = self.token.kind(); | 252 | let kind = self.token.kind(); |
244 | if kind == IDENT || kind == UNDERSCORE || kind.is_keyword() { | 253 | if kind == IDENT || kind == LIFETIME_IDENT || kind == UNDERSCORE || kind.is_keyword() { |
245 | cov_mark::hit!(completes_if_prefix_is_keyword); | 254 | cov_mark::hit!(completes_if_prefix_is_keyword); |
246 | self.original_token.text_range() | 255 | self.original_token.text_range() |
247 | } else { | 256 | } else { |
@@ -292,13 +301,13 @@ impl<'a> CompletionContext<'a> { | |||
292 | file_with_fake_ident: SyntaxNode, | 301 | file_with_fake_ident: SyntaxNode, |
293 | offset: TextSize, | 302 | offset: TextSize, |
294 | ) { | 303 | ) { |
295 | let expected = { | 304 | let (expected_type, expected_name) = { |
296 | let mut node = match self.token.parent() { | 305 | let mut node = match self.token.parent() { |
297 | Some(it) => it, | 306 | Some(it) => it, |
298 | None => return, | 307 | None => return, |
299 | }; | 308 | }; |
300 | loop { | 309 | loop { |
301 | let ret = match_ast! { | 310 | break match_ast! { |
302 | match node { | 311 | match node { |
303 | ast::LetStmt(it) => { | 312 | ast::LetStmt(it) => { |
304 | cov_mark::hit!(expected_type_let_with_leading_char); | 313 | cov_mark::hit!(expected_type_let_with_leading_char); |
@@ -306,7 +315,7 @@ impl<'a> CompletionContext<'a> { | |||
306 | let ty = it.pat() | 315 | let ty = it.pat() |
307 | .and_then(|pat| self.sema.type_of_pat(&pat)); | 316 | .and_then(|pat| self.sema.type_of_pat(&pat)); |
308 | let name = if let Some(ast::Pat::IdentPat(ident)) = it.pat() { | 317 | let name = if let Some(ast::Pat::IdentPat(ident)) = it.pat() { |
309 | Some(ident.syntax().text().to_string()) | 318 | ident.name().map(NameOrNameRef::Name) |
310 | } else { | 319 | } else { |
311 | None | 320 | None |
312 | }; | 321 | }; |
@@ -319,7 +328,10 @@ impl<'a> CompletionContext<'a> { | |||
319 | ActiveParameter::at_token( | 328 | ActiveParameter::at_token( |
320 | &self.sema, | 329 | &self.sema, |
321 | self.token.clone(), | 330 | self.token.clone(), |
322 | ).map(|ap| (Some(ap.ty), Some(ap.name))) | 331 | ).map(|ap| { |
332 | let name = ap.ident().map(NameOrNameRef::Name); | ||
333 | (Some(ap.ty), name) | ||
334 | }) | ||
323 | .unwrap_or((None, None)) | 335 | .unwrap_or((None, None)) |
324 | }, | 336 | }, |
325 | ast::RecordExprFieldList(_it) => { | 337 | ast::RecordExprFieldList(_it) => { |
@@ -327,10 +339,10 @@ impl<'a> CompletionContext<'a> { | |||
327 | self.token.prev_sibling_or_token() | 339 | self.token.prev_sibling_or_token() |
328 | .and_then(|se| se.into_node()) | 340 | .and_then(|se| se.into_node()) |
329 | .and_then(|node| ast::RecordExprField::cast(node)) | 341 | .and_then(|node| ast::RecordExprField::cast(node)) |
330 | .and_then(|rf| self.sema.resolve_record_field(&rf)) | 342 | .and_then(|rf| self.sema.resolve_record_field(&rf).zip(Some(rf))) |
331 | .map(|f|( | 343 | .map(|(f, rf)|( |
332 | Some(f.0.signature_ty(self.db)), | 344 | Some(f.0.signature_ty(self.db)), |
333 | Some(f.0.name(self.db).to_string()), | 345 | rf.field_name().map(NameOrNameRef::NameRef), |
334 | )) | 346 | )) |
335 | .unwrap_or((None, None)) | 347 | .unwrap_or((None, None)) |
336 | }, | 348 | }, |
@@ -340,7 +352,7 @@ impl<'a> CompletionContext<'a> { | |||
340 | .resolve_record_field(&it) | 352 | .resolve_record_field(&it) |
341 | .map(|f|( | 353 | .map(|f|( |
342 | Some(f.0.signature_ty(self.db)), | 354 | Some(f.0.signature_ty(self.db)), |
343 | Some(f.0.name(self.db).to_string()), | 355 | it.field_name().map(NameOrNameRef::NameRef), |
344 | )) | 356 | )) |
345 | .unwrap_or((None, None)) | 357 | .unwrap_or((None, None)) |
346 | }, | 358 | }, |
@@ -378,14 +390,17 @@ impl<'a> CompletionContext<'a> { | |||
378 | }, | 390 | }, |
379 | } | 391 | } |
380 | }; | 392 | }; |
381 | |||
382 | break ret; | ||
383 | } | 393 | } |
384 | }; | 394 | }; |
385 | self.expected_type = expected.0; | 395 | self.expected_type = expected_type; |
386 | self.expected_name = expected.1; | 396 | self.expected_name = expected_name; |
387 | self.attribute_under_caret = find_node_at_offset(&file_with_fake_ident, offset); | 397 | self.attribute_under_caret = find_node_at_offset(&file_with_fake_ident, offset); |
388 | 398 | ||
399 | if let Some(lifetime) = find_node_at_offset::<ast::Lifetime>(&file_with_fake_ident, offset) | ||
400 | { | ||
401 | self.classify_lifetime(original_file, lifetime, offset); | ||
402 | } | ||
403 | |||
389 | // First, let's try to complete a reference to some declaration. | 404 | // First, let's try to complete a reference to some declaration. |
390 | if let Some(name_ref) = find_node_at_offset::<ast::NameRef>(&file_with_fake_ident, offset) { | 405 | if let Some(name_ref) = find_node_at_offset::<ast::NameRef>(&file_with_fake_ident, offset) { |
391 | // Special case, `trait T { fn foo(i_am_a_name_ref) {} }`. | 406 | // Special case, `trait T { fn foo(i_am_a_name_ref) {} }`. |
@@ -445,6 +460,23 @@ impl<'a> CompletionContext<'a> { | |||
445 | } | 460 | } |
446 | } | 461 | } |
447 | 462 | ||
463 | fn classify_lifetime( | ||
464 | &mut self, | ||
465 | original_file: &SyntaxNode, | ||
466 | lifetime: ast::Lifetime, | ||
467 | offset: TextSize, | ||
468 | ) { | ||
469 | self.lifetime_syntax = | ||
470 | find_node_at_offset(original_file, lifetime.syntax().text_range().start()); | ||
471 | if lifetime.syntax().parent().map_or(false, |p| p.kind() != syntax::SyntaxKind::ERROR) { | ||
472 | self.lifetime_allowed = true; | ||
473 | } | ||
474 | if let Some(_) = lifetime.syntax().parent().and_then(ast::LifetimeParam::cast) { | ||
475 | self.lifetime_param_syntax = | ||
476 | self.sema.find_node_at_offset_with_macros(original_file, offset); | ||
477 | } | ||
478 | } | ||
479 | |||
448 | fn classify_name_ref( | 480 | fn classify_name_ref( |
449 | &mut self, | 481 | &mut self, |
450 | original_file: &SyntaxNode, | 482 | original_file: &SyntaxNode, |
@@ -452,11 +484,11 @@ impl<'a> CompletionContext<'a> { | |||
452 | offset: TextSize, | 484 | offset: TextSize, |
453 | ) { | 485 | ) { |
454 | self.name_ref_syntax = | 486 | self.name_ref_syntax = |
455 | find_node_at_offset(&original_file, name_ref.syntax().text_range().start()); | 487 | find_node_at_offset(original_file, name_ref.syntax().text_range().start()); |
456 | let name_range = name_ref.syntax().text_range(); | 488 | let name_range = name_ref.syntax().text_range(); |
457 | if ast::RecordExprField::for_field_name(&name_ref).is_some() { | 489 | if ast::RecordExprField::for_field_name(&name_ref).is_some() { |
458 | self.record_lit_syntax = | 490 | self.record_lit_syntax = |
459 | self.sema.find_node_at_offset_with_macros(&original_file, offset); | 491 | self.sema.find_node_at_offset_with_macros(original_file, offset); |
460 | } | 492 | } |
461 | 493 | ||
462 | self.fill_impl_def(); | 494 | self.fill_impl_def(); |
@@ -631,7 +663,9 @@ mod tests { | |||
631 | .map(|t| t.display_test(&db).to_string()) | 663 | .map(|t| t.display_test(&db).to_string()) |
632 | .unwrap_or("?".to_owned()); | 664 | .unwrap_or("?".to_owned()); |
633 | 665 | ||
634 | let name = completion_context.expected_name.unwrap_or("?".to_owned()); | 666 | let name = completion_context |
667 | .expected_name | ||
668 | .map_or_else(|| "?".to_owned(), |name| name.to_string()); | ||
635 | 669 | ||
636 | expect.assert_eq(&format!("ty: {}, name: {}", ty, name)); | 670 | expect.assert_eq(&format!("ty: {}, name: {}", ty, name)); |
637 | } | 671 | } |
diff --git a/crates/ide_completion/src/lib.rs b/crates/ide_completion/src/lib.rs index d9ea7b7ea..7a0eb6a96 100644 --- a/crates/ide_completion/src/lib.rs +++ b/crates/ide_completion/src/lib.rs | |||
@@ -130,6 +130,7 @@ pub fn completions( | |||
130 | completions::trait_impl::complete_trait_impl(&mut acc, &ctx); | 130 | completions::trait_impl::complete_trait_impl(&mut acc, &ctx); |
131 | completions::mod_::complete_mod(&mut acc, &ctx); | 131 | completions::mod_::complete_mod(&mut acc, &ctx); |
132 | completions::flyimport::import_on_the_fly(&mut acc, &ctx); | 132 | completions::flyimport::import_on_the_fly(&mut acc, &ctx); |
133 | completions::lifetime::complete_lifetime(&mut acc, &ctx); | ||
133 | 134 | ||
134 | Some(acc) | 135 | Some(acc) |
135 | } | 136 | } |
diff --git a/crates/ide_completion/src/render.rs b/crates/ide_completion/src/render.rs index 12921e12b..2b6e9ebd1 100644 --- a/crates/ide_completion/src/render.rs +++ b/crates/ide_completion/src/render.rs | |||
@@ -243,7 +243,7 @@ impl<'a> Render<'a> { | |||
243 | 243 | ||
244 | item.set_relevance(CompletionRelevance { | 244 | item.set_relevance(CompletionRelevance { |
245 | exact_type_match: compute_exact_type_match(self.ctx.completion, &ty), | 245 | exact_type_match: compute_exact_type_match(self.ctx.completion, &ty), |
246 | exact_name_match: compute_exact_name_match(self.ctx.completion, local_name.clone()), | 246 | exact_name_match: compute_exact_name_match(self.ctx.completion, &local_name), |
247 | is_local: true, | 247 | is_local: true, |
248 | ..CompletionRelevance::default() | 248 | ..CompletionRelevance::default() |
249 | }); | 249 | }); |
@@ -319,8 +319,7 @@ fn compute_exact_type_match(ctx: &CompletionContext, completion_ty: &hir::Type) | |||
319 | 319 | ||
320 | fn compute_exact_name_match(ctx: &CompletionContext, completion_name: impl Into<String>) -> bool { | 320 | fn compute_exact_name_match(ctx: &CompletionContext, completion_name: impl Into<String>) -> bool { |
321 | let completion_name = completion_name.into(); | 321 | let completion_name = completion_name.into(); |
322 | 322 | ctx.expected_name.as_ref().map_or(false, |name| name.text() == completion_name) | |
323 | Some(&completion_name) == ctx.expected_name.as_ref() | ||
324 | } | 323 | } |
325 | 324 | ||
326 | fn compute_ref_match(ctx: &CompletionContext, completion_ty: &hir::Type) -> Option<Mutability> { | 325 | fn compute_ref_match(ctx: &CompletionContext, completion_ty: &hir::Type) -> Option<Mutability> { |
diff --git a/crates/ide_db/src/call_info.rs b/crates/ide_db/src/call_info.rs index 7e26c3ccf..e583a52f4 100644 --- a/crates/ide_db/src/call_info.rs +++ b/crates/ide_db/src/call_info.rs | |||
@@ -4,7 +4,7 @@ use either::Either; | |||
4 | use hir::{HasAttrs, HirDisplay, Semantics, Type}; | 4 | use hir::{HasAttrs, HirDisplay, Semantics, Type}; |
5 | use stdx::format_to; | 5 | use stdx::format_to; |
6 | use syntax::{ | 6 | use syntax::{ |
7 | ast::{self, ArgListOwner}, | 7 | ast::{self, ArgListOwner, NameOwner}, |
8 | match_ast, AstNode, SyntaxNode, SyntaxToken, TextRange, TextSize, | 8 | match_ast, AstNode, SyntaxNode, SyntaxToken, TextRange, TextSize, |
9 | }; | 9 | }; |
10 | 10 | ||
@@ -142,7 +142,7 @@ fn call_info_impl( | |||
142 | #[derive(Debug)] | 142 | #[derive(Debug)] |
143 | pub struct ActiveParameter { | 143 | pub struct ActiveParameter { |
144 | pub ty: Type, | 144 | pub ty: Type, |
145 | pub name: String, | 145 | pub pat: Either<ast::SelfParam, ast::Pat>, |
146 | } | 146 | } |
147 | 147 | ||
148 | impl ActiveParameter { | 148 | impl ActiveParameter { |
@@ -165,8 +165,14 @@ impl ActiveParameter { | |||
165 | return None; | 165 | return None; |
166 | } | 166 | } |
167 | let (pat, ty) = params.swap_remove(idx); | 167 | let (pat, ty) = params.swap_remove(idx); |
168 | let name = pat?.to_string(); | 168 | pat.map(|pat| ActiveParameter { ty, pat }) |
169 | Some(ActiveParameter { ty, name }) | 169 | } |
170 | |||
171 | pub fn ident(&self) -> Option<ast::Name> { | ||
172 | self.pat.as_ref().right().and_then(|param| match param { | ||
173 | ast::Pat::IdentPat(ident) => ident.name(), | ||
174 | _ => None, | ||
175 | }) | ||
170 | } | 176 | } |
171 | } | 177 | } |
172 | 178 | ||
diff --git a/crates/syntax/src/ast/node_ext.rs b/crates/syntax/src/ast/node_ext.rs index 01f580a40..42a7b9c2a 100644 --- a/crates/syntax/src/ast/node_ext.rs +++ b/crates/syntax/src/ast/node_ext.rs | |||
@@ -380,6 +380,15 @@ impl fmt::Display for NameOrNameRef { | |||
380 | } | 380 | } |
381 | } | 381 | } |
382 | 382 | ||
383 | impl NameOrNameRef { | ||
384 | pub fn text(&self) -> &str { | ||
385 | match self { | ||
386 | NameOrNameRef::Name(name) => name.text(), | ||
387 | NameOrNameRef::NameRef(name_ref) => name_ref.text(), | ||
388 | } | ||
389 | } | ||
390 | } | ||
391 | |||
383 | impl ast::RecordPatField { | 392 | impl ast::RecordPatField { |
384 | pub fn for_field_name_ref(field_name: &ast::NameRef) -> Option<ast::RecordPatField> { | 393 | pub fn for_field_name_ref(field_name: &ast::NameRef) -> Option<ast::RecordPatField> { |
385 | let candidate = field_name.syntax().parent().and_then(ast::RecordPatField::cast)?; | 394 | let candidate = field_name.syntax().parent().and_then(ast::RecordPatField::cast)?; |