|
| 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 | + //should not apply for if-let |
| 43 | + if is_pattern_cond(cond.clone()) { |
| 44 | + return None; |
| 45 | + } |
| 46 | + |
| 47 | + let cond_range = cond.syntax().text_range(); |
| 48 | + |
| 49 | + //check if the then branch is a nested if |
| 50 | + let then_branch = expr.then_branch()?; |
| 51 | + let stmt = then_branch.stmt_list()?; |
| 52 | + if stmt.statements().count() != 0 { |
| 53 | + return None; |
| 54 | + } |
| 55 | + |
| 56 | + let nested_if_to_merge = then_branch.tail_expr().and_then(|e| match e { |
| 57 | + ast::Expr::IfExpr(e) => Some(e), |
| 58 | + _ => None, |
| 59 | + })?; |
| 60 | + // should not apply to nested if with else branch. |
| 61 | + if nested_if_to_merge.else_branch().is_some() { |
| 62 | + return None; |
| 63 | + } |
| 64 | + let nested_if_cond = nested_if_to_merge.condition()?; |
| 65 | + if is_pattern_cond(nested_if_cond.clone()) { |
| 66 | + return None; |
| 67 | + } |
| 68 | + |
| 69 | + let nested_if_then_branch = nested_if_to_merge.then_branch()?; |
| 70 | + let then_branch_range = then_branch.syntax().text_range(); |
| 71 | + |
| 72 | + acc.add( |
| 73 | + AssistId("merge_nested_if", AssistKind::RefactorRewrite), |
| 74 | + "Merge nested if", |
| 75 | + if_range, |
| 76 | + |edit| { |
| 77 | + let cond_text = if has_logic_op_or(&cond) { |
| 78 | + format!("({})", cond.syntax().text()) |
| 79 | + } else { |
| 80 | + cond.syntax().text().to_string() |
| 81 | + }; |
| 82 | + |
| 83 | + let nested_if_cond_text = if has_logic_op_or(&nested_if_cond) { |
| 84 | + format!("({})", nested_if_cond.syntax().text()) |
| 85 | + } else { |
| 86 | + nested_if_cond.syntax().text().to_string() |
| 87 | + }; |
| 88 | + |
| 89 | + let replace_cond = format!("{} && {}", cond_text, nested_if_cond_text); |
| 90 | + |
| 91 | + edit.replace(cond_range, replace_cond); |
| 92 | + edit.replace(then_branch_range, nested_if_then_branch.syntax().text()); |
| 93 | + }, |
| 94 | + ) |
| 95 | +} |
| 96 | + |
| 97 | +/// Returns whether the given if condition has logical operators. |
| 98 | +fn has_logic_op_or(expr: &ast::Expr) -> bool { |
| 99 | + match expr { |
| 100 | + ast::Expr::BinExpr(bin_expr) => { |
| 101 | + if let Some(kind) = bin_expr.op_kind() { |
| 102 | + matches!(kind, BinaryOp::LogicOp(ast::LogicOp::Or)) |
| 103 | + } else { |
| 104 | + false |
| 105 | + } |
| 106 | + } |
| 107 | + _ => false, |
| 108 | + } |
| 109 | +} |
| 110 | + |
| 111 | +#[cfg(test)] |
| 112 | +mod tests { |
| 113 | + use super::*; |
| 114 | + use crate::tests::{check_assist, check_assist_not_applicable}; |
| 115 | + |
| 116 | + #[test] |
| 117 | + fn merge_nested_if_test1() { |
| 118 | + check_assist( |
| 119 | + merge_nested_if, |
| 120 | + "fn f() { i$0f x == 3 { if y == 4 { 1 } } }", |
| 121 | + "fn f() { if x == 3 && y == 4 { 1 } }", |
| 122 | + ) |
| 123 | + } |
| 124 | + |
| 125 | + #[test] |
| 126 | + fn merge_nested_if_test2() { |
| 127 | + check_assist( |
| 128 | + merge_nested_if, |
| 129 | + "fn f() { i$0f x == 3 || y == 1 { if z == 4 { 1 } } }", |
| 130 | + "fn f() { if (x == 3 || y == 1) && z == 4 { 1 } }", |
| 131 | + ) |
| 132 | + } |
| 133 | + |
| 134 | + #[test] |
| 135 | + fn merge_nested_if_test3() { |
| 136 | + check_assist( |
| 137 | + merge_nested_if, |
| 138 | + "fn f() { i$0f x == 3 && y == 1 { if z == 4 { 1 } } }", |
| 139 | + "fn f() { if x == 3 && y == 1 && z == 4 { 1 } }", |
| 140 | + ) |
| 141 | + } |
| 142 | + |
| 143 | + #[test] |
| 144 | + fn merge_nested_if_test4() { |
| 145 | + check_assist( |
| 146 | + merge_nested_if, |
| 147 | + "fn f() { i$0f x == 3 && y == 1 { if z == 4 && q == 3 { 1 } } }", |
| 148 | + "fn f() { if x == 3 && y == 1 && z == 4 && q == 3 { 1 } }", |
| 149 | + ) |
| 150 | + } |
| 151 | + |
| 152 | + #[test] |
| 153 | + fn merge_nested_if_test5() { |
| 154 | + check_assist( |
| 155 | + merge_nested_if, |
| 156 | + "fn f() { i$0f x == 3 && y == 1 { if z == 4 || q == 3 { 1 } } }", |
| 157 | + "fn f() { if x == 3 && y == 1 && (z == 4 || q == 3) { 1 } }", |
| 158 | + ) |
| 159 | + } |
| 160 | + |
| 161 | + #[test] |
| 162 | + fn merge_nested_if_test6() { |
| 163 | + check_assist( |
| 164 | + merge_nested_if, |
| 165 | + "fn f() { i$0f x == 3 || y == 1 { if z == 4 || q == 3 { 1 } } }", |
| 166 | + "fn f() { if (x == 3 || y == 1) && (z == 4 || q == 3) { 1 } }", |
| 167 | + ) |
| 168 | + } |
| 169 | + |
| 170 | + #[test] |
| 171 | + fn merge_nested_if_test7() { |
| 172 | + check_assist( |
| 173 | + merge_nested_if, |
| 174 | + "fn f() { i$0f x == 3 || y == 1 { if z == 4 && q == 3 { 1 } } }", |
| 175 | + "fn f() { if (x == 3 || y == 1) && z == 4 && q == 3 { 1 } }", |
| 176 | + ) |
| 177 | + } |
| 178 | + |
| 179 | + #[test] |
| 180 | + fn merge_nested_if_do_not_apply_to_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_nested_if_with_else_branch() { |
| 189 | + check_assist_not_applicable( |
| 190 | + merge_nested_if, |
| 191 | + "fn f() { i$0f x == 3 { if y == 4 { 1 } else { 2 } } }", |
| 192 | + ) |
| 193 | + } |
| 194 | + |
| 195 | + #[test] |
| 196 | + fn merge_nested_if_do_not_apply_to_if_let() { |
| 197 | + check_assist_not_applicable( |
| 198 | + merge_nested_if, |
| 199 | + "fn f() { i$0f let Some(x) = y { if x == 4 { 1 } } }", |
| 200 | + ) |
| 201 | + } |
| 202 | + |
| 203 | + #[test] |
| 204 | + fn merge_nested_if_do_not_apply_to_nested_if_let() { |
| 205 | + check_assist_not_applicable( |
| 206 | + merge_nested_if, |
| 207 | + "fn f() { i$0f y == 0 { if let Some(x) = y { 1 } } }", |
| 208 | + ) |
| 209 | + } |
| 210 | + |
| 211 | + #[test] |
| 212 | + fn merge_nested_if_do_not_apply_to_if_with_else_branch_and_nested_if() { |
| 213 | + check_assist_not_applicable( |
| 214 | + merge_nested_if, |
| 215 | + "fn f() { i$0f x == 3 { if y == 4 { 1 } } else { if z == 5 { 2 } } }", |
| 216 | + ) |
| 217 | + } |
| 218 | + |
| 219 | + #[test] |
| 220 | + fn merge_nested_if_do_not_apply_with_cursor_not_on_if() { |
| 221 | + check_assist_not_applicable(merge_nested_if, "fn f() { if $0x==0 { if y == 3 { 1 } } }") |
| 222 | + } |
| 223 | + |
| 224 | + #[test] |
| 225 | + fn merge_nested_if_do_not_apply_with_mulpiple_if() { |
| 226 | + check_assist_not_applicable( |
| 227 | + merge_nested_if, |
| 228 | + "fn f() { i$0f x == 0 { if y == 3 { 1 } else if y == 4 { 2 } } }", |
| 229 | + ) |
| 230 | + } |
| 231 | + #[test] |
| 232 | + fn merge_nested_if_do_not_apply_with_not_only_has_nested_if() { |
| 233 | + check_assist_not_applicable( |
| 234 | + merge_nested_if, |
| 235 | + "fn f() { i$0f x == 0 { if y == 3 { foo(); } foo(); } }", |
| 236 | + ) |
| 237 | + } |
| 238 | + |
| 239 | + #[test] |
| 240 | + fn merge_nested_if_do_not_apply_with_multiply_nested_if() { |
| 241 | + check_assist_not_applicable( |
| 242 | + merge_nested_if, |
| 243 | + "fn f() { i$0f x == 0 { if y == 3 { foo(); } if z == 3 { 2 } } }", |
| 244 | + ) |
| 245 | + } |
| 246 | +} |
0 commit comments