Skip to content

Commit 7fee088

Browse files
committed
complete merge_nested_if with bugs.
1 parent 3ab1666 commit 7fee088

File tree

3 files changed

+247
-0
lines changed

3 files changed

+247
-0
lines changed
Lines changed: 230 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,230 @@
1+
use ide_db::syntax_helpers::node_ext::is_pattern_cond;
2+
use syntax::{
3+
ast::{self, AstNode, BinaryOp},
4+
T,
5+
};
6+
7+
use crate::{
8+
assist_context::{AssistContext, Assists},
9+
AssistId, AssistKind,
10+
};
11+
/// Assist: merge_nested_if
12+
///
13+
/// This transforms if expressions of the form `if x { if y {A} }` into `if x && y {A}`
14+
/// This assist can only be applied with the cursor on `if`.
15+
///
16+
/// ```
17+
/// fn main() {
18+
/// i$0f x == 3 { if y == 4 { 1 } }
19+
/// }
20+
/// ```
21+
/// ->
22+
/// ```
23+
/// fn main() {
24+
/// if x == 3 && y == 4 { 1 }
25+
/// }
26+
/// ```
27+
pub(crate) fn merge_nested_if(acc: &mut Assists, ctx: &AssistContext<'_>) -> Option<()> {
28+
let if_keyword = ctx.find_token_syntax_at_offset(T![if])?;
29+
let expr = ast::IfExpr::cast(if_keyword.parent()?)?;
30+
let if_range = if_keyword.text_range();
31+
let cursor_in_range = if_range.contains_range(ctx.selection_trimmed());
32+
if !cursor_in_range {
33+
return None;
34+
}
35+
36+
//should not apply to if with else branch.
37+
if expr.else_branch().is_some() {
38+
return None;
39+
}
40+
41+
let cond = expr.condition()?;
42+
let cond_range = cond.syntax().text_range();
43+
//should not apply for if-let
44+
if is_pattern_cond(cond.clone()) {
45+
return None;
46+
}
47+
48+
//check if the then branch is a nested if
49+
let then_branch = expr.then_branch()?;
50+
51+
let nested_if_to_merge = then_branch.syntax().descendants().find_map(ast::IfExpr::cast)?;
52+
// should not apply to nested if with else branch.
53+
if nested_if_to_merge.else_branch().is_some() {
54+
return None;
55+
}
56+
let nested_if_cond = nested_if_to_merge.condition()?;
57+
if is_pattern_cond(nested_if_cond.clone()) {
58+
return None;
59+
}
60+
61+
let nested_if_then_branch = nested_if_to_merge.then_branch()?;
62+
let then_branch_range = then_branch.syntax().text_range();
63+
64+
acc.add(
65+
AssistId("merge_nested_if", AssistKind::RefactorRewrite),
66+
"Merge nested if",
67+
if_range,
68+
|edit| {
69+
let cond_text = if has_logic_op_or(&cond) {
70+
format!("({})", cond.syntax().text())
71+
} else {
72+
cond.syntax().text().to_string()
73+
};
74+
75+
let nested_if_cond_text = if has_logic_op_or(&nested_if_cond) {
76+
format!("({})", nested_if_cond.syntax().text())
77+
} else {
78+
nested_if_cond.syntax().text().to_string()
79+
};
80+
81+
let replace_cond = format!("{} && {}", cond_text, nested_if_cond_text);
82+
83+
edit.replace(cond_range, replace_cond);
84+
edit.replace(then_branch_range, nested_if_then_branch.syntax().text());
85+
},
86+
)
87+
}
88+
89+
/// Returns whether the given if condition has logical operators.
90+
fn has_logic_op_or(expr: &ast::Expr) -> bool {
91+
match expr {
92+
ast::Expr::BinExpr(bin_expr) => {
93+
if let Some(kind) = bin_expr.op_kind() {
94+
matches!(kind, BinaryOp::LogicOp(ast::LogicOp::Or))
95+
} else {
96+
false
97+
}
98+
}
99+
_ => false,
100+
}
101+
}
102+
103+
#[cfg(test)]
104+
mod tests {
105+
use super::*;
106+
use crate::tests::{check_assist, check_assist_not_applicable};
107+
108+
#[test]
109+
fn merge_nested_if_test1() {
110+
check_assist(
111+
merge_nested_if,
112+
"fn f() { i$0f x == 3 { if y == 4 { 1 } } }",
113+
"fn f() { if x == 3 && y == 4 { 1 } }",
114+
)
115+
}
116+
117+
#[test]
118+
fn merge_nested_if_test2() {
119+
check_assist(
120+
merge_nested_if,
121+
"fn f() { i$0f x == 3 || y == 1 { if z == 4 { 1 } } }",
122+
"fn f() { if (x == 3 || y == 1) && z == 4 { 1 } }",
123+
)
124+
}
125+
126+
#[test]
127+
fn merge_nested_if_test3() {
128+
check_assist(
129+
merge_nested_if,
130+
"fn f() { i$0f x == 3 && y == 1 { if z == 4 { 1 } } }",
131+
"fn f() { if x == 3 && y == 1 && z == 4 { 1 } }",
132+
)
133+
}
134+
135+
#[test]
136+
fn merge_nested_if_test4() {
137+
check_assist(
138+
merge_nested_if,
139+
"fn f() { i$0f x == 3 && y == 1 { if z == 4 && q == 3 { 1 } } }",
140+
"fn f() { if x == 3 && y == 1 && z == 4 && q == 3 { 1 } }",
141+
)
142+
}
143+
144+
#[test]
145+
fn merge_nested_if_test5() {
146+
check_assist(
147+
merge_nested_if,
148+
"fn f() { i$0f x == 3 && y == 1 { if z == 4 || q == 3 { 1 } } }",
149+
"fn f() { if x == 3 && y == 1 && (z == 4 || q == 3) { 1 } }",
150+
)
151+
}
152+
153+
#[test]
154+
fn merge_nested_if_test6() {
155+
check_assist(
156+
merge_nested_if,
157+
"fn f() { i$0f x == 3 || y == 1 { if z == 4 || q == 3 { 1 } } }",
158+
"fn f() { if (x == 3 || y == 1) && (z == 4 || q == 3) { 1 } }",
159+
)
160+
}
161+
162+
#[test]
163+
fn merge_nested_if_test7() {
164+
check_assist(
165+
merge_nested_if,
166+
"fn f() { i$0f x == 3 || y == 1 { if z == 4 && q == 3 { 1 } } }",
167+
"fn f() { if (x == 3 || y == 1) && z == 4 && q == 3 { 1 } }",
168+
)
169+
}
170+
171+
#[test]
172+
fn merge_nested_if_do_not_apply_to_if_with_else_branch() {
173+
check_assist_not_applicable(
174+
merge_nested_if,
175+
"fn f() { i$0f x == 3 { if y == 4 { 1 } } else { 2 } }",
176+
)
177+
}
178+
179+
#[test]
180+
fn merge_nested_if_do_not_apply_to_nested_if_with_else_branch() {
181+
check_assist_not_applicable(
182+
merge_nested_if,
183+
"fn f() { i$0f x == 3 { if y == 4 { 1 } else { 2 } } }",
184+
)
185+
}
186+
187+
#[test]
188+
fn merge_nested_if_do_not_apply_to_if_let() {
189+
check_assist_not_applicable(
190+
merge_nested_if,
191+
"fn f() { i$0f let Some(x) = y { if x == 4 { 1 } } }",
192+
)
193+
}
194+
195+
#[test]
196+
fn merge_nested_if_do_not_apply_to_nested_if_let() {
197+
check_assist_not_applicable(
198+
merge_nested_if,
199+
"fn f() { i$0f y == 0 { if let Some(x) = y { 1 } } }",
200+
)
201+
}
202+
203+
#[test]
204+
fn merge_nested_if_do_not_apply_to_if_with_else_branch_and_nested_if() {
205+
check_assist_not_applicable(
206+
merge_nested_if,
207+
"fn f() { i$0f x == 3 { if y == 4 { 1 } } else { if z == 5 { 2 } } }",
208+
)
209+
}
210+
211+
#[test]
212+
fn merge_nested_if_do_not_apply_with_cursor_not_on_if() {
213+
check_assist_not_applicable(merge_nested_if, "fn f() { if $0x==0 { if y == 3 { 1 } } }")
214+
}
215+
216+
#[test]
217+
fn merge_nested_if_do_not_apply_with_mulpiple_if() {
218+
check_assist_not_applicable(
219+
merge_nested_if,
220+
"fn f() { i$0f x == 0 { if y == 3 { 1 } else if y == 4 { 2 } } }",
221+
)
222+
}
223+
#[test]
224+
fn merge_nested_if_do_not_apply_with_not_only_has_nested_if(){
225+
check_assist_not_applicable(
226+
merge_nested_if,
227+
"fn f() { i$0f x == 0 { if y == 3 { foo(); } foo(); } }",
228+
)
229+
}
230+
}

