9345: fix: don't add duplicate `&` during completion r=matklad a=matklad

bors r+
🤖

Co-authored-by: Aleksey Kladov <aleksey.kladov@gmail.com>
This commit is contained in:
bors[bot] 2021-06-20 16:38:46 +00:00 committed by GitHub
commit 8cc2b710db
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 72 additions and 68 deletions

View file

@ -385,14 +385,19 @@ impl<'a> CompletionContext<'a> {
(ty, name)
},
ast::ArgList(_it) => {
cov_mark::hit!(expected_type_fn_param_with_leading_char);
cov_mark::hit!(expected_type_fn_param_without_leading_char);
cov_mark::hit!(expected_type_fn_param);
ActiveParameter::at_token(
&self.sema,
self.token.clone(),
).map(|ap| {
let name = ap.ident().map(NameOrNameRef::Name);
(Some(ap.ty), name)
let ty = if has_ref(&self.token) {
cov_mark::hit!(expected_type_fn_param_ref);
ap.ty.remove_ref()
} else {
Some(ap.ty)
};
(ty, name)
})
.unwrap_or((None, None))
},
@ -697,6 +702,19 @@ fn path_or_use_tree_qualifier(path: &ast::Path) -> Option<(ast::Path, bool)> {
use_tree.path().zip(Some(true))
}
fn has_ref(token: &SyntaxToken) -> bool {
let mut token = token.clone();
for skip in [WHITESPACE, IDENT, T![mut]] {
if token.kind() == skip {
token = match token.prev_token() {
Some(it) => it,
None => return false,
}
}
}
token.kind() == T![&]
}
#[cfg(test)]
mod tests {
use expect_test::{expect, Expect};
@ -769,14 +787,18 @@ fn foo() {
}
#[test]
fn expected_type_fn_param_without_leading_char() {
cov_mark::check!(expected_type_fn_param_without_leading_char);
fn expected_type_fn_param() {
cov_mark::check!(expected_type_fn_param);
check_expected_type_and_name(
r#"
fn foo() {
bar($0);
}
fn foo() { bar($0); }
fn bar(x: u32) {}
"#,
expect![[r#"ty: u32, name: x"#]],
);
check_expected_type_and_name(
r#"
fn foo() { bar(c$0); }
fn bar(x: u32) {}
"#,
expect![[r#"ty: u32, name: x"#]],
@ -784,18 +806,29 @@ fn bar(x: u32) {}
}
#[test]
fn expected_type_fn_param_with_leading_char() {
cov_mark::check!(expected_type_fn_param_with_leading_char);
fn expected_type_fn_param_ref() {
cov_mark::check!(expected_type_fn_param_ref);
check_expected_type_and_name(
r#"
fn foo() {
bar(c$0);
}
fn bar(x: u32) {}
fn foo() { bar(&$0); }
fn bar(x: &u32) {}
"#,
expect![[r#"ty: u32, name: x"#]],
);
check_expected_type_and_name(
r#"
fn foo() { bar(&mut $0); }
fn bar(x: &mut u32) {}
"#,
expect![[r#"ty: u32, name: x"#]],
);
check_expected_type_and_name(
r#"
fn foo() { bar(&c$0); }
fn bar(x: &u32) {}
"#,
expect![[r#"ty: u32, name: x"#]],
);
}
#[test]

View file

@ -1057,7 +1057,7 @@ fn f() {
#[test]
fn suggest_ref_mut() {
cov_mark::check!(suggest_ref);
check(
check_relevance(
r#"
struct S;
fn foo(s: &mut S) {}
@ -1067,58 +1067,29 @@ fn main() {
}
"#,
expect![[r#"
[
CompletionItem {
label: "S",
source_range: 70..70,
delete: 70..70,
insert: "S",
kind: SymbolKind(
Struct,
),
},
CompletionItem {
label: "foo(…)",
source_range: 70..70,
delete: 70..70,
insert: "foo(${1:&mut s})$0",
kind: SymbolKind(
Function,
),
lookup: "foo",
detail: "fn(&mut S)",
trigger_call_info: true,
},
CompletionItem {
label: "main()",
source_range: 70..70,
delete: 70..70,
insert: "main()$0",
kind: SymbolKind(
Function,
),
lookup: "main",
detail: "fn()",
},
CompletionItem {
label: "s",
source_range: 70..70,
delete: 70..70,
insert: "s",
kind: SymbolKind(
Local,
),
detail: "S",
relevance: CompletionRelevance {
exact_name_match: true,
type_match: None,
is_local: true,
},
ref_match: "&mut ",
},
]
lc s [name+local]
lc &mut s [type+name+local]
st S []
fn main() []
fn foo() []
"#]],
)
);
check_relevance(
r#"
struct S;
fn foo(s: &mut S) {}
fn main() {
let mut s = S;
foo(&mut $0);
}
"#,
expect![[r#"
lc s [type+name+local]
st S []
fn main() []
fn foo() []
"#]],
);
}
#[test]