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>
This commit is contained in:
bors[bot] 2020-05-27 13:22:26 +00:00 committed by GitHub
commit 94889b6472
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
3 changed files with 49 additions and 1 deletions

View file

@ -1363,6 +1363,7 @@ impl HirDisplay for Type {
}
/// For IDE only
#[derive(Debug)]
pub enum ScopeDef {
ModuleDef(ModuleDef),
MacroDef(MacroDef),

View file

@ -297,6 +297,42 @@ mod tests {
);
}
#[test]
fn completes_bindings_from_for_with_in_prefix() {
mark::check!(completes_bindings_from_for_with_in_prefix);
assert_debug_snapshot!(
do_reference_completion(
r"
fn test() {
for index in &[1, 2, 3] {
let t = in<|>
}
}
"
),
@r###"
[
CompletionItem {
label: "index",
source_range: 107..107,
delete: 107..107,
insert: "index",
kind: Binding,
},
CompletionItem {
label: "test()",
source_range: 107..107,
delete: 107..107,
insert: "test()$0",
kind: Function,
lookup: "test",
detail: "fn test()",
},
]
"###
);
}
#[test]
fn completes_generic_params() {
assert_debug_snapshot!(

View file

@ -12,6 +12,7 @@ use ra_syntax::{
use ra_text_edit::Indel;
use crate::{call_info::ActiveParameter, completion::CompletionConfig, FilePosition};
use test_utils::mark;
/// `CompletionContext` is created early during completion to figure out, where
/// exactly is the cursor, syntax-wise.
@ -169,7 +170,17 @@ impl<'a> CompletionContext<'a> {
match self.token.kind() {
// workaroud when completion is triggered by trigger characters.
IDENT => self.original_token.text_range(),
_ => TextRange::empty(self.offset),
_ => {
// If we haven't characters between keyword and our cursor we take the keyword start range to edit
if self.token.kind().is_keyword()
&& self.offset == self.original_token.text_range().end()
{
mark::hit!(completes_bindings_from_for_with_in_prefix);
TextRange::empty(self.original_token.text_range().start())
} else {
TextRange::empty(self.offset)
}
}
}
}