Skip to content

Commit 0bc9e62

Browse files
committed
Completion now replaces whole fn/const/type def with snippet.
1 parent 43e62a8 commit 0bc9e62

File tree

1 file changed

+31
-16
lines changed

1 file changed

+31
-16
lines changed

crates/ra_ide/src/completion/complete_trait_impl.rs

Lines changed: 31 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -10,10 +10,11 @@ use crate::{
1010
use hir::{self, Docs, HasSource};
1111
use ra_syntax::{
1212
ast::{self, edit},
13-
AstNode, SyntaxKind, TextRange,
13+
AstNode, SyntaxKind, SyntaxNode, TextRange,
1414
};
1515

1616
use ra_assists::utils::get_missing_impl_items;
17+
use ra_text_edit::TextEdit;
1718

1819
pub(crate) fn complete_trait_impl(acc: &mut Completions, ctx: &CompletionContext) {
1920
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
3738
_ => None,
3839
})
3940
{
40-
add_function_impl(acc, ctx, &missing_fn);
41+
add_function_impl(&trigger, acc, ctx, &missing_fn);
4142
}
4243
}
4344

@@ -49,7 +50,7 @@ pub(crate) fn complete_trait_impl(acc: &mut Completions, ctx: &CompletionContext
4950
_ => None,
5051
})
5152
{
52-
add_type_alias_impl(acc, ctx, &missing_fn);
53+
add_type_alias_impl(&trigger, acc, ctx, &missing_fn);
5354
}
5455
}
5556

@@ -61,7 +62,7 @@ pub(crate) fn complete_trait_impl(acc: &mut Completions, ctx: &CompletionContext
6162
_ => None,
6263
})
6364
{
64-
add_const_impl(acc, ctx, &missing_fn);
65+
add_const_impl(&trigger, acc, ctx, &missing_fn);
6566
}
6667
}
6768

@@ -70,7 +71,12 @@ pub(crate) fn complete_trait_impl(acc: &mut Completions, ctx: &CompletionContext
7071
}
7172
}
7273

73-
fn add_function_impl(acc: &mut Completions, ctx: &CompletionContext, func: &hir::Function) {
74+
fn add_function_impl(
75+
fn_def_node: &SyntaxNode,
76+
acc: &mut Completions,
77+
ctx: &CompletionContext,
78+
func: &hir::Function,
79+
) {
7480
let display = FunctionSignature::from_hir(ctx.db, func.clone());
7581

7682
let func_name = func.name(ctx.db);
@@ -93,28 +99,37 @@ fn add_function_impl(acc: &mut Completions, ctx: &CompletionContext, func: &hir:
9399

94100
let snippet = format!("{} {{}}", display);
95101

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);
97106
}
98107

99108
fn add_type_alias_impl(
109+
type_def_node: &SyntaxNode,
100110
acc: &mut Completions,
101111
ctx: &CompletionContext,
102112
type_alias: &hir::TypeAlias,
103113
) {
104114
let snippet = format!("type {} = ", type_alias.name(ctx.db).to_string());
105115

106116
CompletionItem::new(CompletionKind::Magic, ctx.source_range(), snippet.clone())
107-
.insert_text(snippet)
117+
.text_edit(TextEdit::replace(type_def_node.text_range(), snippet))
108118
.kind(CompletionItemKind::TypeAlias)
109119
.set_documentation(type_alias.docs(ctx.db))
110120
.add_to(acc);
111121
}
112122

113-
fn add_const_impl(acc: &mut Completions, ctx: &CompletionContext, const_: &hir::Const) {
123+
fn add_const_impl(
124+
const_def_node: &SyntaxNode,
125+
acc: &mut Completions,
126+
ctx: &CompletionContext,
127+
const_: &hir::Const,
128+
) {
114129
let snippet = make_const_compl_syntax(&const_.source(ctx.db).value);
115130

116131
CompletionItem::new(CompletionKind::Magic, ctx.source_range(), snippet.clone())
117-
.insert_text(snippet)
132+
.text_edit(TextEdit::replace(const_def_node.text_range(), snippet))
118133
.kind(CompletionItemKind::Const)
119134
.set_documentation(const_.docs(ctx.db))
120135
.add_to(acc);
@@ -172,7 +187,7 @@ mod tests {
172187
CompletionItem {
173188
label: "fn foo()",
174189
source_range: [140; 140),
175-
delete: [140; 140),
190+
delete: [138; 140),
176191
insert: "fn foo() {}",
177192
kind: Function,
178193
},
@@ -203,7 +218,7 @@ mod tests {
203218
CompletionItem {
204219
label: "fn bar()",
205220
source_range: [195; 195),
206-
delete: [195; 195),
221+
delete: [193; 195),
207222
insert: "fn bar() {}",
208223
kind: Function,
209224
},
@@ -231,7 +246,7 @@ mod tests {
231246
CompletionItem {
232247
label: "fn foo()",
233248
source_range: [143; 143),
234-
delete: [143; 143),
249+
delete: [141; 143),
235250
insert: "fn foo<T>() {}",
236251
kind: Function,
237252
},
@@ -259,7 +274,7 @@ mod tests {
259274
CompletionItem {
260275
label: "fn foo()",
261276
source_range: [165; 165),
262-
delete: [165; 165),
277+
delete: [163; 165),
263278
insert: "fn foo<T>()\nwhere T: Into<String> {}",
264279
kind: Function,
265280
},
@@ -285,7 +300,7 @@ mod tests {
285300
CompletionItem {
286301
label: "type SomeType = ",
287302
source_range: [123; 123),
288-
delete: [123; 123),
303+
delete: [119; 123),
289304
insert: "type SomeType = ",
290305
kind: TypeAlias,
291306
},
@@ -311,7 +326,7 @@ mod tests {
311326
CompletionItem {
312327
label: "const SOME_CONST: u16 = ",
313328
source_range: [133; 134),
314-
delete: [133; 134),
329+
delete: [127; 134),
315330
insert: "const SOME_CONST: u16 = ",
316331
kind: Const,
317332
},
@@ -337,7 +352,7 @@ mod tests {
337352
CompletionItem {
338353
label: "const SOME_CONST: u16 = ",
339354
source_range: [138; 139),
340-
delete: [138; 139),
355+
delete: [132; 139),
341356
insert: "const SOME_CONST: u16 = ",
342357
kind: Const,
343358
},

0 commit comments

Comments
 (0)