Skip to content

Commit be97cbf

Browse files
committed
Adjusted the completion lookups to filter by just the name.
1 parent 0bc9e62 commit be97cbf

File tree

1 file changed

+44
-29
lines changed

1 file changed

+44
-29
lines changed

crates/ra_ide/src/completion/complete_trait_impl.rs

Lines changed: 44 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -79,16 +79,16 @@ fn add_function_impl(
7979
) {
8080
let display = FunctionSignature::from_hir(ctx.db, func.clone());
8181

82-
let func_name = func.name(ctx.db);
82+
let fn_name = func.name(ctx.db).to_string();
8383

8484
let label = if func.params(ctx.db).len() > 0 {
85-
format!("fn {}(..)", func_name.to_string())
85+
format!("fn {}(..)", fn_name)
8686
} else {
87-
format!("fn {}()", func_name.to_string())
87+
format!("fn {}()", fn_name)
8888
};
8989

9090
let builder = CompletionItem::new(CompletionKind::Magic, ctx.source_range(), label.clone())
91-
.lookup_by(label)
91+
.lookup_by(fn_name)
9292
.set_documentation(func.docs(ctx.db));
9393

9494
let completion_kind = if func.has_self_param(ctx.db) {
@@ -111,10 +111,13 @@ fn add_type_alias_impl(
111111
ctx: &CompletionContext,
112112
type_alias: &hir::TypeAlias,
113113
) {
114-
let snippet = format!("type {} = ", type_alias.name(ctx.db).to_string());
114+
let alias_name = type_alias.name(ctx.db).to_string();
115+
116+
let snippet = format!("type {} = ", alias_name);
115117

116118
CompletionItem::new(CompletionKind::Magic, ctx.source_range(), snippet.clone())
117119
.text_edit(TextEdit::replace(type_def_node.text_range(), snippet))
120+
.lookup_by(alias_name)
118121
.kind(CompletionItemKind::TypeAlias)
119122
.set_documentation(type_alias.docs(ctx.db))
120123
.add_to(acc);
@@ -126,13 +129,18 @@ fn add_const_impl(
126129
ctx: &CompletionContext,
127130
const_: &hir::Const,
128131
) {
129-
let snippet = make_const_compl_syntax(&const_.source(ctx.db).value);
132+
let const_name = const_.name(ctx.db).map(|n| n.to_string());
130133

131-
CompletionItem::new(CompletionKind::Magic, ctx.source_range(), snippet.clone())
132-
.text_edit(TextEdit::replace(const_def_node.text_range(), snippet))
133-
.kind(CompletionItemKind::Const)
134-
.set_documentation(const_.docs(ctx.db))
135-
.add_to(acc);
134+
if let Some(const_name) = const_name {
135+
let snippet = make_const_compl_syntax(&const_.source(ctx.db).value);
136+
137+
CompletionItem::new(CompletionKind::Magic, ctx.source_range(), snippet.clone())
138+
.text_edit(TextEdit::replace(const_def_node.text_range(), snippet))
139+
.lookup_by(const_name)
140+
.kind(CompletionItemKind::Const)
141+
.set_documentation(const_.docs(ctx.db))
142+
.add_to(acc);
143+
}
136144
}
137145

138146
fn make_const_compl_syntax(const_: &ast::ConstDef) -> String {
@@ -178,18 +186,19 @@ mod tests {
178186
struct T1;
179187
180188
impl Test for T1 {
181-
fn<|>
189+
fn f<|>
182190
}
183191
",
184192
);
185193
assert_debug_snapshot!(completions, @r###"
186194
[
187195
CompletionItem {
188196
label: "fn foo()",
189-
source_range: [140; 140),
190-
delete: [138; 140),
197+
source_range: [141; 142),
198+
delete: [138; 142),
191199
insert: "fn foo() {}",
192200
kind: Function,
201+
lookup: "foo",
193202
},
194203
]
195204
"###);
@@ -201,26 +210,27 @@ mod tests {
201210
r"
202211
trait Test {
203212
fn foo();
204-
fn bar();
213+
fn foo_bar();
205214
}
206215
207216
struct T1;
208217
209218
impl Test for T1 {
210219
fn foo() {}
211220
212-
fn<|>
221+
fn f<|>
213222
}
214223
",
215224
);
216225
assert_debug_snapshot!(completions, @r###"
217226
[
218227
CompletionItem {
219-
label: "fn bar()",
220-
source_range: [195; 195),
221-
delete: [193; 195),
222-
insert: "fn bar() {}",
228+
label: "fn foo_bar()",
229+
source_range: [200; 201),
230+
delete: [197; 201),
231+
insert: "fn foo_bar() {}",
223232
kind: Function,
233+
lookup: "foo_bar",
224234
},
225235
]
226236
"###);
@@ -237,18 +247,19 @@ mod tests {
237247
struct T1;
238248
239249
impl Test for T1 {
240-
fn<|>
250+
fn f<|>
241251
}
242252
",
243253
);
244254
assert_debug_snapshot!(completions, @r###"
245255
[
246256
CompletionItem {
247257
label: "fn foo()",
248-
source_range: [143; 143),
249-
delete: [141; 143),
258+
source_range: [144; 145),
259+
delete: [141; 145),
250260
insert: "fn foo<T>() {}",
251261
kind: Function,
262+
lookup: "foo",
252263
},
253264
]
254265
"###);
@@ -265,18 +276,19 @@ mod tests {
265276
struct T1;
266277
267278
impl Test for T1 {
268-
fn<|>
279+
fn f<|>
269280
}
270281
",
271282
);
272283
assert_debug_snapshot!(completions, @r###"
273284
[
274285
CompletionItem {
275286
label: "fn foo()",
276-
source_range: [165; 165),
277-
delete: [163; 165),
287+
source_range: [166; 167),
288+
delete: [163; 167),
278289
insert: "fn foo<T>()\nwhere T: Into<String> {}",
279290
kind: Function,
291+
lookup: "foo",
280292
},
281293
]
282294
"###);
@@ -291,18 +303,19 @@ mod tests {
291303
}
292304
293305
impl Test for () {
294-
type<|>
306+
type S<|>
295307
}
296308
",
297309
);
298310
assert_debug_snapshot!(completions, @r###"
299311
[
300312
CompletionItem {
301313
label: "type SomeType = ",
302-
source_range: [123; 123),
303-
delete: [119; 123),
314+
source_range: [124; 125),
315+
delete: [119; 125),
304316
insert: "type SomeType = ",
305317
kind: TypeAlias,
318+
lookup: "SomeType",
306319
},
307320
]
308321
"###);
@@ -329,6 +342,7 @@ mod tests {
329342
delete: [127; 134),
330343
insert: "const SOME_CONST: u16 = ",
331344
kind: Const,
345+
lookup: "SOME_CONST",
332346
},
333347
]
334348
"###);
@@ -355,6 +369,7 @@ mod tests {
355369
delete: [132; 139),
356370
insert: "const SOME_CONST: u16 = ",
357371
kind: Const,
372+
lookup: "SOME_CONST",
358373
},
359374
]
360375
"###);

0 commit comments

Comments
 (0)