Skip to content

Commit a4416ac

Browse files
committed
add assist to move arm condition to match guard
1 parent 99ad614 commit a4416ac

File tree

3 files changed

+269
-117
lines changed

3 files changed

+269
-117
lines changed

crates/ra_assists/src/lib.rs

+3-2
Original file line numberDiff line numberDiff line change
@@ -100,7 +100,7 @@ mod split_import;
100100
mod remove_dbg;
101101
pub mod auto_import;
102102
mod add_missing_impl_members;
103-
mod move_guard_to_arm_body_and_back;
103+
mod move_guard;
104104

105105
fn all_assists<DB: HirDatabase>() -> &'static [fn(AssistCtx<DB>) -> Option<Assist>] {
106106
&[
@@ -119,7 +119,8 @@ fn all_assists<DB: HirDatabase>() -> &'static [fn(AssistCtx<DB>) -> Option<Assis
119119
add_missing_impl_members::add_missing_impl_members,
120120
add_missing_impl_members::add_missing_default_members,
121121
inline_local_variable::inline_local_varialbe,
122-
move_guard_to_arm_body_and_back::move_guard_to_arm_body,
122+
move_guard::move_guard_to_arm_body,
123+
move_guard::move_arm_cond_to_match_guard,
123124
]
124125
}
125126

crates/ra_assists/src/move_guard.rs

