Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: Better support references in consuming postfix completions #18161

Merged
merged 2 commits into from
Sep 24, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
53 changes: 52 additions & 1 deletion crates/ide-completion/src/completions/postfix.rs
Original file line number Diff line number Diff line change
Expand Up @@ -294,6 +294,18 @@ fn include_references(initial_element: &ast::Expr) -> (ast::Expr, ast::Expr) {

let mut new_element_opt = initial_element.clone();

while let Some(parent_deref_element) =
resulting_element.syntax().parent().and_then(ast::PrefixExpr::cast)
{
if parent_deref_element.op_kind() != Some(ast::UnaryOp::Deref) {
break;
}

resulting_element = ast::Expr::from(parent_deref_element);

new_element_opt = make::expr_prefix(syntax::T![*], new_element_opt);
}

if let Some(first_ref_expr) = resulting_element.syntax().parent().and_then(ast::RefExpr::cast) {
if let Some(expr) = first_ref_expr.expr() {
resulting_element = expr;
Expand All @@ -302,9 +314,10 @@ fn include_references(initial_element: &ast::Expr) -> (ast::Expr, ast::Expr) {
while let Some(parent_ref_element) =
resulting_element.syntax().parent().and_then(ast::RefExpr::cast)
{
let exclusive = parent_ref_element.mut_token().is_some();
resulting_element = ast::Expr::from(parent_ref_element);

new_element_opt = make::expr_ref(new_element_opt, false);
new_element_opt = make::expr_ref(new_element_opt, exclusive);
}
} else {
// If we do not find any ref expressions, restore
Expand Down Expand Up @@ -855,4 +868,42 @@ fn test() {
expect![[r#""#]],
);
}

#[test]
fn mut_ref_consuming() {
check_edit(
"call",
r#"
fn main() {
let mut x = &mut 2;
&mut x.$0;
}
"#,
r#"
fn main() {
let mut x = &mut 2;
${1}(&mut x);
}
"#,
);
}

#[test]
fn deref_consuming() {
check_edit(
"call",
r#"
fn main() {
let mut x = &mut 2;
&mut *x.$0;
}
"#,
r#"
fn main() {
let mut x = &mut 2;
${1}(&mut *x);
}
"#,
);
}
}