Skip to content

Commit a75b500

Browse files
Merge pull request #21903 from A4-Tacks/postfix-include-nots
fix: postfix completions include nots prefix-expr
2 parents 1f40aa9 + 9dbb5dd commit a75b500

1 file changed

Lines changed: 32 additions & 17 deletions

File tree

crates/ide-completion/src/completions/postfix.rs

Lines changed: 32 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -402,30 +402,31 @@ fn receiver_accessor(receiver: &ast::Expr) -> ast::Expr {
402402
.unwrap_or_else(|| receiver.clone())
403403
}
404404

405-
/// Given an `initial_element`, tries to expand it to include deref(s), and then references.
405+
/// Given an `initial_element`, tries to expand it to include deref(s), not(s), and then references.
406406
/// Returns the expanded expressions, and the added prefix as a string
407407
///
408408
/// For example, if called with the `42` in `&&mut *42`, would return `(&&mut *42, "&&mut *")`.
409409
fn include_references(initial_element: &ast::Expr) -> (ast::Expr, String) {
410410
let mut resulting_element = initial_element.clone();
411411
let mut prefix = String::new();
412412

413-
let mut found_ref_or_deref = false;
414-
415-
while let Some(parent_deref_element) =
416-
resulting_element.syntax().parent().and_then(ast::PrefixExpr::cast)
417-
&& parent_deref_element.op_kind() == Some(ast::UnaryOp::Deref)
413+
while let Some(parent) = resulting_element.syntax().parent().and_then(ast::PrefixExpr::cast)
414+
&& parent.op_kind() == Some(ast::UnaryOp::Deref)
418415
{
419-
found_ref_or_deref = true;
420-
resulting_element = ast::Expr::from(parent_deref_element);
421-
416+
resulting_element = ast::Expr::from(parent);
422417
prefix.insert(0, '*');
423418
}
424419

420+
while let Some(parent) = resulting_element.syntax().parent().and_then(ast::PrefixExpr::cast)
421+
&& parent.op_kind() == Some(ast::UnaryOp::Not)
422+
{
423+
resulting_element = ast::Expr::from(parent);
424+
prefix.insert(0, '!');
425+
}
426+
425427
while let Some(parent_ref_element) =
426428
resulting_element.syntax().parent().and_then(ast::RefExpr::cast)
427429
{
428-
found_ref_or_deref = true;
429430
let last_child_or_token = parent_ref_element.syntax().last_child_or_token();
430431
prefix.insert_str(
431432
0,
@@ -440,13 +441,6 @@ fn include_references(initial_element: &ast::Expr) -> (ast::Expr, String) {
440441
resulting_element = ast::Expr::from(parent_ref_element);
441442
}
442443

443-
if !found_ref_or_deref {
444-
// If we do not find any ref/deref expressions, restore
445-
// all the progress of tree climbing
446-
prefix.clear();
447-
resulting_element = initial_element.clone();
448-
}
449-
450444
(resulting_element, prefix)
451445
}
452446

@@ -1132,6 +1126,27 @@ fn main() {
11321126
)
11331127
}
11341128

1129+
#[test]
1130+
fn postfix_completion_for_nots() {
1131+
check_edit(
1132+
"if",
1133+
r#"
1134+
fn main() {
1135+
let is_foo = true;
1136+
!is_foo.$0
1137+
}
1138+
"#,
1139+
r#"
1140+
fn main() {
1141+
let is_foo = true;
1142+
if !is_foo {
1143+
$0
1144+
}
1145+
}
1146+
"#,
1147+
)
1148+
}
1149+
11351150
#[test]
11361151
fn postfix_completion_for_unsafe() {
11371152
postfix_completion_for_block("unsafe");

0 commit comments

Comments
 (0)