1
1
use ra_fmt:: unwrap_trivial_block;
2
- use ra_syntax:: { ast, AstNode , TextRange , T } ;
2
+ use ra_syntax:: {
3
+ ast:: {
4
+ self ,
5
+ edit:: { AstNodeEdit , IndentLevel } ,
6
+ } ,
7
+ AstNode , TextRange , T ,
8
+ } ;
3
9
4
10
use crate :: { AssistContext , AssistId , Assists } ;
5
11
@@ -21,15 +27,21 @@ use crate::{AssistContext, AssistId, Assists};
21
27
// }
22
28
// ```
23
29
pub ( crate ) fn unwrap_block ( acc : & mut Assists , ctx : & AssistContext ) -> Option < ( ) > {
24
- let l_curly_token = ctx. find_token_at_offset ( T ! [ '{' ] ) ?;
25
- let block = ast:: BlockExpr :: cast ( l_curly_token. parent ( ) ) ?;
26
- let parent = block. syntax ( ) . parent ( ) ?;
27
30
let assist_id = AssistId ( "unwrap_block" ) ;
28
31
let assist_label = "Unwrap block" ;
32
+
33
+ let l_curly_token = ctx. find_token_at_offset ( T ! [ '{' ] ) ?;
34
+ let mut block = ast:: BlockExpr :: cast ( l_curly_token. parent ( ) ) ?;
35
+ let mut parent = block. syntax ( ) . parent ( ) ?;
36
+ if ast:: MatchArm :: can_cast ( parent. kind ( ) ) {
37
+ parent = parent. ancestors ( ) . find ( |it| ast:: MatchExpr :: can_cast ( it. kind ( ) ) ) ?
38
+ }
39
+
29
40
let parent = ast:: Expr :: cast ( parent) ?;
30
41
31
42
match parent. clone ( ) {
32
43
ast:: Expr :: ForExpr ( _) | ast:: Expr :: WhileExpr ( _) | ast:: Expr :: LoopExpr ( _) => ( ) ,
44
+ ast:: Expr :: MatchExpr ( _) => block = block. dedent ( IndentLevel ( 1 ) ) ,
33
45
ast:: Expr :: IfExpr ( if_expr) => {
34
46
let then_branch = if_expr. then_branch ( ) ?;
35
47
if then_branch == block {
@@ -459,6 +471,30 @@ mod tests {
459
471
) ;
460
472
}
461
473
474
+ #[ test]
475
+ fn unwrap_match_arm ( ) {
476
+ check_assist (
477
+ unwrap_block,
478
+ r#"
479
+ fn main() {
480
+ match rel_path {
481
+ Ok(rel_path) => {<|>
482
+ let rel_path = RelativePathBuf::from_path(rel_path).ok()?;
483
+ Some((*id, rel_path))
484
+ }
485
+ Err(_) => None,
486
+ }
487
+ }
488
+ "# ,
489
+ r#"
490
+ fn main() {
491
+ let rel_path = RelativePathBuf::from_path(rel_path).ok()?;
492
+ Some((*id, rel_path))
493
+ }
494
+ "# ,
495
+ ) ;
496
+ }
497
+
462
498
#[ test]
463
499
fn simple_if_in_while_bad_cursor_position ( ) {
464
500
check_assist_not_applicable (
0 commit comments