aboutsummaryrefslogtreecommitdiff
path: root/crates
diff options
context:
space:
mode:
Diffstat (limited to 'crates')
-rw-r--r--crates/ra_ide/src/completion/complete_trait_impl.rs43
1 files changed, 36 insertions, 7 deletions
diff --git a/crates/ra_ide/src/completion/complete_trait_impl.rs b/crates/ra_ide/src/completion/complete_trait_impl.rs
index 54301cdf2..83b97eb76 100644
--- a/crates/ra_ide/src/completion/complete_trait_impl.rs
+++ b/crates/ra_ide/src/completion/complete_trait_impl.rs
@@ -17,7 +17,10 @@ use crate::{
17 17
18pub(crate) fn complete_trait_impl(acc: &mut Completions, ctx: &CompletionContext) { 18pub(crate) fn complete_trait_impl(acc: &mut Completions, ctx: &CompletionContext) {
19 let trigger = ctx.token.ancestors().find(|p| match p.kind() { 19 let trigger = ctx.token.ancestors().find(|p| match p.kind() {
20 SyntaxKind::FN_DEF | SyntaxKind::TYPE_ALIAS_DEF | SyntaxKind::CONST_DEF => true, 20 SyntaxKind::FN_DEF
21 | SyntaxKind::TYPE_ALIAS_DEF
22 | SyntaxKind::CONST_DEF
23 | SyntaxKind::BLOCK_EXPR => true,
21 _ => false, 24 _ => false,
22 }); 25 });
23 26
@@ -98,10 +101,9 @@ fn add_function_impl(
98 101
99 let snippet = format!("{} {{}}", display); 102 let snippet = format!("{} {{}}", display);
100 103
101 builder 104 let range = TextRange::from_to(fn_def_node.text_range().start(), ctx.source_range().end());
102 .text_edit(TextEdit::replace(fn_def_node.text_range(), snippet)) 105
103 .kind(completion_kind) 106 builder.text_edit(TextEdit::replace(range, snippet)).kind(completion_kind).add_to(acc);
104 .add_to(acc);
105} 107}
106 108
107fn add_type_alias_impl( 109fn add_type_alias_impl(
@@ -114,8 +116,10 @@ fn add_type_alias_impl(
114 116
115 let snippet = format!("type {} = ", alias_name); 117 let snippet = format!("type {} = ", alias_name);
116 118
119 let range = TextRange::from_to(type_def_node.text_range().start(), ctx.source_range().end());
120
117 CompletionItem::new(CompletionKind::Magic, ctx.source_range(), snippet.clone()) 121 CompletionItem::new(CompletionKind::Magic, ctx.source_range(), snippet.clone())
118 .text_edit(TextEdit::replace(type_def_node.text_range(), snippet)) 122 .text_edit(TextEdit::replace(range, snippet))
119 .lookup_by(alias_name) 123 .lookup_by(alias_name)
120 .kind(CompletionItemKind::TypeAlias) 124 .kind(CompletionItemKind::TypeAlias)
121 .set_documentation(type_alias.docs(ctx.db)) 125 .set_documentation(type_alias.docs(ctx.db))
@@ -133,8 +137,11 @@ fn add_const_impl(
133 if let Some(const_name) = const_name { 137 if let Some(const_name) = const_name {
134 let snippet = make_const_compl_syntax(&const_.source(ctx.db).value); 138 let snippet = make_const_compl_syntax(&const_.source(ctx.db).value);
135 139
140 let range =
141 TextRange::from_to(const_def_node.text_range().start(), ctx.source_range().end());
142
136 CompletionItem::new(CompletionKind::Magic, ctx.source_range(), snippet.clone()) 143 CompletionItem::new(CompletionKind::Magic, ctx.source_range(), snippet.clone())
137 .text_edit(TextEdit::replace(const_def_node.text_range(), snippet)) 144 .text_edit(TextEdit::replace(range, snippet))
138 .lookup_by(const_name) 145 .lookup_by(const_name)
139 .kind(CompletionItemKind::Const) 146 .kind(CompletionItemKind::Const)
140 .set_documentation(const_.docs(ctx.db)) 147 .set_documentation(const_.docs(ctx.db))
@@ -236,6 +243,28 @@ mod tests {
236 } 243 }
237 244
238 #[test] 245 #[test]
246 fn completes_only_on_top_level() {
247 let completions = complete(
248 r"
249 trait Test {
250 fn foo();
251
252 fn foo_bar();
253 }
254
255 struct T1;
256
257 impl Test for T1 {
258 fn foo() {
259 <|>
260 }
261 }
262 ",
263 );
264 assert_debug_snapshot!(completions, @r###"[]"###);
265 }
266
267 #[test]
239 fn generic_fn() { 268 fn generic_fn() {
240 let completions = complete( 269 let completions = complete(
241 r" 270 r"