Skip to content

Commit 8535f2b

Browse files
committed
Handle edge cases.
Handle case when BlockExpr is child of IfExpr, WhileExpr, LoopExpr, ForExpr. An additional { } will be added when: - It is not a BlockExpr - It is a BlockExpr and a child of IfExpr, WhileExpr, LoopExpr, ForExpr.
1 parent 370ba94 commit 8535f2b

File tree

1 file changed

+14
-4
lines changed

1 file changed

+14
-4
lines changed

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

Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ use hir::{Documentation, HasAttrs};
66
use ide_db::{imports::insert_use::ImportScope, ty_filter::TryEnum, SnippetCap};
77
use syntax::{
88
ast::{self, make, AstNode, AstToken},
9-
SyntaxKind::{EXPR_STMT, STMT_LIST},
9+
SyntaxKind::{BLOCK_EXPR, EXPR_STMT, FOR_EXPR, IF_EXPR, LOOP_EXPR, STMT_LIST, WHILE_EXPR},
1010
TextRange, TextSize,
1111
};
1212
use text_edit::TextEdit;
@@ -123,9 +123,19 @@ pub(crate) fn complete_postfix(
123123
postfix_snippet("ref", "&expr", &format!("&{receiver_text}")).add_to(acc);
124124
postfix_snippet("refm", "&mut expr", &format!("&mut {receiver_text}")).add_to(acc);
125125

126-
let unsafe_completion_string = match dot_receiver {
127-
ast::Expr::BlockExpr(_) => format!("unsafe {receiver_text}"),
128-
_ => format!("unsafe {{ {receiver_text} }}"),
126+
let mut unsafe_should_be_wrapped = true;
127+
if dot_receiver.syntax().kind() == BLOCK_EXPR {
128+
unsafe_should_be_wrapped = false;
129+
if let Some(parent) = dot_receiver.syntax().parent() {
130+
if matches!(parent.kind(), IF_EXPR | WHILE_EXPR | LOOP_EXPR | FOR_EXPR) {
131+
unsafe_should_be_wrapped = true;
132+
}
133+
}
134+
};
135+
let unsafe_completion_string = if unsafe_should_be_wrapped {
136+
format!("unsafe {{ {receiver_text} }}")
137+
} else {
138+
format!("unsafe {receiver_text}")
129139
};
130140
postfix_snippet("unsafe", "unsafe {}", &unsafe_completion_string).add_to(acc);
131141

0 commit comments

Comments
 (0)