**Unwrap Block** supports stand-alone blocks

This commit is contained in:
Aleksey Kladov 2020-11-17 14:08:31 +01:00
parent 10fa9c595a
commit 8c6f933773

View File

@ -3,7 +3,7 @@ use syntax::{
self,
edit::{AstNodeEdit, IndentLevel},
},
AstNode, TextRange, T,
AstNode, SyntaxKind, TextRange, T,
};
use crate::{utils::unwrap_trivial_block, AssistContext, AssistId, AssistKind, Assists};
@ -37,6 +37,15 @@ pub(crate) fn unwrap_block(acc: &mut Assists, ctx: &AssistContext) -> Option<()>
parent = parent.ancestors().find(|it| ast::MatchExpr::can_cast(it.kind()))?
}
if matches!(parent.kind(), SyntaxKind::BLOCK_EXPR | SyntaxKind::EXPR_STMT) {
return acc.add(assist_id, assist_label, target, |builder| {
builder.replace(
block.syntax().text_range(),
update_expr_string(block.to_string(), &[' ', '{', '\n']),
);
});
}
let parent = ast::Expr::cast(parent)?;
match parent.clone() {
@ -109,6 +118,64 @@ mod tests {
use super::*;
#[test]
fn unwrap_tail_expr_block() {
check_assist(
unwrap_block,
r#"
fn main() {
<|>{
92
}
}
"#,
r#"
fn main() {
92
}
"#,
)
}
#[test]
fn unwrap_stmt_expr_block() {
check_assist(
unwrap_block,
r#"
fn main() {
<|>{
92;
}
()
}
"#,
r#"
fn main() {
92;
()
}
"#,
);
// Pedantically, we should add an `;` here...
check_assist(
unwrap_block,
r#"
fn main() {
<|>{
92
}
()
}
"#,
r#"
fn main() {
92
()
}
"#,
);
}
#[test]
fn simple_if() {
check_assist(