aboutsummaryrefslogtreecommitdiff
path: root/crates/ra_ide/src/completion
diff options
context:
space:
mode:
Diffstat (limited to 'crates/ra_ide/src/completion')
-rw-r--r--crates/ra_ide/src/completion/complete_trait_impl.rs19
-rw-r--r--crates/ra_ide/src/completion/complete_unqualified_path.rs40
2 files changed, 49 insertions, 10 deletions
diff --git a/crates/ra_ide/src/completion/complete_trait_impl.rs b/crates/ra_ide/src/completion/complete_trait_impl.rs
index fab02945c..2ec0e7ce9 100644
--- a/crates/ra_ide/src/completion/complete_trait_impl.rs
+++ b/crates/ra_ide/src/completion/complete_trait_impl.rs
@@ -142,11 +142,11 @@ fn add_function_impl(
142 CompletionItemKind::Function 142 CompletionItemKind::Function
143 }; 143 };
144 144
145 let snippet = format!("{} {{}}", display); 145 let snippet = format!("{} {{\n $0\n}}", display);
146 146
147 let range = TextRange::from_to(fn_def_node.text_range().start(), ctx.source_range().end()); 147 let range = TextRange::from_to(fn_def_node.text_range().start(), ctx.source_range().end());
148 148
149 builder.text_edit(TextEdit::replace(range, snippet)).kind(completion_kind).add_to(acc); 149 builder.snippet_edit(TextEdit::replace(range, snippet)).kind(completion_kind).add_to(acc);
150} 150}
151 151
152fn add_type_alias_impl( 152fn add_type_alias_impl(
@@ -217,9 +217,10 @@ fn make_const_compl_syntax(const_: &ast::ConstDef) -> String {
217 217
218#[cfg(test)] 218#[cfg(test)]
219mod tests { 219mod tests {
220 use crate::completion::{test_utils::do_completion, CompletionItem, CompletionKind};
221 use insta::assert_debug_snapshot; 220 use insta::assert_debug_snapshot;
222 221
222 use crate::completion::{test_utils::do_completion, CompletionItem, CompletionKind};
223
223 fn complete(code: &str) -> Vec<CompletionItem> { 224 fn complete(code: &str) -> Vec<CompletionItem> {
224 do_completion(code, CompletionKind::Magic) 225 do_completion(code, CompletionKind::Magic)
225 } 226 }
@@ -255,7 +256,7 @@ mod tests {
255 label: "fn test()", 256 label: "fn test()",
256 source_range: [209; 210), 257 source_range: [209; 210),
257 delete: [209; 210), 258 delete: [209; 210),
258 insert: "fn test() {}", 259 insert: "fn test() {\n $0\n}",
259 kind: Function, 260 kind: Function,
260 lookup: "test", 261 lookup: "test",
261 }, 262 },
@@ -313,7 +314,7 @@ mod tests {
313 label: "fn test()", 314 label: "fn test()",
314 source_range: [139; 140), 315 source_range: [139; 140),
315 delete: [139; 140), 316 delete: [139; 140),
316 insert: "fn test() {}", 317 insert: "fn test() {\n $0\n}",
317 kind: Function, 318 kind: Function,
318 lookup: "test", 319 lookup: "test",
319 }, 320 },
@@ -342,7 +343,7 @@ mod tests {
342 label: "fn foo()", 343 label: "fn foo()",
343 source_range: [141; 142), 344 source_range: [141; 142),
344 delete: [138; 142), 345 delete: [138; 142),
345 insert: "fn foo() {}", 346 insert: "fn foo() {\n $0\n}",
346 kind: Function, 347 kind: Function,
347 lookup: "foo", 348 lookup: "foo",
348 }, 349 },
@@ -374,7 +375,7 @@ mod tests {
374 label: "fn foo_bar()", 375 label: "fn foo_bar()",
375 source_range: [200; 201), 376 source_range: [200; 201),
376 delete: [197; 201), 377 delete: [197; 201),
377 insert: "fn foo_bar() {}", 378 insert: "fn foo_bar() {\n $0\n}",
378 kind: Function, 379 kind: Function,
379 lookup: "foo_bar", 380 lookup: "foo_bar",
380 }, 381 },
@@ -425,7 +426,7 @@ mod tests {
425 label: "fn foo()", 426 label: "fn foo()",
426 source_range: [144; 145), 427 source_range: [144; 145),
427 delete: [141; 145), 428 delete: [141; 145),
428 insert: "fn foo<T>() {}", 429 insert: "fn foo<T>() {\n $0\n}",
429 kind: Function, 430 kind: Function,
430 lookup: "foo", 431 lookup: "foo",
431 }, 432 },
@@ -454,7 +455,7 @@ mod tests {
454 label: "fn foo()", 455 label: "fn foo()",
455 source_range: [166; 167), 456 source_range: [166; 167),
456 delete: [163; 167), 457 delete: [163; 167),
457 insert: "fn foo<T>()\nwhere T: Into<String> {}", 458 insert: "fn foo<T>()\nwhere T: Into<String> {\n $0\n}",
458 kind: Function, 459 kind: Function,
459 lookup: "foo", 460 lookup: "foo",
460 }, 461 },
diff --git a/crates/ra_ide/src/completion/complete_unqualified_path.rs b/crates/ra_ide/src/completion/complete_unqualified_path.rs
index 2d8e0776c..ad5fdcc4e 100644
--- a/crates/ra_ide/src/completion/complete_unqualified_path.rs
+++ b/crates/ra_ide/src/completion/complete_unqualified_path.rs
@@ -1,6 +1,10 @@
1//! Completion of names from the current scope, e.g. locals and imported items. 1//! Completion of names from the current scope, e.g. locals and imported items.
2 2
3use hir::ScopeDef;
4use test_utils::tested_by;
5
3use crate::completion::{CompletionContext, Completions}; 6use crate::completion::{CompletionContext, Completions};
7use ra_syntax::AstNode;
4 8
5pub(super) fn complete_unqualified_path(acc: &mut Completions, ctx: &CompletionContext) { 9pub(super) fn complete_unqualified_path(acc: &mut Completions, ctx: &CompletionContext) {
6 if !ctx.is_trivial_path { 10 if !ctx.is_trivial_path {
@@ -14,12 +18,23 @@ pub(super) fn complete_unqualified_path(acc: &mut Completions, ctx: &CompletionC
14 return; 18 return;
15 } 19 }
16 20
17 ctx.scope().process_all_names(&mut |name, res| acc.add_resolution(ctx, name.to_string(), &res)); 21 ctx.scope().process_all_names(&mut |name, res| {
22 if ctx.use_item_syntax.is_some() {
23 if let (ScopeDef::Unknown, Some(name_ref)) = (&res, &ctx.name_ref_syntax) {
24 if name_ref.syntax().text() == name.to_string().as_str() {
25 tested_by!(self_fulfilling_completion);
26 return;
27 }
28 }
29 }
30 acc.add_resolution(ctx, name.to_string(), &res)
31 });
18} 32}
19 33
20#[cfg(test)] 34#[cfg(test)]
21mod tests { 35mod tests {
22 use insta::assert_debug_snapshot; 36 use insta::assert_debug_snapshot;
37 use test_utils::covers;
23 38
24 use crate::completion::{test_utils::do_completion, CompletionItem, CompletionKind}; 39 use crate::completion::{test_utils::do_completion, CompletionItem, CompletionKind};
25 40
@@ -28,6 +43,29 @@ mod tests {
28 } 43 }
29 44
30 #[test] 45 #[test]
46 fn self_fulfilling_completion() {
47 covers!(self_fulfilling_completion);
48 assert_debug_snapshot!(
49 do_reference_completion(
50 r#"
51 use foo<|>
52 use std::collections;
53 "#,
54 ),
55 @r###"
56 [
57 CompletionItem {
58 label: "collections",
59 source_range: [21; 24),
60 delete: [21; 24),
61 insert: "collections",
62 },
63 ]
64 "###
65 );
66 }
67
68 #[test]
31 fn bind_pat_and_path_ignore_at() { 69 fn bind_pat_and_path_ignore_at() {
32 assert_debug_snapshot!( 70 assert_debug_snapshot!(
33 do_reference_completion( 71 do_reference_completion(