@@ -3,7 +3,7 @@ use syntax::{
33 self ,
44 edit:: { AstNodeEdit , IndentLevel } ,
55 } ,
6- AstNode , TextRange , T ,
6+ AstNode , SyntaxKind , TextRange , T ,
77} ;
88
99use crate :: { utils:: unwrap_trivial_block, AssistContext , AssistId , AssistKind , Assists } ;
@@ -31,11 +31,21 @@ pub(crate) fn unwrap_block(acc: &mut Assists, ctx: &AssistContext) -> Option<()>
3131
3232 let l_curly_token = ctx. find_token_syntax_at_offset ( T ! [ '{' ] ) ?;
3333 let mut block = ast:: BlockExpr :: cast ( l_curly_token. parent ( ) ) ?;
34+ let target = block. syntax ( ) . text_range ( ) ;
3435 let mut parent = block. syntax ( ) . parent ( ) ?;
3536 if ast:: MatchArm :: can_cast ( parent. kind ( ) ) {
3637 parent = parent. ancestors ( ) . find ( |it| ast:: MatchExpr :: can_cast ( it. kind ( ) ) ) ?
3738 }
3839
40+ if matches ! ( parent. kind( ) , SyntaxKind :: BLOCK_EXPR | SyntaxKind :: EXPR_STMT ) {
41+ return acc. add ( assist_id, assist_label, target, |builder| {
42+ builder. replace (
43+ block. syntax ( ) . text_range ( ) ,
44+ update_expr_string ( block. to_string ( ) , & [ ' ' , '{' , '\n' ] ) ,
45+ ) ;
46+ } ) ;
47+ }
48+
3949 let parent = ast:: Expr :: cast ( parent) ?;
4050
4151 match parent. clone ( ) {
@@ -48,7 +58,6 @@ pub(crate) fn unwrap_block(acc: &mut Assists, ctx: &AssistContext) -> Option<()>
4858 // For `else if` blocks
4959 let ancestor_then_branch = ancestor. then_branch ( ) ?;
5060
51- let target = then_branch. syntax ( ) . text_range ( ) ;
5261 return acc. add ( assist_id, assist_label, target, |edit| {
5362 let range_to_del_else_if = TextRange :: new (
5463 ancestor_then_branch. syntax ( ) . text_range ( ) . end ( ) ,
@@ -68,7 +77,6 @@ pub(crate) fn unwrap_block(acc: &mut Assists, ctx: &AssistContext) -> Option<()>
6877 } ) ;
6978 }
7079 } else {
71- let target = block. syntax ( ) . text_range ( ) ;
7280 return acc. add ( assist_id, assist_label, target, |edit| {
7381 let range_to_del = TextRange :: new (
7482 then_branch. syntax ( ) . text_range ( ) . end ( ) ,
@@ -84,7 +92,6 @@ pub(crate) fn unwrap_block(acc: &mut Assists, ctx: &AssistContext) -> Option<()>
8492 } ;
8593
8694 let unwrapped = unwrap_trivial_block ( block) ;
87- let target = unwrapped. syntax ( ) . text_range ( ) ;
8895 acc. add ( assist_id, assist_label, target, |builder| {
8996 builder. replace (
9097 parent. syntax ( ) . text_range ( ) ,
@@ -111,6 +118,64 @@ mod tests {
111118
112119 use super :: * ;
113120
121+ #[ test]
122+ fn unwrap_tail_expr_block ( ) {
123+ check_assist (
124+ unwrap_block,
125+ r#"
126+ fn main() {
127+ <|>{
128+ 92
129+ }
130+ }
131+ "# ,
132+ r#"
133+ fn main() {
134+ 92
135+ }
136+ "# ,
137+ )
138+ }
139+
140+ #[ test]
141+ fn unwrap_stmt_expr_block ( ) {
142+ check_assist (
143+ unwrap_block,
144+ r#"
145+ fn main() {
146+ <|>{
147+ 92;
148+ }
149+ ()
150+ }
151+ "# ,
152+ r#"
153+ fn main() {
154+ 92;
155+ ()
156+ }
157+ "# ,
158+ ) ;
159+ // Pedantically, we should add an `;` here...
160+ check_assist (
161+ unwrap_block,
162+ r#"
163+ fn main() {
164+ <|>{
165+ 92
166+ }
167+ ()
168+ }
169+ "# ,
170+ r#"
171+ fn main() {
172+ 92
173+ ()
174+ }
175+ "# ,
176+ ) ;
177+ }
178+
114179 #[ test]
115180 fn simple_if ( ) {
116181 check_assist (
0 commit comments