@@ -24,38 +24,38 @@ use ra_syntax::{ast, AstNode, TextRange, T};
24
24
pub ( crate ) fn unwrap_block ( ctx : AssistCtx ) -> Option < Assist > {
25
25
let l_curly_token = ctx. find_token_at_offset ( T ! [ '{' ] ) ?;
26
26
27
- let res = if let Some ( if_expr) = l_curly_token. ancestors ( ) . find_map ( IfExpr :: cast) {
27
+ let ( expr, expr_to_unwrap) = if let Some ( if_expr) =
28
+ l_curly_token. ancestors ( ) . find_map ( IfExpr :: cast)
29
+ {
28
30
// if expression
29
31
let expr_to_unwrap = if_expr. blocks ( ) . find_map ( |expr| extract_expr ( ctx. frange . range , expr) ) ;
30
32
let expr_to_unwrap = expr_to_unwrap?;
31
33
// Find if we are in a else if block
32
34
let ancestor = if_expr. syntax ( ) . ancestors ( ) . skip ( 1 ) . find_map ( ast:: IfExpr :: cast) ;
33
35
34
- if let Some ( ancestor) = ancestor {
35
- Some ( ( ast:: Expr :: IfExpr ( ancestor) , expr_to_unwrap) )
36
- } else {
37
- Some ( ( ast:: Expr :: IfExpr ( if_expr) , expr_to_unwrap) )
36
+ match ancestor {
37
+ None => ( ast:: Expr :: IfExpr ( if_expr) , expr_to_unwrap) ,
38
+ Some ( ancestor) => ( ast:: Expr :: IfExpr ( ancestor) , expr_to_unwrap) ,
38
39
}
39
40
} else if let Some ( for_expr) = l_curly_token. ancestors ( ) . find_map ( ForExpr :: cast) {
40
41
// for expression
41
42
let block_expr = for_expr. loop_body ( ) ?;
42
- extract_expr ( ctx. frange . range , block_expr)
43
- . map ( |expr_to_unwrap| ( ast:: Expr :: ForExpr ( for_expr) , expr_to_unwrap) )
43
+ let expr_to_unwrap = extract_expr ( ctx. frange . range , block_expr) ? ;
44
+ ( ast:: Expr :: ForExpr ( for_expr) , expr_to_unwrap)
44
45
} else if let Some ( while_expr) = l_curly_token. ancestors ( ) . find_map ( WhileExpr :: cast) {
45
46
// while expression
46
47
let block_expr = while_expr. loop_body ( ) ?;
47
- extract_expr ( ctx. frange . range , block_expr)
48
- . map ( |expr_to_unwrap| ( ast:: Expr :: WhileExpr ( while_expr) , expr_to_unwrap) )
48
+ let expr_to_unwrap = extract_expr ( ctx. frange . range , block_expr) ? ;
49
+ ( ast:: Expr :: WhileExpr ( while_expr) , expr_to_unwrap)
49
50
} else if let Some ( loop_expr) = l_curly_token. ancestors ( ) . find_map ( LoopExpr :: cast) {
50
51
// loop expression
51
52
let block_expr = loop_expr. loop_body ( ) ?;
52
- extract_expr ( ctx. frange . range , block_expr)
53
- . map ( |expr_to_unwrap| ( ast:: Expr :: LoopExpr ( loop_expr) , expr_to_unwrap) )
53
+ let expr_to_unwrap = extract_expr ( ctx. frange . range , block_expr) ? ;
54
+ ( ast:: Expr :: LoopExpr ( loop_expr) , expr_to_unwrap)
54
55
} else {
55
- None
56
+ return None ;
56
57
} ;
57
58
58
- let ( expr, expr_to_unwrap) = res?;
59
59
ctx. add_assist ( AssistId ( "unwrap_block" ) , "Unwrap block" , |edit| {
60
60
edit. set_cursor ( expr. syntax ( ) . text_range ( ) . start ( ) ) ;
61
61
edit. target ( expr_to_unwrap. syntax ( ) . text_range ( ) ) ;
0 commit comments