From be97cbfdb4dc83b07bfc0e2fd6f8d2d3d90a5c65 Mon Sep 17 00:00:00 2001 From: Kevin DeLorey <2295721+kdelorey@users.noreply.github.com> Date: Thu, 13 Feb 2020 19:10:08 -0600 Subject: Adjusted the completion lookups to filter by just the name. --- .../ra_ide/src/completion/complete_trait_impl.rs | 73 +++++++++++++--------- 1 file changed, 44 insertions(+), 29 deletions(-) diff --git a/crates/ra_ide/src/completion/complete_trait_impl.rs b/crates/ra_ide/src/completion/complete_trait_impl.rs index 81899308b..bff19c5bb 100644 --- a/crates/ra_ide/src/completion/complete_trait_impl.rs +++ b/crates/ra_ide/src/completion/complete_trait_impl.rs @@ -79,16 +79,16 @@ fn add_function_impl( ) { let display = FunctionSignature::from_hir(ctx.db, func.clone()); - let func_name = func.name(ctx.db); + let fn_name = func.name(ctx.db).to_string(); let label = if func.params(ctx.db).len() > 0 { - format!("fn {}(..)", func_name.to_string()) + format!("fn {}(..)", fn_name) } else { - format!("fn {}()", func_name.to_string()) + format!("fn {}()", fn_name) }; let builder = CompletionItem::new(CompletionKind::Magic, ctx.source_range(), label.clone()) - .lookup_by(label) + .lookup_by(fn_name) .set_documentation(func.docs(ctx.db)); let completion_kind = if func.has_self_param(ctx.db) { @@ -111,10 +111,13 @@ fn add_type_alias_impl( ctx: &CompletionContext, type_alias: &hir::TypeAlias, ) { - let snippet = format!("type {} = ", type_alias.name(ctx.db).to_string()); + let alias_name = type_alias.name(ctx.db).to_string(); + + let snippet = format!("type {} = ", alias_name); CompletionItem::new(CompletionKind::Magic, ctx.source_range(), snippet.clone()) .text_edit(TextEdit::replace(type_def_node.text_range(), snippet)) + .lookup_by(alias_name) .kind(CompletionItemKind::TypeAlias) .set_documentation(type_alias.docs(ctx.db)) .add_to(acc); @@ -126,13 +129,18 @@ fn add_const_impl( ctx: &CompletionContext, const_: &hir::Const, ) { - let snippet = make_const_compl_syntax(&const_.source(ctx.db).value); + let const_name = const_.name(ctx.db).map(|n| n.to_string()); - CompletionItem::new(CompletionKind::Magic, ctx.source_range(), snippet.clone()) - .text_edit(TextEdit::replace(const_def_node.text_range(), snippet)) - .kind(CompletionItemKind::Const) - .set_documentation(const_.docs(ctx.db)) - .add_to(acc); + if let Some(const_name) = const_name { + let snippet = make_const_compl_syntax(&const_.source(ctx.db).value); + + CompletionItem::new(CompletionKind::Magic, ctx.source_range(), snippet.clone()) + .text_edit(TextEdit::replace(const_def_node.text_range(), snippet)) + .lookup_by(const_name) + .kind(CompletionItemKind::Const) + .set_documentation(const_.docs(ctx.db)) + .add_to(acc); + } } fn make_const_compl_syntax(const_: &ast::ConstDef) -> String { @@ -178,7 +186,7 @@ mod tests { struct T1; impl Test for T1 { - fn<|> + fn f<|> } ", ); @@ -186,10 +194,11 @@ mod tests { [ CompletionItem { label: "fn foo()", - source_range: [140; 140), - delete: [138; 140), + source_range: [141; 142), + delete: [138; 142), insert: "fn foo() {}", kind: Function, + lookup: "foo", }, ] "###); @@ -201,7 +210,7 @@ mod tests { r" trait Test { fn foo(); - fn bar(); + fn foo_bar(); } struct T1; @@ -209,18 +218,19 @@ mod tests { impl Test for T1 { fn foo() {} - fn<|> + fn f<|> } ", ); assert_debug_snapshot!(completions, @r###" [ CompletionItem { - label: "fn bar()", - source_range: [195; 195), - delete: [193; 195), - insert: "fn bar() {}", + label: "fn foo_bar()", + source_range: [200; 201), + delete: [197; 201), + insert: "fn foo_bar() {}", kind: Function, + lookup: "foo_bar", }, ] "###); @@ -237,7 +247,7 @@ mod tests { struct T1; impl Test for T1 { - fn<|> + fn f<|> } ", ); @@ -245,10 +255,11 @@ mod tests { [ CompletionItem { label: "fn foo()", - source_range: [143; 143), - delete: [141; 143), + source_range: [144; 145), + delete: [141; 145), insert: "fn foo() {}", kind: Function, + lookup: "foo", }, ] "###); @@ -265,7 +276,7 @@ mod tests { struct T1; impl Test for T1 { - fn<|> + fn f<|> } ", ); @@ -273,10 +284,11 @@ mod tests { [ CompletionItem { label: "fn foo()", - source_range: [165; 165), - delete: [163; 165), + source_range: [166; 167), + delete: [163; 167), insert: "fn foo()\nwhere T: Into {}", kind: Function, + lookup: "foo", }, ] "###); @@ -291,7 +303,7 @@ mod tests { } impl Test for () { - type<|> + type S<|> } ", ); @@ -299,10 +311,11 @@ mod tests { [ CompletionItem { label: "type SomeType = ", - source_range: [123; 123), - delete: [119; 123), + source_range: [124; 125), + delete: [119; 125), insert: "type SomeType = ", kind: TypeAlias, + lookup: "SomeType", }, ] "###); @@ -329,6 +342,7 @@ mod tests { delete: [127; 134), insert: "const SOME_CONST: u16 = ", kind: Const, + lookup: "SOME_CONST", }, ] "###); @@ -355,6 +369,7 @@ mod tests { delete: [132; 139), insert: "const SOME_CONST: u16 = ", kind: Const, + lookup: "SOME_CONST", }, ] "###); -- cgit v1.2.3