mirror of
https://github.com/rust-lang/rust-analyzer
synced 2024-12-25 04:23:25 +00:00
Merge #9345
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:
commit
8cc2b710db
2 changed files with 72 additions and 68 deletions
|
@ -385,14 +385,19 @@ impl<'a> CompletionContext<'a> {
|
||||||
(ty, name)
|
(ty, name)
|
||||||
},
|
},
|
||||||
ast::ArgList(_it) => {
|
ast::ArgList(_it) => {
|
||||||
cov_mark::hit!(expected_type_fn_param_with_leading_char);
|
cov_mark::hit!(expected_type_fn_param);
|
||||||
cov_mark::hit!(expected_type_fn_param_without_leading_char);
|
|
||||||
ActiveParameter::at_token(
|
ActiveParameter::at_token(
|
||||||
&self.sema,
|
&self.sema,
|
||||||
self.token.clone(),
|
self.token.clone(),
|
||||||
).map(|ap| {
|
).map(|ap| {
|
||||||
let name = ap.ident().map(NameOrNameRef::Name);
|
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))
|
.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))
|
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)]
|
#[cfg(test)]
|
||||||
mod tests {
|
mod tests {
|
||||||
use expect_test::{expect, Expect};
|
use expect_test::{expect, Expect};
|
||||||
|
@ -769,14 +787,18 @@ fn foo() {
|
||||||
}
|
}
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn expected_type_fn_param_without_leading_char() {
|
fn expected_type_fn_param() {
|
||||||
cov_mark::check!(expected_type_fn_param_without_leading_char);
|
cov_mark::check!(expected_type_fn_param);
|
||||||
check_expected_type_and_name(
|
check_expected_type_and_name(
|
||||||
r#"
|
r#"
|
||||||
fn foo() {
|
fn foo() { bar($0); }
|
||||||
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) {}
|
fn bar(x: u32) {}
|
||||||
"#,
|
"#,
|
||||||
expect![[r#"ty: u32, name: x"#]],
|
expect![[r#"ty: u32, name: x"#]],
|
||||||
|
@ -784,18 +806,29 @@ fn bar(x: u32) {}
|
||||||
}
|
}
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn expected_type_fn_param_with_leading_char() {
|
fn expected_type_fn_param_ref() {
|
||||||
cov_mark::check!(expected_type_fn_param_with_leading_char);
|
cov_mark::check!(expected_type_fn_param_ref);
|
||||||
check_expected_type_and_name(
|
check_expected_type_and_name(
|
||||||
r#"
|
r#"
|
||||||
fn foo() {
|
fn foo() { bar(&$0); }
|
||||||
bar(c$0);
|
fn bar(x: &u32) {}
|
||||||
}
|
|
||||||
|
|
||||||
fn bar(x: u32) {}
|
|
||||||
"#,
|
"#,
|
||||||
expect![[r#"ty: u32, name: x"#]],
|
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]
|
#[test]
|
||||||
|
|
|
@ -1057,7 +1057,7 @@ fn f() {
|
||||||
#[test]
|
#[test]
|
||||||
fn suggest_ref_mut() {
|
fn suggest_ref_mut() {
|
||||||
cov_mark::check!(suggest_ref);
|
cov_mark::check!(suggest_ref);
|
||||||
check(
|
check_relevance(
|
||||||
r#"
|
r#"
|
||||||
struct S;
|
struct S;
|
||||||
fn foo(s: &mut S) {}
|
fn foo(s: &mut S) {}
|
||||||
|
@ -1067,58 +1067,29 @@ fn main() {
|
||||||
}
|
}
|
||||||
"#,
|
"#,
|
||||||
expect![[r#"
|
expect![[r#"
|
||||||
[
|
lc s [name+local]
|
||||||
CompletionItem {
|
lc &mut s [type+name+local]
|
||||||
label: "S",
|
st S []
|
||||||
source_range: 70..70,
|
fn main() []
|
||||||
delete: 70..70,
|
fn foo(…) []
|
||||||
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 ",
|
|
||||||
},
|
|
||||||
]
|
|
||||||
"#]],
|
"#]],
|
||||||
)
|
);
|
||||||
|
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]
|
#[test]
|
||||||
|
|
Loading…
Reference in a new issue