Skip to content

Commit 0e814a3

Browse files
committed
fix textedit range returned for completion when left token is a keyword #4545
Signed-off-by: Benjamin Coenen <5719034+bnjjj@users.noreply.github.com>
1 parent f4f5fca commit 0e814a3

File tree

3 files changed

+46
-1
lines changed

3 files changed

+46
-1
lines changed

crates/ra_hir/src/code_model.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1363,6 +1363,7 @@ impl HirDisplay for Type {
13631363
}
13641364

13651365
/// For IDE only
1366+
#[derive(Debug)]
13661367
pub enum ScopeDef {
13671368
ModuleDef(ModuleDef),
13681369
MacroDef(MacroDef),

crates/ra_ide/src/completion/complete_unqualified_path.rs

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -297,6 +297,41 @@ mod tests {
297297
);
298298
}
299299

300+
#[test]
301+
fn completes_bindings_from_for_with_in_prefix() {
302+
assert_debug_snapshot!(
303+
do_reference_completion(
304+
r"
305+
fn test() {
306+
for index in &[1, 2, 3] {
307+
let t = in<|>
308+
}
309+
}
310+
"
311+
),
312+
@r###"
313+
[
314+
CompletionItem {
315+
label: "index",
316+
source_range: 107..107,
317+
delete: 107..107,
318+
insert: "index",
319+
kind: Binding,
320+
},
321+
CompletionItem {
322+
label: "test()",
323+
source_range: 107..107,
324+
delete: 107..107,
325+
insert: "test()$0",
326+
kind: Function,
327+
lookup: "test",
328+
detail: "fn test()",
329+
},
330+
]
331+
"###
332+
);
333+
}
334+
300335
#[test]
301336
fn completes_generic_params() {
302337
assert_debug_snapshot!(

crates/ra_ide/src/completion/completion_context.rs

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -169,7 +169,16 @@ impl<'a> CompletionContext<'a> {
169169
match self.token.kind() {
170170
// workaroud when completion is triggered by trigger characters.
171171
IDENT => self.original_token.text_range(),
172-
_ => TextRange::empty(self.offset),
172+
_ => {
173+
// If we haven't characters between keyword and our cursor we take the keyword start range to edit
174+
if self.token.kind().is_keyword()
175+
&& self.offset == self.original_token.text_range().end()
176+
{
177+
TextRange::empty(self.original_token.text_range().start())
178+
} else {
179+
TextRange::empty(self.offset)
180+
}
181+
}
173182
}
174183
}
175184

0 commit comments

Comments
 (0)