Skip to content

Commit 9dbb5dd

Browse files
committed
fix: postfix completions include nots prefix-expr
Example --- Like `is_foo`, `.not`, `.if` ```rust fn main() { let is_foo = true; !is_foo.$0 } ``` **Before this PR** ```rust fn main() { let is_foo = true; !if is_foo { $0 } } ``` **After this PR** ```rust fn main() { let is_foo = true; if !is_foo { $0 } } ```
1 parent 642e9e1 commit 9dbb5dd

1 file changed

Lines changed: 32 additions & 6 deletions

File tree

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

Lines changed: 32 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -402,23 +402,28 @@ 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-
while let Some(parent_deref_element) =
414-
resulting_element.syntax().parent().and_then(ast::PrefixExpr::cast)
415-
&& 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)
416415
{
417-
resulting_element = ast::Expr::from(parent_deref_element);
418-
416+
resulting_element = ast::Expr::from(parent);
419417
prefix.insert(0, '*');
420418
}
421419

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+
422427
while let Some(parent_ref_element) =
423428
resulting_element.syntax().parent().and_then(ast::RefExpr::cast)
424429
{
@@ -1121,6 +1126,27 @@ fn main() {
11211126
)
11221127
}
11231128

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+
11241150
#[test]
11251151
fn postfix_completion_for_unsafe() {
11261152
postfix_completion_for_block("unsafe");

0 commit comments

Comments
 (0)