Skip to content

Commit 877cfba

Browse files
committed
Started to refactor the trigger of the trait_impl completion.
1 parent 6f130e7 commit 877cfba

File tree

1 file changed

+77
-33
lines changed

1 file changed

+77
-33
lines changed

crates/ra_ide/src/completion/complete_trait_impl.rs

Lines changed: 77 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -16,19 +16,63 @@ use ra_syntax::{
1616
use ra_assists::utils::get_missing_impl_items;
1717

1818
pub(crate) fn complete_trait_impl(acc: &mut Completions, ctx: &CompletionContext) {
19-
// it is possible to have a parent `fn` and `impl` block. Ignore completion
20-
// attempts from within a `fn` block.
21-
if ctx.function_syntax.is_some() {
22-
return;
23-
}
19+
let trigger = ctx.token
20+
.ancestors()
21+
.find(|p| match p.kind() {
22+
SyntaxKind::FN_DEF |
23+
SyntaxKind::TYPE_ALIAS_DEF |
24+
SyntaxKind::CONST_DEF |
25+
SyntaxKind::ITEM_LIST => true,
26+
_ => false
27+
});
28+
29+
let impl_block = trigger
30+
.as_ref()
31+
.and_then(|node| node.parent())
32+
.and_then(|node| node.parent())
33+
.and_then(|node| ast::ImplBlock::cast(node));
34+
35+
if let (Some(trigger), Some(impl_block)) = (trigger, impl_block) {
36+
match trigger.kind() {
37+
SyntaxKind::FN_DEF => {
38+
for missing_fn in get_missing_impl_items(ctx.db, &ctx.analyzer, &impl_block)
39+
.iter()
40+
.filter_map(|item| {
41+
match item {
42+
hir::AssocItem::Function(fn_item) => Some(fn_item),
43+
_ => None
44+
}
45+
})
46+
{
47+
add_function_impl(acc, ctx, &missing_fn);
48+
}
49+
},
2450

25-
if let Some(ref impl_block) = ctx.impl_block {
26-
for item in get_missing_impl_items(ctx.db, &ctx.analyzer, impl_block) {
27-
match item {
28-
hir::AssocItem::Function(f) => add_function_impl(acc, ctx, &f),
29-
hir::AssocItem::TypeAlias(t) => add_type_alias_impl(acc, ctx, &t),
30-
hir::AssocItem::Const(c) => add_const_impl(acc, ctx, &c),
31-
}
51+
SyntaxKind::TYPE_ALIAS_DEF => {
52+
for missing_fn in get_missing_impl_items(ctx.db, &ctx.analyzer, &impl_block)
53+
.iter()
54+
.filter_map(|item| match item {
55+
hir::AssocItem::TypeAlias(type_item) => Some(type_item),
56+
_ => None
57+
})
58+
{
59+
add_type_alias_impl(acc, ctx, &missing_fn);
60+
}
61+
},
62+
63+
SyntaxKind::CONST_DEF => {
64+
for missing_fn in get_missing_impl_items(ctx.db, &ctx.analyzer, &impl_block)
65+
.iter()
66+
.filter_map(|item| match item {
67+
hir::AssocItem::Const(const_item) => Some(const_item),
68+
_ => None
69+
})
70+
{
71+
add_const_impl(acc, ctx, &missing_fn);
72+
}
73+
},
74+
75+
_ => {}
3276
}
3377
}
3478
}
@@ -126,16 +170,16 @@ mod tests {
126170
struct T1;
127171
128172
impl Test for T1 {
129-
<|>
173+
fn<|>
130174
}
131175
",
132176
);
133177
assert_debug_snapshot!(completions, @r###"
134178
[
135179
CompletionItem {
136180
label: "fn foo()",
137-
source_range: [138; 138),
138-
delete: [138; 138),
181+
source_range: [140; 140),
182+
delete: [140; 140),
139183
insert: "fn foo() {}",
140184
kind: Function,
141185
},
@@ -157,16 +201,16 @@ mod tests {
157201
impl Test for T1 {
158202
fn foo() {}
159203
160-
<|>
204+
fn<|>
161205
}
162206
",
163207
);
164208
assert_debug_snapshot!(completions, @r###"
165209
[
166210
CompletionItem {
167211
label: "fn bar()",
168-
source_range: [193; 193),
169-
delete: [193; 193),
212+
source_range: [195; 195),
213+
delete: [195; 195),
170214
insert: "fn bar() {}",
171215
kind: Function,
172216
},
@@ -185,16 +229,16 @@ mod tests {
185229
struct T1;
186230
187231
impl Test for T1 {
188-
<|>
232+
fn<|>
189233
}
190234
",
191235
);
192236
assert_debug_snapshot!(completions, @r###"
193237
[
194238
CompletionItem {
195239
label: "fn foo()",
196-
source_range: [141; 141),
197-
delete: [141; 141),
240+
source_range: [143; 143),
241+
delete: [143; 143),
198242
insert: "fn foo<T>() {}",
199243
kind: Function,
200244
},
@@ -213,16 +257,16 @@ mod tests {
213257
struct T1;
214258
215259
impl Test for T1 {
216-
<|>
260+
fn<|>
217261
}
218262
",
219263
);
220264
assert_debug_snapshot!(completions, @r###"
221265
[
222266
CompletionItem {
223267
label: "fn foo()",
224-
source_range: [163; 163),
225-
delete: [163; 163),
268+
source_range: [165; 165),
269+
delete: [165; 165),
226270
insert: "fn foo<T>()\nwhere T: Into<String> {}",
227271
kind: Function,
228272
},
@@ -239,16 +283,16 @@ mod tests {
239283
}
240284
241285
impl Test for () {
242-
<|>
286+
type<|>
243287
}
244288
",
245289
);
246290
assert_debug_snapshot!(completions, @r###"
247291
[
248292
CompletionItem {
249293
label: "type SomeType = ",
250-
source_range: [119; 119),
251-
delete: [119; 119),
294+
source_range: [123; 123),
295+
delete: [123; 123),
252296
insert: "type SomeType = ",
253297
kind: TypeAlias,
254298
},
@@ -265,16 +309,16 @@ mod tests {
265309
}
266310
267311
impl Test for () {
268-
<|>
312+
const<|>
269313
}
270314
",
271315
);
272316
assert_debug_snapshot!(completions, @r###"
273317
[
274318
CompletionItem {
275319
label: "const SOME_CONST: u16 = ",
276-
source_range: [127; 127),
277-
delete: [127; 127),
320+
source_range: [132; 132),
321+
delete: [132; 132),
278322
insert: "const SOME_CONST: u16 = ",
279323
kind: Const,
280324
},
@@ -291,16 +335,16 @@ mod tests {
291335
}
292336
293337
impl Test for () {
294-
<|>
338+
const<|>
295339
}
296340
",
297341
);
298342
assert_debug_snapshot!(completions, @r###"
299343
[
300344
CompletionItem {
301345
label: "const SOME_CONST: u16 = ",
302-
source_range: [132; 132),
303-
delete: [132; 132),
346+
source_range: [137; 137),
347+
delete: [137; 137),
304348
insert: "const SOME_CONST: u16 = ",
305349
kind: Const,
306350
},

0 commit comments

Comments
 (0)