Skip to content

Commit f756d5d

Browse files
committed
Better cursor placement when merging arms
1 parent 28acd01 commit f756d5d

File tree

1 file changed

+17
-4
lines changed

1 file changed

+17
-4
lines changed

crates/ra_assists/src/assists/merge_match_arms.rs

Lines changed: 17 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,17 @@ pub(crate) fn merge_match_arms(ctx: AssistCtx<impl HirDatabase>) -> Option<Assis
4040
}
4141
let current_expr = current_arm.expr()?;
4242
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+
};
4454

4555
// We check if the following match arms match this one. We could, but don't,
4656
// compare to the previous match arm as well.
@@ -78,7 +88,10 @@ pub(crate) fn merge_match_arms(ctx: AssistCtx<impl HirDatabase>) -> Option<Assis
7888
let end = arms_to_merge.last().unwrap().syntax().text_range().end();
7989

8090
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+
});
8295
edit.replace(TextRange::from_to(start, end), arm);
8396
})
8497
}
@@ -204,7 +217,7 @@ mod tests {
204217
205218
fn main() {
206219
match X::A {
207-
X::A =><|> 92,
220+
X::A<|> => 92,
208221
X::B => 92,
209222
X::C => 92,
210223
X::D => 62,
@@ -217,7 +230,7 @@ mod tests {
217230
218231
fn main() {
219232
match X::A {
220-
X::A | X::B | X::C =><|> 92,
233+
X::A<|> | X::B | X::C => 92,
221234
X::D => 62,
222235
_ => panic!(),
223236
}

0 commit comments

Comments
 (0)