From 8535f2bb1b1f554e3ff1633b5428b426ce50892b Mon Sep 17 00:00:00 2001 From: Duong Quoc Khanh Date: Wed, 8 Feb 2023 03:37:20 +0900 Subject: [PATCH] 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. --- .../ide-completion/src/completions/postfix.rs | 18 ++++++++++++++---- 1 file 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);