@@ -178,6 +178,19 @@ pub(crate) fn add_missing_match_arms(acc: &mut Assists, ctx: &AssistContext) ->
178
178
None => Cursor :: Before ( first_new_arm. syntax ( ) ) ,
179
179
} ;
180
180
let snippet = render_snippet ( cap, new_match_arm_list. syntax ( ) , cursor) ;
181
+ // remove the second last line if it only contains trailing whitespace
182
+ let lines = snippet. lines ( ) . collect_vec ( ) ;
183
+ let snippet = lines
184
+ . iter ( )
185
+ . enumerate ( )
186
+ . filter_map ( |( index, & line) | {
187
+ if index + 2 == lines. len ( ) && line. trim ( ) . is_empty ( ) {
188
+ return None ;
189
+ }
190
+ return Some ( line) ;
191
+ } )
192
+ . join ( "\n " ) ;
193
+
181
194
builder. replace_snippet ( cap, old_range, snippet) ;
182
195
}
183
196
_ => builder. replace ( old_range, new_match_arm_list. to_string ( ) ) ,
@@ -197,6 +210,19 @@ fn cursor_at_trivial_match_arm_list(
197
210
return Some ( ( ) ) ;
198
211
}
199
212
213
+ // match x {
214
+ // bar => baz,
215
+ // $0
216
+ // }
217
+ if let Some ( last_arm) = match_arm_list. arms ( ) . last ( ) {
218
+ let last_arm_range = last_arm. syntax ( ) . text_range ( ) ;
219
+ let match_expr_range = match_expr. syntax ( ) . text_range ( ) ;
220
+ if last_arm_range. end ( ) <= ctx. offset ( ) && ctx. offset ( ) < match_expr_range. end ( ) {
221
+ cov_mark:: hit!( add_missing_match_arms_end_of_last_arm) ;
222
+ return Some ( ( ) ) ;
223
+ }
224
+ }
225
+
200
226
// match { _$0 => {...} }
201
227
let wild_pat = ctx. find_node_at_offset_with_descend :: < ast:: WildcardPat > ( ) ?;
202
228
let arm = wild_pat. syntax ( ) . parent ( ) . and_then ( ast:: MatchArm :: cast) ?;
@@ -676,6 +702,42 @@ fn main() {
676
702
) ;
677
703
}
678
704
705
+ #[ test]
706
+ fn add_missing_match_arms_end_of_last_arm ( ) {
707
+ cov_mark:: check!( add_missing_match_arms_end_of_last_arm) ;
708
+ check_assist (
709
+ add_missing_match_arms,
710
+ r#"
711
+ enum A { One, Two }
712
+ enum B { One, Two }
713
+
714
+ fn main() {
715
+ let a = A::One;
716
+ let b = B::One;
717
+ match (a, b) {
718
+ (A::Two, B::One) => {},
719
+ $0
720
+ }
721
+ }
722
+ "# ,
723
+ r#"
724
+ enum A { One, Two }
725
+ enum B { One, Two }
726
+
727
+ fn main() {
728
+ let a = A::One;
729
+ let b = B::One;
730
+ match (a, b) {
731
+ (A::Two, B::One) => {},
732
+ $0(A::One, B::One) => todo!(),
733
+ (A::One, B::Two) => todo!(),
734
+ (A::Two, B::Two) => todo!(),
735
+ }
736
+ }
737
+ "# ,
738
+ ) ;
739
+ }
740
+
679
741
#[ test]
680
742
fn add_missing_match_arms_tuple_of_enum ( ) {
681
743
check_assist (
0 commit comments