@@ -5,7 +5,7 @@ use itertools::Itertools;
5
5
use syntax:: {
6
6
algo:: non_trivia_sibling,
7
7
ast:: { self , AstNode , AstToken , IsString } ,
8
- Direction , NodeOrToken , SourceFile ,
8
+ Direction , NodeOrToken , SourceFile , SyntaxElement ,
9
9
SyntaxKind :: { self , USE_TREE , WHITESPACE } ,
10
10
SyntaxNode , SyntaxToken , TextRange , TextSize , T ,
11
11
} ;
@@ -107,6 +107,7 @@ fn remove_newline(edit: &mut TextEditBuilder, token: &SyntaxToken, offset: TextS
107
107
edit. delete ( TextRange :: new ( prev. text_range ( ) . start ( ) , token. text_range ( ) . end ( ) ) ) ;
108
108
return ;
109
109
}
110
+
110
111
if prev. kind ( ) == T ! [ , ] && next. kind ( ) == T ! [ '}' ] {
111
112
// Removes: comma, newline (incl. surrounding whitespace)
112
113
let space = if let Some ( left) = prev. prev_sibling_or_token ( ) {
@@ -133,6 +134,17 @@ fn remove_newline(edit: &mut TextEditBuilder, token: &SyntaxToken, offset: TextS
133
134
return ;
134
135
}
135
136
137
+ if let ( Some ( prev) , Some ( _next) ) = ( as_if_expr ( & prev) , as_if_expr ( & next) ) {
138
+ match prev. else_token ( ) {
139
+ Some ( _) => cov_mark:: hit!( join_two_ifs_with_existing_else) ,
140
+ None => {
141
+ cov_mark:: hit!( join_two_ifs) ;
142
+ edit. replace ( token. text_range ( ) , " else " . to_string ( ) ) ;
143
+ return ;
144
+ }
145
+ }
146
+ }
147
+
136
148
// Special case that turns something like:
137
149
//
138
150
// ```
@@ -200,6 +212,14 @@ fn is_trailing_comma(left: SyntaxKind, right: SyntaxKind) -> bool {
200
212
matches ! ( ( left, right) , ( T ![ , ] , T ![ ')' ] | T ![ ']' ] ) )
201
213
}
202
214
215
+ fn as_if_expr ( element : & SyntaxElement ) -> Option < ast:: IfExpr > {
216
+ let mut node = element. as_node ( ) ?. clone ( ) ;
217
+ if let Some ( stmt) = ast:: ExprStmt :: cast ( node. clone ( ) ) {
218
+ node = stmt. expr ( ) ?. syntax ( ) . clone ( ) ;
219
+ }
220
+ ast:: IfExpr :: cast ( node)
221
+ }
222
+
203
223
fn compute_ws ( left : SyntaxKind , right : SyntaxKind ) -> & ' static str {
204
224
match left {
205
225
T ! [ '(' ] | T ! [ '[' ] => return "" ,
@@ -873,6 +893,7 @@ $0hello world
873
893
"# ,
874
894
) ;
875
895
}
896
+
876
897
#[ test]
877
898
fn join_last_line_empty ( ) {
878
899
check_join_lines (
@@ -881,6 +902,62 @@ fn main() {$0}
881
902
"# ,
882
903
r#"
883
904
fn main() {$0}
905
+ "# ,
906
+ ) ;
907
+ }
908
+
909
+ #[ test]
910
+ fn join_two_ifs ( ) {
911
+ cov_mark:: check!( join_two_ifs) ;
912
+ check_join_lines (
913
+ r#"
914
+ fn main() {
915
+ if foo {
916
+
917
+ }$0
918
+ if bar {
919
+
920
+ }
921
+ }
922
+ "# ,
923
+ r#"
924
+ fn main() {
925
+ if foo {
926
+
927
+ }$0 else if bar {
928
+
929
+ }
930
+ }
931
+ "# ,
932
+ ) ;
933
+ }
934
+
935
+ #[ test]
936
+ fn join_two_ifs_with_existing_else ( ) {
937
+ cov_mark:: check!( join_two_ifs_with_existing_else) ;
938
+ check_join_lines (
939
+ r#"
940
+ fn main() {
941
+ if foo {
942
+
943
+ } else {
944
+
945
+ }$0
946
+ if bar {
947
+
948
+ }
949
+ }
950
+ "# ,
951
+ r#"
952
+ fn main() {
953
+ if foo {
954
+
955
+ } else {
956
+
957
+ }$0 if bar {
958
+
959
+ }
960
+ }
884
961
"# ,
885
962
) ;
886
963
}
0 commit comments