Skip to content

Commit 94889b6

Browse files
bors[bot]bnjjj
andauthored
Merge #4592
4592: fix textedit range returned for completion when left token is a keyword r=bnjjj a=bnjjj close #4545 Co-authored-by: Benjamin Coenen <5719034+bnjjj@users.noreply.github.com>
2 parents 59adc7b + 846cefa commit 94889b6

File tree

3 files changed

+49
-1
lines changed

3 files changed

+49
-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: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -297,6 +297,42 @@ mod tests {
297297
);
298298
}
299299

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

crates/ra_ide/src/completion/completion_context.rs

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ use ra_syntax::{
1212
use ra_text_edit::Indel;
1313

1414
use crate::{call_info::ActiveParameter, completion::CompletionConfig, FilePosition};
15+
use test_utils::mark;
1516

1617
/// `CompletionContext` is created early during completion to figure out, where
1718
/// exactly is the cursor, syntax-wise.
@@ -169,7 +170,17 @@ impl<'a> CompletionContext<'a> {
169170
match self.token.kind() {
170171
// workaroud when completion is triggered by trigger characters.
171172
IDENT => self.original_token.text_range(),
172-
_ => TextRange::empty(self.offset),
173+
_ => {
174+
// If we haven't characters between keyword and our cursor we take the keyword start range to edit
175+
if self.token.kind().is_keyword()
176+
&& self.offset == self.original_token.text_range().end()
177+
{
178+
mark::hit!(completes_bindings_from_for_with_in_prefix);
179+
TextRange::empty(self.original_token.text_range().start())
180+
} else {
181+
TextRange::empty(self.offset)
182+
}
183+
}
173184
}
174185
}
175186

0 commit comments

Comments
 (0)