Skip to content

Commit

Permalink
fix: don't add duplicate & during completion
Browse files Browse the repository at this point in the history
  • Loading branch information
matklad committed Jun 20, 2021
1 parent f1097c2 commit 9a3eae8
Show file tree
Hide file tree
Showing 2 changed files with 72 additions and 68 deletions.
65 changes: 49 additions & 16 deletions crates/ide_completion/src/context.rs
Expand Up @@ -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))
},
Expand Down Expand Up @@ -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};
Expand Down Expand Up @@ -769,33 +787,48 @@ 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"#]],
);
}

#[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]
Expand Down
75 changes: 23 additions & 52 deletions crates/ide_completion/src/render.rs
Expand Up @@ -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) {}
Expand All @@ -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]
Expand Down

0 comments on commit 9a3eae8

Please sign in to comment.