crates/ide-assists/src/lib.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -217,6 +217,7 @@ mod handlers {
217217
mod unqualify_method_call;
218218
mod wrap_return_type_in_result;
219219
mod into_to_qualified_from;
220+
mod merge_nested_if;
220221

221222
pub(crate) fn all() -> &'static [Handler] {
222223
&[
@@ -291,6 +292,7 @@ mod handlers {
291292
invert_if::invert_if,
292293
merge_imports::merge_imports,
293294
merge_match_arms::merge_match_arms,
295+
merge_nested_if::merge_nested_if,
294296
move_bounds::move_bounds_to_where_clause,
295297
move_const_to_impl::move_const_to_impl,
296298
move_guard::move_arm_cond_to_match_guard,

crates/ide-assists/src/tests/generated.rs

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2051,6 +2051,21 @@ fn handle(action: Action) {
20512051
)
20522052
}
20532053

2054+
#[test]
2055+
fn doctest_merge_nested_if() {
2056+
check_doc_test(
2057+
"merge_nested_if",
2058+
r#####"
2059+
fn main() {
2060+
i$0f x == 3 { if y == 4 { 1 } }
2061+
}"#####,
2062+
r#####"
2063+
fn main() {
2064+
if x == 3 && y == 4 { 1 }
2065+
}"#####,
2066+
)
2067+
}
2068+
20542069
#[test]
20552070
fn doctest_move_arm_cond_to_match_guard() {
20562071
check_doc_test(

0 commit comments

Comments
 (0)