1
- use ra_syntax:: ast:: { self , AstNode } ;
1
+ use ra_syntax:: ast:: { self , make , AstNode } ;
2
2
use ra_syntax:: T ;
3
3
4
4
use crate :: { Assist , AssistCtx , AssistId } ;
@@ -35,8 +35,8 @@ pub(crate) fn invert_if(ctx: AssistCtx) -> Option<Assist> {
35
35
let then_node = expr. then_branch ( ) ?. syntax ( ) . clone ( ) ;
36
36
37
37
if let ast:: ElseBranch :: Block ( else_block) = expr. else_branch ( ) ? {
38
- let flip_cond = invert_boolean_expression ( & cond) ?;
39
38
let cond_range = cond. syntax ( ) . text_range ( ) ;
39
+ let flip_cond = invert_boolean_expression ( cond) ;
40
40
let else_node = else_block. syntax ( ) ;
41
41
let else_range = else_node. text_range ( ) ;
42
42
let then_range = then_node. text_range ( ) ;
@@ -51,16 +51,23 @@ pub(crate) fn invert_if(ctx: AssistCtx) -> Option<Assist> {
51
51
None
52
52
}
53
53
54
- pub ( crate ) fn invert_boolean_expression ( expr : & ast:: Expr ) -> Option < ast:: Expr > {
54
+ pub ( crate ) fn invert_boolean_expression ( expr : ast:: Expr ) -> ast:: Expr {
55
+ if let Some ( expr) = invert_special_case ( & expr) {
56
+ return expr;
57
+ }
58
+ make:: expr_prefix ( T ! [ !] , expr)
59
+ }
60
+
61
+ pub ( crate ) fn invert_special_case ( expr : & ast:: Expr ) -> Option < ast:: Expr > {
55
62
match expr {
56
63
ast:: Expr :: BinExpr ( bin) => match bin. op_kind ( ) ? {
57
64
ast:: BinOp :: NegatedEqualityTest => bin. replace_op ( T ! [ ==] ) . map ( |it| it. into ( ) ) ,
65
+ ast:: BinOp :: EqualityTest => bin. replace_op ( T ! [ !=] ) . map ( |it| it. into ( ) ) ,
58
66
_ => None ,
59
67
} ,
60
- ast:: Expr :: PrefixExpr ( pe) => match pe. op_kind ( ) ? {
61
- ast:: PrefixOp :: Not => pe. expr ( ) ,
62
- _ => None ,
63
- } ,
68
+ ast:: Expr :: PrefixExpr ( pe) if pe. op_kind ( ) ? == ast:: PrefixOp :: Not => pe. expr ( ) ,
69
+ // FIXME:
70
+ // ast::Expr::Literal(true | false )
64
71
_ => None ,
65
72
}
66
73
}
@@ -90,12 +97,16 @@ mod tests {
90
97
}
91
98
92
99
#[ test]
93
- fn invert_if_doesnt_apply_with_cursor_not_on_if ( ) {
94
- check_assist_not_applicable ( invert_if, "fn f() { if !<|>cond { 3 * 2 } else { 1 } }" )
100
+ fn invert_if_general_case ( ) {
101
+ check_assist (
102
+ invert_if,
103
+ "fn f() { i<|>f cond { 3 * 2 } else { 1 } }" ,
104
+ "fn f() { i<|>f !cond { 1 } else { 3 * 2 } }" ,
105
+ )
95
106
}
96
107
97
108
#[ test]
98
- fn invert_if_doesnt_apply_without_negated ( ) {
99
- check_assist_not_applicable ( invert_if, "fn f() { i <|>f cond { 3 * 2 } else { 1 } }" )
109
+ fn invert_if_doesnt_apply_with_cursor_not_on_if ( ) {
110
+ check_assist_not_applicable ( invert_if, "fn f() { if ! <|>cond { 3 * 2 } else { 1 } }" )
100
111
}
101
112
}
0 commit comments