+266
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,266 @@
1+
use hir::db::HirDatabase;
2+
use ra_syntax::{
3+
TextUnit,
4+
SyntaxElement,
5+
ast::{MatchArm, AstNode, AstToken, IfExpr},
6+
ast,
7+
};
8+
9+
use crate::{AssistCtx, Assist, AssistId};
10+
11+
pub(crate) fn move_guard_to_arm_body(mut ctx: AssistCtx<impl HirDatabase>) -> Option<Assist> {
12+
let match_arm = ctx.node_at_offset::<MatchArm>()?;
13+
let guard = match_arm.guard()?;
14+
let space_before_guard = guard.syntax().prev_sibling_or_token();
15+
16+
let guard_conditions = guard.expr()?;
17+
let arm_expr = match_arm.expr()?;
18+
let buf = format!("if {} {{ {} }}", guard_conditions.syntax().text(), arm_expr.syntax().text());
19+
20+
ctx.add_action(AssistId("move_guard_to_arm_body"), "move guard to arm body", |edit| {
21+
edit.target(guard.syntax().range());
22+
let offseting_amount = match space_before_guard {
23+
Some(SyntaxElement::Token(tok)) => {
24+
if let Some(_) = ast::Whitespace::cast(tok) {
25+
let ele = space_before_guard.unwrap().range();
26+
edit.delete(ele);
27+
ele.len()
28+
} else {
29+
TextUnit::from(0)
30+
}
31+
}
32+
_ => TextUnit::from(0),
33+
};
34+
35+
edit.delete(guard.syntax().range());
36+
edit.replace_node_and_indent(arm_expr.syntax(), buf);
37+
edit.set_cursor(arm_expr.syntax().range().start() + TextUnit::from(3) - offseting_amount);
38+
});
39+
ctx.build()
40+
}
41+
42+
pub(crate) fn move_arm_cond_to_match_guard(mut ctx: AssistCtx<impl HirDatabase>) -> Option<Assist> {
43+
let match_arm: &MatchArm = ctx.node_at_offset::<MatchArm>()?;
44+
let last_match_pat = match_arm.pats().last()?;
45+
46+
let arm_body = match_arm.expr()?;
47+
let if_expr: &IfExpr = IfExpr::cast(arm_body.syntax())?;
48+
let cond = if_expr.condition()?;
49+
let then_block = if_expr.then_branch()?;
50+
51+
// Not support if with else branch
52+
if let Some(_) = if_expr.else_branch() {
53+
return None;
54+
}
55+
56+
let buf = format!(" if {}", cond.syntax().text());
57+
58+
ctx.add_action(
59+
AssistId("move_arm_cond_to_match_guard"),
60+
"move condition to match guard",
61+
|edit| {
62+
edit.target(if_expr.syntax().range());
63+
let then_only_expr = then_block.statements().next().is_none();
64+
65+
match then_block.expr() {
66+
Some(then_expr) if then_only_expr => {
67+
edit.replace(if_expr.syntax().range(), then_expr.syntax().text())
68+
}
69+
_ => edit.replace(if_expr.syntax().range(), then_block.syntax().text()),
70+
}
71+
72+
edit.insert(last_match_pat.syntax().range().end(), buf);
73+
edit.set_cursor(last_match_pat.syntax().range().end() + TextUnit::from(1));
74+
},
75+
);
76+
ctx.build()
77+
}
78+
79+
#[cfg(test)]
80+
mod tests {
81+
use super::*;
82+
83+
use crate::helpers::{ check_assist, check_assist_target };
84+
85+
#[test]
86+
fn move_guard_to_arm_body_target() {
87+
check_assist_target(
88+
move_guard_to_arm_body,
89+
r#"
90+
fn f() {
91+
let t = 'a';
92+
let chars = "abcd";
93+
match t {
94+
'\r' <|>if chars.clone().next() == Some('\n') => false,
95+
_ => true
96+
}
97+
}
98+
"#,
99+
r#"if chars.clone().next() == Some('\n')"#,
100+
);
101+
}
102+
103+
#[test]
104+
fn move_guard_to_arm_body_works() {
105+
check_assist(
106+
move_guard_to_arm_body,
107+
r#"
108+
fn f() {
109+
let t = 'a';
110+
let chars = "abcd";
111+
match t {
112+
'\r' <|>if chars.clone().next() == Some('\n') => false,
113+
_ => true
114+
}
115+
}
116+
"#,
117+
r#"
118+
fn f() {
119+
let t = 'a';
120+
let chars = "abcd";
121+
match t {
122+
'\r' => if chars.clone().next() == Some('\n') { <|>false },
123+
_ => true
124+
}
125+
}
126+
"#,
127+
);
128+
}
129+
130+
#[test]
131+
fn move_guard_to_arm_body_works_complex_match() {
132+
check_assist(
133+
move_guard_to_arm_body,
134+
r#"
135+
fn f() {
136+
match x {
137+
<|>y @ 4 | y @ 5 if y > 5 => true,
138+
_ => false
139+
}
140+
}
141+
"#,
142+
r#"
143+
fn f() {
144+
match x {
145+
y @ 4 | y @ 5 => if y > 5 { <|>true },
146+
_ => false
147+
}
148+
}
149+
"#,
150+
);
151+
}
152+
153+
#[test]
154+
fn move_arm_cond_to_match_guard_works() {
155+
check_assist(
156+
move_arm_cond_to_match_guard,
157+
r#"
158+
fn f() {
159+
let t = 'a';
160+
let chars = "abcd";
161+
match t {
162+
'\r' => if chars.clone().next() == Some('\n') { <|>false },
163+
_ => true
164+
}
165+
}
166+
"#,
167+
r#"
168+
fn f() {
169+
let t = 'a';
170+
let chars = "abcd";
171+
match t {
172+
'\r' <|>if chars.clone().next() == Some('\n') => false,
173+
_ => true
174+
}
175+
}
176+
"#,
177+
);
178+
}
179+
180+
#[test]
181+
fn move_arm_cond_to_match_guard_if_let_works() {
182+
check_assist(
183+
move_arm_cond_to_match_guard,
184+
r#"
185+
fn f() {
186+
let t = 'a';
187+
let chars = "abcd";
188+
match t {
189+
'\r' => if let Some(_) = chars.clone().next() { <|>false },
190+
_ => true
191+
}
192+
}
193+
"#,
194+
r#"
195+
fn f() {
196+
let t = 'a';
197+
let chars = "abcd";
198+
match t {
199+
'\r' <|>if let Some(_) = chars.clone().next() => false,
200+
_ => true
201+
}
202+
}
203+
"#,
204+
);
205+
}
206+
207+
#[test]
208+
fn move_arm_cond_to_match_guard_if_empty_body_works() {
209+
check_assist(
210+
move_arm_cond_to_match_guard,
211+
r#"
212+
fn f() {
213+
let t = 'a';
214+
let chars = "abcd";
215+
match t {
216+
'\r' => if let Some(_) = chars.clone().next() { <|> },
217+
_ => true
218+
}
219+
}
220+
"#,
221+
r#"
222+
fn f() {
223+
let t = 'a';
224+
let chars = "abcd";
225+
match t {
226+
'\r' <|>if let Some(_) = chars.clone().next() => { },
227+
_ => true
228+
}
229+
}
230+
"#,
231+
);
232+
}
233+
234+
#[test]
235+
fn move_arm_cond_to_match_guard_if_multiline_body_works() {
236+
check_assist(
237+
move_arm_cond_to_match_guard,
238+
r#"
239+
fn f() {
240+
let mut t = 'a';
241+
let chars = "abcd";
242+
match t {
243+
'\r' => if let Some(_) = chars.clone().next() {
244+
t = 'e';<|>
245+
false
246+
},
247+
_ => true
248+
}
249+
}
250+
"#,
251+
r#"
252+
fn f() {
253+
let mut t = 'a';
254+
let chars = "abcd";
255+
match t {
256+
'\r' <|>if let Some(_) = chars.clone().next() => {
257+
t = 'e';
258+
false
259+
},
260+
_ => true
261+
}
262+
}
263+
"#,
264+
);
265+
}
266+
}

0 commit comments

Comments
 (0)