diff options
Diffstat (limited to 'crates/ra_assists/src/handlers/move_guard.rs')
-rw-r--r-- | crates/ra_assists/src/handlers/move_guard.rs | 33 |
1 files changed, 11 insertions, 22 deletions
diff --git a/crates/ra_assists/src/handlers/move_guard.rs b/crates/ra_assists/src/handlers/move_guard.rs index fc0335b57..7edcf0748 100644 --- a/crates/ra_assists/src/handlers/move_guard.rs +++ b/crates/ra_assists/src/handlers/move_guard.rs | |||
@@ -1,7 +1,6 @@ | |||
1 | use ra_syntax::{ | 1 | use ra_syntax::{ |
2 | ast, | 2 | ast::{AstNode, IfExpr, MatchArm}, |
3 | ast::{AstNode, AstToken, IfExpr, MatchArm}, | 3 | SyntaxKind::WHITESPACE, |
4 | TextSize, | ||
5 | }; | 4 | }; |
6 | 5 | ||
7 | use crate::{AssistContext, AssistId, Assists}; | 6 | use crate::{AssistContext, AssistId, Assists}; |
@@ -42,24 +41,15 @@ pub(crate) fn move_guard_to_arm_body(acc: &mut Assists, ctx: &AssistContext) -> | |||
42 | 41 | ||
43 | let target = guard.syntax().text_range(); | 42 | let target = guard.syntax().text_range(); |
44 | acc.add(AssistId("move_guard_to_arm_body"), "Move guard to arm body", target, |edit| { | 43 | acc.add(AssistId("move_guard_to_arm_body"), "Move guard to arm body", target, |edit| { |
45 | let offseting_amount = match space_before_guard.and_then(|it| it.into_token()) { | 44 | match space_before_guard { |
46 | Some(tok) => { | 45 | Some(element) if element.kind() == WHITESPACE => { |
47 | if ast::Whitespace::cast(tok.clone()).is_some() { | 46 | edit.delete(element.text_range()); |
48 | let ele = tok.text_range(); | ||
49 | edit.delete(ele); | ||
50 | ele.len() | ||
51 | } else { | ||
52 | TextSize::from(0) | ||
53 | } | ||
54 | } | 47 | } |
55 | _ => TextSize::from(0), | 48 | _ => (), |
56 | }; | 49 | }; |
57 | 50 | ||
58 | edit.delete(guard.syntax().text_range()); | 51 | edit.delete(guard.syntax().text_range()); |
59 | edit.replace_node_and_indent(arm_expr.syntax(), buf); | 52 | edit.replace_node_and_indent(arm_expr.syntax(), buf); |
60 | edit.set_cursor( | ||
61 | arm_expr.syntax().text_range().start() + TextSize::from(3) - offseting_amount, | ||
62 | ); | ||
63 | }) | 53 | }) |
64 | } | 54 | } |
65 | 55 | ||
@@ -124,7 +114,6 @@ pub(crate) fn move_arm_cond_to_match_guard(acc: &mut Assists, ctx: &AssistContex | |||
124 | } | 114 | } |
125 | 115 | ||
126 | edit.insert(match_pat.syntax().text_range().end(), buf); | 116 | edit.insert(match_pat.syntax().text_range().end(), buf); |
127 | edit.set_cursor(match_pat.syntax().text_range().end() + TextSize::from(1)); | ||
128 | }, | 117 | }, |
129 | ) | 118 | ) |
130 | } | 119 | } |
@@ -172,7 +161,7 @@ mod tests { | |||
172 | let t = 'a'; | 161 | let t = 'a'; |
173 | let chars = "abcd"; | 162 | let chars = "abcd"; |
174 | match t { | 163 | match t { |
175 | '\r' => if chars.clone().next() == Some('\n') { <|>false }, | 164 | '\r' => if chars.clone().next() == Some('\n') { false }, |
176 | _ => true | 165 | _ => true |
177 | } | 166 | } |
178 | } | 167 | } |
@@ -195,7 +184,7 @@ mod tests { | |||
195 | r#" | 184 | r#" |
196 | fn f() { | 185 | fn f() { |
197 | match x { | 186 | match x { |
198 | y @ 4 | y @ 5 => if y > 5 { <|>true }, | 187 | y @ 4 | y @ 5 => if y > 5 { true }, |
199 | _ => false | 188 | _ => false |
200 | } | 189 | } |
201 | } | 190 | } |
@@ -222,7 +211,7 @@ mod tests { | |||
222 | let t = 'a'; | 211 | let t = 'a'; |
223 | let chars = "abcd"; | 212 | let chars = "abcd"; |
224 | match t { | 213 | match t { |
225 | '\r' <|>if chars.clone().next() == Some('\n') => false, | 214 | '\r' if chars.clone().next() == Some('\n') => false, |
226 | _ => true | 215 | _ => true |
227 | } | 216 | } |
228 | } | 217 | } |
@@ -266,7 +255,7 @@ mod tests { | |||
266 | let t = 'a'; | 255 | let t = 'a'; |
267 | let chars = "abcd"; | 256 | let chars = "abcd"; |
268 | match t { | 257 | match t { |
269 | '\r' <|>if chars.clone().next().is_some() => { }, | 258 | '\r' if chars.clone().next().is_some() => { }, |
270 | _ => true | 259 | _ => true |
271 | } | 260 | } |
272 | } | 261 | } |
@@ -296,7 +285,7 @@ mod tests { | |||
296 | let mut t = 'a'; | 285 | let mut t = 'a'; |
297 | let chars = "abcd"; | 286 | let chars = "abcd"; |
298 | match t { | 287 | match t { |
299 | '\r' <|>if chars.clone().next().is_some() => { | 288 | '\r' if chars.clone().next().is_some() => { |
300 | t = 'e'; | 289 | t = 'e'; |
301 | false | 290 | false |
302 | }, | 291 | }, |