@@ -40,7 +40,17 @@ pub(crate) fn merge_match_arms(ctx: AssistCtx<impl HirDatabase>) -> Option<Assis
40
40
}
41
41
let current_expr = current_arm. expr ( ) ?;
42
42
let current_text_range = current_arm. syntax ( ) . text_range ( ) ;
43
- let cursor_offset_back = current_text_range. end ( ) - ctx. frange . range . start ( ) ;
43
+
44
+ enum CursorPos {
45
+ InExpr ( TextUnit ) ,
46
+ InPat ( TextUnit ) ,
47
+ }
48
+ let cursor_pos = ctx. frange . range . start ( ) ;
49
+ let cursor_pos = if current_expr. syntax ( ) . text_range ( ) . contains ( cursor_pos) {
50
+ CursorPos :: InExpr ( current_text_range. end ( ) - cursor_pos)
51
+ } else {
52
+ CursorPos :: InPat ( cursor_pos)
53
+ } ;
44
54
45
55
// We check if the following match arms match this one. We could, but don't,
46
56
// compare to the previous match arm as well.
@@ -78,7 +88,10 @@ pub(crate) fn merge_match_arms(ctx: AssistCtx<impl HirDatabase>) -> Option<Assis
78
88
let end = arms_to_merge. last ( ) . unwrap ( ) . syntax ( ) . text_range ( ) . end ( ) ;
79
89
80
90
edit. target ( current_text_range) ;
81
- edit. set_cursor ( start + TextUnit :: from_usize ( arm. len ( ) ) - cursor_offset_back) ;
91
+ edit. set_cursor ( match cursor_pos {
92
+ CursorPos :: InExpr ( back_offset) => start + TextUnit :: from_usize ( arm. len ( ) ) - back_offset,
93
+ CursorPos :: InPat ( offset) => offset,
94
+ } ) ;
82
95
edit. replace ( TextRange :: from_to ( start, end) , arm) ;
83
96
} )
84
97
}
@@ -204,7 +217,7 @@ mod tests {
204
217
205
218
fn main() {
206
219
match X::A {
207
- X::A =><| > 92,
220
+ X::A<|> = > 92,
208
221
X::B => 92,
209
222
X::C => 92,
210
223
X::D => 62,
@@ -217,7 +230,7 @@ mod tests {
217
230
218
231
fn main() {
219
232
match X::A {
220
- X::A | X::B | X::C =><| > 92,
233
+ X::A<|> | X::B | X::C => 92,
221
234
X::D => 62,
222
235
_ => panic!(),
223
236
}
0 commit comments