diff options
author | Aleksey Kladov <[email protected]> | 2020-02-05 12:41:43 +0000 |
---|---|---|
committer | Aleksey Kladov <[email protected]> | 2020-02-05 12:41:43 +0000 |
commit | f756d5da063461c99c20266dc4896d8388fe28b9 (patch) | |
tree | 3f3e74630dadfc8ee45fc49386152a4c0d5e9cd6 /crates/ra_assists/src/assists | |
parent | 28acd01c638590a64a06148021ae60c0ccef8bb6 (diff) |
Better cursor placement when merging arms
Diffstat (limited to 'crates/ra_assists/src/assists')
-rw-r--r-- | crates/ra_assists/src/assists/merge_match_arms.rs | 21 |
1 files changed, 17 insertions, 4 deletions
diff --git a/crates/ra_assists/src/assists/merge_match_arms.rs b/crates/ra_assists/src/assists/merge_match_arms.rs index 8af30866c..64c9379da 100644 --- a/crates/ra_assists/src/assists/merge_match_arms.rs +++ b/crates/ra_assists/src/assists/merge_match_arms.rs | |||
@@ -40,7 +40,17 @@ pub(crate) fn merge_match_arms(ctx: AssistCtx<impl HirDatabase>) -> Option<Assis | |||
40 | } | 40 | } |
41 | let current_expr = current_arm.expr()?; | 41 | let current_expr = current_arm.expr()?; |
42 | let current_text_range = current_arm.syntax().text_range(); | 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 | // We check if the following match arms match this one. We could, but don't, | 55 | // We check if the following match arms match this one. We could, but don't, |
46 | // compare to the previous match arm as well. | 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 | let end = arms_to_merge.last().unwrap().syntax().text_range().end(); | 88 | let end = arms_to_merge.last().unwrap().syntax().text_range().end(); |
79 | 89 | ||
80 | edit.target(current_text_range); | 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 | edit.replace(TextRange::from_to(start, end), arm); | 95 | edit.replace(TextRange::from_to(start, end), arm); |
83 | }) | 96 | }) |
84 | } | 97 | } |
@@ -204,7 +217,7 @@ mod tests { | |||
204 | 217 | ||
205 | fn main() { | 218 | fn main() { |
206 | match X::A { | 219 | match X::A { |
207 | X::A =><|> 92, | 220 | X::A<|> => 92, |
208 | X::B => 92, | 221 | X::B => 92, |
209 | X::C => 92, | 222 | X::C => 92, |
210 | X::D => 62, | 223 | X::D => 62, |
@@ -217,7 +230,7 @@ mod tests { | |||
217 | 230 | ||
218 | fn main() { | 231 | fn main() { |
219 | match X::A { | 232 | match X::A { |
220 | X::A | X::B | X::C =><|> 92, | 233 | X::A<|> | X::B | X::C => 92, |
221 | X::D => 62, | 234 | X::D => 62, |
222 | _ => panic!(), | 235 | _ => panic!(), |
223 | } | 236 | } |