aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorKevin DeLorey <[email protected]>2020-02-08 17:28:39 +0000
committerKevin DeLorey <[email protected]>2020-02-08 17:28:39 +0000
commitf801723dd2e4a518c1608909509f47f03d75fe1a (patch)
tree202cfd56356ebf4a776eee84a7ee22c739ededcf
parent5216b09ed67b4d9ff57e3ffb7fec010e73832309 (diff)
Got the magic completion working.
-rw-r--r--crates/ra_ide/src/completion/complete_trait_impl.rs48
-rw-r--r--crates/ra_ide/src/completion/completion_context.rs9
2 files changed, 16 insertions, 41 deletions
diff --git a/crates/ra_ide/src/completion/complete_trait_impl.rs b/crates/ra_ide/src/completion/complete_trait_impl.rs
index 8f38c6325..7af485cdd 100644
--- a/crates/ra_ide/src/completion/complete_trait_impl.rs
+++ b/crates/ra_ide/src/completion/complete_trait_impl.rs
@@ -6,13 +6,12 @@ use hir::{ self, db::HirDatabase };
6use ra_syntax::{ SyntaxKind, ast, ast::AstNode, TextRange }; 6use ra_syntax::{ SyntaxKind, ast, ast::AstNode, TextRange };
7 7
8pub(crate) fn complete_trait_impl(acc: &mut Completions, ctx: &CompletionContext) { 8pub(crate) fn complete_trait_impl(acc: &mut Completions, ctx: &CompletionContext) {
9 let item_list = ast::ItemList::cast(ctx.token.parent()); 9 let impl_block = ctx.impl_block.as_ref();
10 let impl_block = item_list 10 let item_list = impl_block.and_then(|i| i.item_list());
11 .clone()
12 .and_then(|i| i.syntax().parent())
13 .and_then(|p| ast::ImplBlock::cast(p));
14 11
15 if item_list.is_none() || impl_block.is_none() { 12 if item_list.is_none()
13 || impl_block.is_none()
14 || ctx.function_syntax.is_some() {
16 return; 15 return;
17 } 16 }
18 17
@@ -166,7 +165,8 @@ pub(crate) fn add_function_impl(acc: &mut Completions, ctx: &CompletionContext,
166 format!("fn {}()", func_name.to_string()) 165 format!("fn {}()", func_name.to_string())
167 }; 166 };
168 167
169 let builder = CompletionItem::new(CompletionKind::Magic, start, label); 168 let builder = CompletionItem::new(CompletionKind::Magic, start, label.clone())
169 .lookup_by(label);
170 170
171 let completion_kind = if func.has_self_param(ctx.db) { 171 let completion_kind = if func.has_self_param(ctx.db) {
172 CompletionItemKind::Method 172 CompletionItemKind::Method
@@ -183,7 +183,6 @@ pub(crate) fn add_function_impl(acc: &mut Completions, ctx: &CompletionContext,
183 builder 183 builder
184 .insert_text(snippet) 184 .insert_text(snippet)
185 .kind(completion_kind) 185 .kind(completion_kind)
186 .lookup_by(func_name.to_string())
187 .add_to(acc); 186 .add_to(acc);
188} 187}
189 188
@@ -219,7 +218,6 @@ mod tests {
219 delete: [138; 138), 218 delete: [138; 138),
220 insert: "fn foo() {}", 219 insert: "fn foo() {}",
221 kind: Function, 220 kind: Function,
222 lookup: "foo",
223 }, 221 },
224 ] 222 ]
225 "###); 223 "###);
@@ -251,7 +249,6 @@ mod tests {
251 delete: [193; 193), 249 delete: [193; 193),
252 insert: "fn bar() {}", 250 insert: "fn bar() {}",
253 kind: Function, 251 kind: Function,
254 lookup: "bar",
255 }, 252 },
256 ] 253 ]
257 "###); 254 "###);
@@ -280,7 +277,6 @@ mod tests {
280 delete: [141; 141), 277 delete: [141; 141),
281 insert: "fn foo<T>() {}", 278 insert: "fn foo<T>() {}",
282 kind: Function, 279 kind: Function,
283 lookup: "foo",
284 }, 280 },
285 ] 281 ]
286 "###); 282 "###);
@@ -309,36 +305,6 @@ mod tests {
309 delete: [163; 163), 305 delete: [163; 163),
310 insert: "fn foo<T>()\nwhere T: Into<String> {}", 306 insert: "fn foo<T>()\nwhere T: Into<String> {}",
311 kind: Function, 307 kind: Function,
312 lookup: "foo",
313 },
314 ]
315 "###);
316 }
317
318 #[test]
319 fn start_from_fn_kw() {
320 let completions = complete(
321 r"
322 trait Test {
323 fn foo();
324 }
325
326 struct T1;
327
328 impl Test for T1 {
329 fn <|>
330 }
331 ",
332 );
333 assert_debug_snapshot!(completions, @r###"
334 [
335 CompletionItem {
336 label: "fn foo()",
337 source_range: [138; 140),
338 delete: [138; 140),
339 insert: "fn foo() {}",
340 kind: Function,
341 lookup: "foo",
342 }, 308 },
343 ] 309 ]
344 "###); 310 "###);
diff --git a/crates/ra_ide/src/completion/completion_context.rs b/crates/ra_ide/src/completion/completion_context.rs
index deaacda6c..18c91a840 100644
--- a/crates/ra_ide/src/completion/completion_context.rs
+++ b/crates/ra_ide/src/completion/completion_context.rs
@@ -24,6 +24,7 @@ pub(crate) struct CompletionContext<'a> {
24 pub(super) use_item_syntax: Option<ast::UseItem>, 24 pub(super) use_item_syntax: Option<ast::UseItem>,
25 pub(super) record_lit_syntax: Option<ast::RecordLit>, 25 pub(super) record_lit_syntax: Option<ast::RecordLit>,
26 pub(super) record_lit_pat: Option<ast::RecordPat>, 26 pub(super) record_lit_pat: Option<ast::RecordPat>,
27 pub(super) impl_block: Option<ast::ImplBlock>,
27 pub(super) is_param: bool, 28 pub(super) is_param: bool,
28 /// If a name-binding or reference to a const in a pattern. 29 /// If a name-binding or reference to a const in a pattern.
29 /// Irrefutable patterns (like let) are excluded. 30 /// Irrefutable patterns (like let) are excluded.
@@ -71,6 +72,7 @@ impl<'a> CompletionContext<'a> {
71 use_item_syntax: None, 72 use_item_syntax: None,
72 record_lit_syntax: None, 73 record_lit_syntax: None,
73 record_lit_pat: None, 74 record_lit_pat: None,
75 impl_block: None,
74 is_param: false, 76 is_param: false,
75 is_pat_binding: false, 77 is_pat_binding: false,
76 is_trivial_path: false, 78 is_trivial_path: false,
@@ -147,6 +149,13 @@ impl<'a> CompletionContext<'a> {
147 self.record_lit_syntax = find_node_at_offset(original_file.syntax(), self.offset); 149 self.record_lit_syntax = find_node_at_offset(original_file.syntax(), self.offset);
148 } 150 }
149 151
152 self.impl_block = self
153 .token
154 .parent()
155 .ancestors()
156 .take_while(|it| it.kind() != SOURCE_FILE && it.kind() != MODULE)
157 .find_map(ast::ImplBlock::cast);
158
150 let top_node = name_ref 159 let top_node = name_ref
151 .syntax() 160 .syntax()
152 .ancestors() 161 .ancestors()