From 8c6f933773df872a394fdcae890b9c0c42bdbc6c Mon Sep 17 00:00:00 2001 From: Aleksey Kladov Date: Tue, 17 Nov 2020 14:08:31 +0100 Subject: **Unwrap Block** supports stand-alone blocks --- crates/assists/src/handlers/unwrap_block.rs | 69 ++++++++++++++++++++++++++++- 1 file changed, 68 insertions(+), 1 deletion(-) (limited to 'crates') diff --git a/crates/assists/src/handlers/unwrap_block.rs b/crates/assists/src/handlers/unwrap_block.rs index a3ef33520..496714243 100644 --- a/crates/assists/src/handlers/unwrap_block.rs +++ b/crates/assists/src/handlers/unwrap_block.rs @@ -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( -- cgit v1.2.3