aboutsummaryrefslogtreecommitdiff
path: root/crates/ra_ide/src/completion/complete_trait_impl.rs
diff options
context:
space:
mode:
authorKevin DeLorey <[email protected]>2020-02-13 03:00:47 +0000
committerKevin DeLorey <[email protected]>2020-02-13 03:00:47 +0000
commit0bc9e623747b462ab11a4660a19a50bc38313875 (patch)
tree66c1ea4694f37cf4d2dbe74f8ad2af1ab28019e4 /crates/ra_ide/src/completion/complete_trait_impl.rs
parent43e62a87ab97f946f3d2da2db0aa0b1f0ef7d8bf (diff)
Completion now replaces whole fn/const/type def with snippet.
Diffstat (limited to 'crates/ra_ide/src/completion/complete_trait_impl.rs')
-rw-r--r--crates/ra_ide/src/completion/complete_trait_impl.rs47
1 files changed, 31 insertions, 16 deletions
diff --git a/crates/ra_ide/src/completion/complete_trait_impl.rs b/crates/ra_ide/src/completion/complete_trait_impl.rs
index 912bf789e..81899308b 100644
--- a/crates/ra_ide/src/completion/complete_trait_impl.rs
+++ b/crates/ra_ide/src/completion/complete_trait_impl.rs
@@ -10,10 +10,11 @@ use crate::{
10use hir::{self, Docs, HasSource}; 10use hir::{self, Docs, HasSource};
11use ra_syntax::{ 11use ra_syntax::{
12 ast::{self, edit}, 12 ast::{self, edit},
13 AstNode, SyntaxKind, TextRange, 13 AstNode, SyntaxKind, SyntaxNode, TextRange,
14}; 14};
15 15
16use ra_assists::utils::get_missing_impl_items; 16use ra_assists::utils::get_missing_impl_items;
17use ra_text_edit::TextEdit;
17 18
18pub(crate) fn complete_trait_impl(acc: &mut Completions, ctx: &CompletionContext) { 19pub(crate) fn complete_trait_impl(acc: &mut Completions, ctx: &CompletionContext) {
19 let trigger = ctx.token.ancestors().find(|p| match p.kind() { 20 let trigger = ctx.token.ancestors().find(|p| match p.kind() {
@@ -37,7 +38,7 @@ pub(crate) fn complete_trait_impl(acc: &mut Completions, ctx: &CompletionContext
37 _ => None, 38 _ => None,
38 }) 39 })
39 { 40 {
40 add_function_impl(acc, ctx, &missing_fn); 41 add_function_impl(&trigger, acc, ctx, &missing_fn);
41 } 42 }
42 } 43 }
43 44
@@ -49,7 +50,7 @@ pub(crate) fn complete_trait_impl(acc: &mut Completions, ctx: &CompletionContext
49 _ => None, 50 _ => None,
50 }) 51 })
51 { 52 {
52 add_type_alias_impl(acc, ctx, &missing_fn); 53 add_type_alias_impl(&trigger, acc, ctx, &missing_fn);
53 } 54 }
54 } 55 }
55 56
@@ -61,7 +62,7 @@ pub(crate) fn complete_trait_impl(acc: &mut Completions, ctx: &CompletionContext
61 _ => None, 62 _ => None,
62 }) 63 })
63 { 64 {
64 add_const_impl(acc, ctx, &missing_fn); 65 add_const_impl(&trigger, acc, ctx, &missing_fn);
65 } 66 }
66 } 67 }
67 68
@@ -70,7 +71,12 @@ pub(crate) fn complete_trait_impl(acc: &mut Completions, ctx: &CompletionContext
70 } 71 }
71} 72}
72 73
73fn add_function_impl(acc: &mut Completions, ctx: &CompletionContext, func: &hir::Function) { 74fn add_function_impl(
75 fn_def_node: &SyntaxNode,
76 acc: &mut Completions,
77 ctx: &CompletionContext,
78 func: &hir::Function,
79) {
74 let display = FunctionSignature::from_hir(ctx.db, func.clone()); 80 let display = FunctionSignature::from_hir(ctx.db, func.clone());
75 81
76 let func_name = func.name(ctx.db); 82 let func_name = func.name(ctx.db);
@@ -93,10 +99,14 @@ fn add_function_impl(acc: &mut Completions, ctx: &CompletionContext, func: &hir:
93 99
94 let snippet = format!("{} {{}}", display); 100 let snippet = format!("{} {{}}", display);
95 101
96 builder.insert_text(snippet).kind(completion_kind).add_to(acc); 102 builder
103 .text_edit(TextEdit::replace(fn_def_node.text_range(), snippet))
104 .kind(completion_kind)
105 .add_to(acc);
97} 106}
98 107
99fn add_type_alias_impl( 108fn add_type_alias_impl(
109 type_def_node: &SyntaxNode,
100 acc: &mut Completions, 110 acc: &mut Completions,
101 ctx: &CompletionContext, 111 ctx: &CompletionContext,
102 type_alias: &hir::TypeAlias, 112 type_alias: &hir::TypeAlias,
@@ -104,17 +114,22 @@ fn add_type_alias_impl(
104 let snippet = format!("type {} = ", type_alias.name(ctx.db).to_string()); 114 let snippet = format!("type {} = ", type_alias.name(ctx.db).to_string());
105 115
106 CompletionItem::new(CompletionKind::Magic, ctx.source_range(), snippet.clone()) 116 CompletionItem::new(CompletionKind::Magic, ctx.source_range(), snippet.clone())
107 .insert_text(snippet) 117 .text_edit(TextEdit::replace(type_def_node.text_range(), snippet))
108 .kind(CompletionItemKind::TypeAlias) 118 .kind(CompletionItemKind::TypeAlias)
109 .set_documentation(type_alias.docs(ctx.db)) 119 .set_documentation(type_alias.docs(ctx.db))
110 .add_to(acc); 120 .add_to(acc);
111} 121}
112 122
113fn add_const_impl(acc: &mut Completions, ctx: &CompletionContext, const_: &hir::Const) { 123fn add_const_impl(
124 const_def_node: &SyntaxNode,
125 acc: &mut Completions,
126 ctx: &CompletionContext,
127 const_: &hir::Const,
128) {
114 let snippet = make_const_compl_syntax(&const_.source(ctx.db).value); 129 let snippet = make_const_compl_syntax(&const_.source(ctx.db).value);
115 130
116 CompletionItem::new(CompletionKind::Magic, ctx.source_range(), snippet.clone()) 131 CompletionItem::new(CompletionKind::Magic, ctx.source_range(), snippet.clone())
117 .insert_text(snippet) 132 .text_edit(TextEdit::replace(const_def_node.text_range(), snippet))
118 .kind(CompletionItemKind::Const) 133 .kind(CompletionItemKind::Const)
119 .set_documentation(const_.docs(ctx.db)) 134 .set_documentation(const_.docs(ctx.db))
120 .add_to(acc); 135 .add_to(acc);
@@ -172,7 +187,7 @@ mod tests {
172 CompletionItem { 187 CompletionItem {
173 label: "fn foo()", 188 label: "fn foo()",
174 source_range: [140; 140), 189 source_range: [140; 140),
175 delete: [140; 140), 190 delete: [138; 140),
176 insert: "fn foo() {}", 191 insert: "fn foo() {}",
177 kind: Function, 192 kind: Function,
178 }, 193 },
@@ -203,7 +218,7 @@ mod tests {
203 CompletionItem { 218 CompletionItem {
204 label: "fn bar()", 219 label: "fn bar()",
205 source_range: [195; 195), 220 source_range: [195; 195),
206 delete: [195; 195), 221 delete: [193; 195),
207 insert: "fn bar() {}", 222 insert: "fn bar() {}",
208 kind: Function, 223 kind: Function,
209 }, 224 },
@@ -231,7 +246,7 @@ mod tests {
231 CompletionItem { 246 CompletionItem {
232 label: "fn foo()", 247 label: "fn foo()",
233 source_range: [143; 143), 248 source_range: [143; 143),
234 delete: [143; 143), 249 delete: [141; 143),
235 insert: "fn foo<T>() {}", 250 insert: "fn foo<T>() {}",
236 kind: Function, 251 kind: Function,
237 }, 252 },
@@ -259,7 +274,7 @@ mod tests {
259 CompletionItem { 274 CompletionItem {
260 label: "fn foo()", 275 label: "fn foo()",
261 source_range: [165; 165), 276 source_range: [165; 165),
262 delete: [165; 165), 277 delete: [163; 165),
263 insert: "fn foo<T>()\nwhere T: Into<String> {}", 278 insert: "fn foo<T>()\nwhere T: Into<String> {}",
264 kind: Function, 279 kind: Function,
265 }, 280 },
@@ -285,7 +300,7 @@ mod tests {
285 CompletionItem { 300 CompletionItem {
286 label: "type SomeType = ", 301 label: "type SomeType = ",
287 source_range: [123; 123), 302 source_range: [123; 123),
288 delete: [123; 123), 303 delete: [119; 123),
289 insert: "type SomeType = ", 304 insert: "type SomeType = ",
290 kind: TypeAlias, 305 kind: TypeAlias,
291 }, 306 },
@@ -311,7 +326,7 @@ mod tests {
311 CompletionItem { 326 CompletionItem {
312 label: "const SOME_CONST: u16 = ", 327 label: "const SOME_CONST: u16 = ",
313 source_range: [133; 134), 328 source_range: [133; 134),
314 delete: [133; 134), 329 delete: [127; 134),
315 insert: "const SOME_CONST: u16 = ", 330 insert: "const SOME_CONST: u16 = ",
316 kind: Const, 331 kind: Const,
317 }, 332 },
@@ -337,7 +352,7 @@ mod tests {
337 CompletionItem { 352 CompletionItem {
338 label: "const SOME_CONST: u16 = ", 353 label: "const SOME_CONST: u16 = ",
339 source_range: [138; 139), 354 source_range: [138; 139),
340 delete: [138; 139), 355 delete: [132; 139),
341 insert: "const SOME_CONST: u16 = ", 356 insert: "const SOME_CONST: u16 = ",
342 kind: Const, 357 kind: Const,
343 }, 358 },