about summary refs log tree commit diff
diff options
context:
space:
mode:
authorDuong Quoc Khanh <dqkqdlot@gmail.com>2023-02-08 03:37:20 +0900
committerDuong Quoc Khanh <dqkqdlot@gmail.com>2023-02-08 03:37:20 +0900
commit8535f2bb1b1f554e3ff1633b5428b426ce50892b (patch)
tree11aaec686faf92b0ca0112e2e4abbaa32206a387
parent370ba94ca2c610fb91288c8198ccd02db0eb54f5 (diff)
downloadrust-8535f2bb1b1f554e3ff1633b5428b426ce50892b.tar.gz
rust-8535f2bb1b1f554e3ff1633b5428b426ce50892b.zip
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.
-rw-r--r--crates/ide-completion/src/completions/postfix.rs18
1 files changed, 14 insertions, 4 deletions
diff --git a/crates/ide-completion/src/completions/postfix.rs b/crates/ide-completion/src/completions/postfix.rs
index c68d106d835..83d457c2159 100644
--- a/crates/ide-completion/src/completions/postfix.rs
+++ b/crates/ide-completion/src/completions/postfix.rs
@@ -6,7 +6,7 @@ use hir::{Documentation, HasAttrs};
 use ide_db::{imports::insert_use::ImportScope, ty_filter::TryEnum, SnippetCap};
 use syntax::{
     ast::{self, make, AstNode, AstToken},
-    SyntaxKind::{EXPR_STMT, STMT_LIST},
+    SyntaxKind::{BLOCK_EXPR, EXPR_STMT, FOR_EXPR, IF_EXPR, LOOP_EXPR, STMT_LIST, WHILE_EXPR},
     TextRange, TextSize,
 };
 use text_edit::TextEdit;
@@ -123,9 +123,19 @@ pub(crate) fn complete_postfix(
     postfix_snippet("ref", "&expr", &format!("&{receiver_text}")).add_to(acc);
     postfix_snippet("refm", "&mut expr", &format!("&mut {receiver_text}")).add_to(acc);
 
-    let unsafe_completion_string = match dot_receiver {
-        ast::Expr::BlockExpr(_) => format!("unsafe {receiver_text}"),
-        _ => format!("unsafe {{ {receiver_text} }}"),
+    let mut unsafe_should_be_wrapped = true;
+    if dot_receiver.syntax().kind() == BLOCK_EXPR {
+        unsafe_should_be_wrapped = false;
+        if let Some(parent) = dot_receiver.syntax().parent() {
+            if matches!(parent.kind(), IF_EXPR | WHILE_EXPR | LOOP_EXPR | FOR_EXPR) {
+                unsafe_should_be_wrapped = true;
+            }
+        }
+    };
+    let unsafe_completion_string = if unsafe_should_be_wrapped {
+        format!("unsafe {{ {receiver_text} }}")
+    } else {
+        format!("unsafe {receiver_text}")
     };
     postfix_snippet("unsafe", "unsafe {}", &unsafe_completion_string).add_to(acc);