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.
This commit is contained in:
Duong Quoc Khanh 2023-02-08 03:37:20 +09:00
parent 370ba94ca2
commit 8535f2bb1b
No known key found for this signature in database
GPG key ID: DA52A74E804A00E7

View file

@ -6,7 +6,7 @@ use hir::{Documentation, HasAttrs};
use ide_db::{imports::insert_use::ImportScope, ty_filter::TryEnum, SnippetCap}; use ide_db::{imports::insert_use::ImportScope, ty_filter::TryEnum, SnippetCap};
use syntax::{ use syntax::{
ast::{self, make, AstNode, AstToken}, 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, TextRange, TextSize,
}; };
use text_edit::TextEdit; 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("ref", "&expr", &format!("&{receiver_text}")).add_to(acc);
postfix_snippet("refm", "&mut expr", &format!("&mut {receiver_text}")).add_to(acc); postfix_snippet("refm", "&mut expr", &format!("&mut {receiver_text}")).add_to(acc);
let unsafe_completion_string = match dot_receiver { let mut unsafe_should_be_wrapped = true;
ast::Expr::BlockExpr(_) => format!("unsafe {receiver_text}"), if dot_receiver.syntax().kind() == BLOCK_EXPR {
_ => format!("unsafe {{ {receiver_text} }}"), 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); postfix_snippet("unsafe", "unsafe {}", &unsafe_completion_string).add_to(acc);