diff options
author | Galilée 'Bill' Enguehard <[email protected]> | 2020-05-21 22:27:38 +0100 |
---|---|---|
committer | Galilée 'Bill' Enguehard <[email protected]> | 2020-05-21 22:27:38 +0100 |
commit | 7fece3bdd2450c0807f7dd742239cae95f0cc65e (patch) | |
tree | 866c4db826c959e79c63a6727bdb9f2c61e6fc4f /crates/ra_assists/src/handlers/merge_match_arms.rs | |
parent | db926218b2082077750291f8426ddd28b284cd08 (diff) | |
parent | 59732df8d40dfadc6dcf5951265416576399712a (diff) |
Merge branch 'master' of github.com:rust-analyzer/rust-analyzer into modname_spacing
Diffstat (limited to 'crates/ra_assists/src/handlers/merge_match_arms.rs')
-rw-r--r-- | crates/ra_assists/src/handlers/merge_match_arms.rs | 34 |
1 files changed, 9 insertions, 25 deletions
diff --git a/crates/ra_assists/src/handlers/merge_match_arms.rs b/crates/ra_assists/src/handlers/merge_match_arms.rs index 5a77d3dbc..ca04ec671 100644 --- a/crates/ra_assists/src/handlers/merge_match_arms.rs +++ b/crates/ra_assists/src/handlers/merge_match_arms.rs | |||
@@ -3,10 +3,10 @@ use std::iter::successors; | |||
3 | use ra_syntax::{ | 3 | use ra_syntax::{ |
4 | algo::neighbor, | 4 | algo::neighbor, |
5 | ast::{self, AstNode}, | 5 | ast::{self, AstNode}, |
6 | Direction, TextSize, | 6 | Direction, |
7 | }; | 7 | }; |
8 | 8 | ||
9 | use crate::{Assist, AssistCtx, AssistId, TextRange}; | 9 | use crate::{AssistContext, AssistId, Assists, TextRange}; |
10 | 10 | ||
11 | // Assist: merge_match_arms | 11 | // Assist: merge_match_arms |
12 | // | 12 | // |
@@ -32,7 +32,7 @@ use crate::{Assist, AssistCtx, AssistId, TextRange}; | |||
32 | // } | 32 | // } |
33 | // } | 33 | // } |
34 | // ``` | 34 | // ``` |
35 | pub(crate) fn merge_match_arms(ctx: AssistCtx) -> Option<Assist> { | 35 | pub(crate) fn merge_match_arms(acc: &mut Assists, ctx: &AssistContext) -> Option<()> { |
36 | let current_arm = ctx.find_node_at_offset::<ast::MatchArm>()?; | 36 | let current_arm = ctx.find_node_at_offset::<ast::MatchArm>()?; |
37 | // Don't try to handle arms with guards for now - can add support for this later | 37 | // Don't try to handle arms with guards for now - can add support for this later |
38 | if current_arm.guard().is_some() { | 38 | if current_arm.guard().is_some() { |
@@ -41,17 +41,6 @@ pub(crate) fn merge_match_arms(ctx: AssistCtx) -> Option<Assist> { | |||
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 | 43 | ||
44 | enum CursorPos { | ||
45 | InExpr(TextSize), | ||
46 | InPat(TextSize), | ||
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 | }; | ||
54 | |||
55 | // We check if the following match arms match this one. We could, but don't, | 44 | // We check if the following match arms match this one. We could, but don't, |
56 | // compare to the previous match arm as well. | 45 | // compare to the previous match arm as well. |
57 | let arms_to_merge = successors(Some(current_arm), |it| neighbor(it, Direction::Next)) | 46 | let arms_to_merge = successors(Some(current_arm), |it| neighbor(it, Direction::Next)) |
@@ -70,7 +59,7 @@ pub(crate) fn merge_match_arms(ctx: AssistCtx) -> Option<Assist> { | |||
70 | return None; | 59 | return None; |
71 | } | 60 | } |
72 | 61 | ||
73 | ctx.add_assist(AssistId("merge_match_arms"), "Merge match arms", |edit| { | 62 | acc.add(AssistId("merge_match_arms"), "Merge match arms", current_text_range, |edit| { |
74 | let pats = if arms_to_merge.iter().any(contains_placeholder) { | 63 | let pats = if arms_to_merge.iter().any(contains_placeholder) { |
75 | "_".into() | 64 | "_".into() |
76 | } else { | 65 | } else { |
@@ -87,11 +76,6 @@ pub(crate) fn merge_match_arms(ctx: AssistCtx) -> Option<Assist> { | |||
87 | let start = arms_to_merge.first().unwrap().syntax().text_range().start(); | 76 | let start = arms_to_merge.first().unwrap().syntax().text_range().start(); |
88 | let end = arms_to_merge.last().unwrap().syntax().text_range().end(); | 77 | let end = arms_to_merge.last().unwrap().syntax().text_range().end(); |
89 | 78 | ||
90 | edit.target(current_text_range); | ||
91 | edit.set_cursor(match cursor_pos { | ||
92 | CursorPos::InExpr(back_offset) => start + TextSize::of(&arm) - back_offset, | ||
93 | CursorPos::InPat(offset) => offset, | ||
94 | }); | ||
95 | edit.replace(TextRange::new(start, end), arm); | 79 | edit.replace(TextRange::new(start, end), arm); |
96 | }) | 80 | }) |
97 | } | 81 | } |
@@ -105,7 +89,7 @@ fn contains_placeholder(a: &ast::MatchArm) -> bool { | |||
105 | 89 | ||
106 | #[cfg(test)] | 90 | #[cfg(test)] |
107 | mod tests { | 91 | mod tests { |
108 | use crate::helpers::{check_assist, check_assist_not_applicable}; | 92 | use crate::tests::{check_assist, check_assist_not_applicable}; |
109 | 93 | ||
110 | use super::*; | 94 | use super::*; |
111 | 95 | ||
@@ -133,7 +117,7 @@ mod tests { | |||
133 | fn main() { | 117 | fn main() { |
134 | let x = X::A; | 118 | let x = X::A; |
135 | let y = match x { | 119 | let y = match x { |
136 | X::A | X::B => { 1i32<|> } | 120 | X::A | X::B => { 1i32 } |
137 | X::C => { 2i32 } | 121 | X::C => { 2i32 } |
138 | } | 122 | } |
139 | } | 123 | } |
@@ -165,7 +149,7 @@ mod tests { | |||
165 | fn main() { | 149 | fn main() { |
166 | let x = X::A; | 150 | let x = X::A; |
167 | let y = match x { | 151 | let y = match x { |
168 | X::A | X::B | X::C | X::D => {<|> 1i32 }, | 152 | X::A | X::B | X::C | X::D => { 1i32 }, |
169 | X::E => { 2i32 }, | 153 | X::E => { 2i32 }, |
170 | } | 154 | } |
171 | } | 155 | } |
@@ -198,7 +182,7 @@ mod tests { | |||
198 | let x = X::A; | 182 | let x = X::A; |
199 | let y = match x { | 183 | let y = match x { |
200 | X::A => { 1i32 }, | 184 | X::A => { 1i32 }, |
201 | _ => { 2i<|>32 } | 185 | _ => { 2i32 } |
202 | } | 186 | } |
203 | } | 187 | } |
204 | "#, | 188 | "#, |
@@ -227,7 +211,7 @@ mod tests { | |||
227 | 211 | ||
228 | fn main() { | 212 | fn main() { |
229 | match X::A { | 213 | match X::A { |
230 | X::A<|> | X::B | X::C => 92, | 214 | X::A | X::B | X::C => 92, |
231 | X::D => 62, | 215 | X::D => 62, |
232 | _ => panic!(), | 216 | _ => panic!(), |
233 | } | 217 | } |