From 8dd0b7d2b59cffda0aa788c5d3016fe75d31a2ff Mon Sep 17 00:00:00 2001 From: Aleksey Kladov Date: Tue, 17 Nov 2020 14:00:52 +0100 Subject: Assist target should point at *existing* code --- crates/assists/src/handlers/unwrap_block.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'crates/assists') diff --git a/crates/assists/src/handlers/unwrap_block.rs b/crates/assists/src/handlers/unwrap_block.rs index 36ef871b9..a82399aa8 100644 --- a/crates/assists/src/handlers/unwrap_block.rs +++ b/crates/assists/src/handlers/unwrap_block.rs @@ -83,8 +83,8 @@ pub(crate) fn unwrap_block(acc: &mut Assists, ctx: &AssistContext) -> Option<()> _ => return None, }; + let target = block.syntax().text_range(); let unwrapped = unwrap_trivial_block(block); - let target = unwrapped.syntax().text_range(); acc.add(assist_id, assist_label, target, |builder| { builder.replace( parent.syntax().text_range(), -- cgit v1.2.3 From 10fa9c595ab6cae7420eb879cabadc30db4d6d7e Mon Sep 17 00:00:00 2001 From: Aleksey Kladov Date: Tue, 17 Nov 2020 14:07:13 +0100 Subject: Simplify --- crates/assists/src/handlers/unwrap_block.rs | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) (limited to 'crates/assists') diff --git a/crates/assists/src/handlers/unwrap_block.rs b/crates/assists/src/handlers/unwrap_block.rs index a82399aa8..a3ef33520 100644 --- a/crates/assists/src/handlers/unwrap_block.rs +++ b/crates/assists/src/handlers/unwrap_block.rs @@ -31,6 +31,7 @@ pub(crate) fn unwrap_block(acc: &mut Assists, ctx: &AssistContext) -> Option<()> let l_curly_token = ctx.find_token_syntax_at_offset(T!['{'])?; let mut block = ast::BlockExpr::cast(l_curly_token.parent())?; + let target = block.syntax().text_range(); let mut parent = block.syntax().parent()?; if ast::MatchArm::can_cast(parent.kind()) { parent = parent.ancestors().find(|it| ast::MatchExpr::can_cast(it.kind()))? @@ -48,7 +49,6 @@ pub(crate) fn unwrap_block(acc: &mut Assists, ctx: &AssistContext) -> Option<()> // For `else if` blocks let ancestor_then_branch = ancestor.then_branch()?; - let target = then_branch.syntax().text_range(); return acc.add(assist_id, assist_label, target, |edit| { let range_to_del_else_if = TextRange::new( ancestor_then_branch.syntax().text_range().end(), @@ -68,7 +68,6 @@ pub(crate) fn unwrap_block(acc: &mut Assists, ctx: &AssistContext) -> Option<()> }); } } else { - let target = block.syntax().text_range(); return acc.add(assist_id, assist_label, target, |edit| { let range_to_del = TextRange::new( then_branch.syntax().text_range().end(), @@ -83,7 +82,6 @@ pub(crate) fn unwrap_block(acc: &mut Assists, ctx: &AssistContext) -> Option<()> _ => return None, }; - let target = block.syntax().text_range(); let unwrapped = unwrap_trivial_block(block); acc.add(assist_id, assist_label, target, |builder| { builder.replace( -- cgit v1.2.3 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/assists') 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