|
| 1 | +use ide_db::assists::AssistId; |
| 2 | +use itertools::Itertools; |
| 3 | +use syntax::{ |
| 4 | + AstNode, T, |
| 5 | + algo::previous_non_trivia_token, |
| 6 | + ast::{ |
| 7 | + self, HasArgList, HasLoopBody, HasName, RangeItem, edit::AstNodeEdit, make, |
| 8 | + syntax_factory::SyntaxFactory, |
| 9 | + }, |
| 10 | + syntax_editor::{Element, Position}, |
| 11 | +}; |
| 12 | + |
| 13 | +use crate::assist_context::{AssistContext, Assists}; |
| 14 | + |
| 15 | +// Assist: convert_range_for_to_while |
| 16 | +// |
| 17 | +// Convert for each range into while loop. |
| 18 | +// |
| 19 | +// ``` |
| 20 | +// fn foo() { |
| 21 | +// $0for i in 3..7 { |
| 22 | +// foo(i); |
| 23 | +// } |
| 24 | +// } |
| 25 | +// ``` |
| 26 | +// -> |
| 27 | +// ``` |
| 28 | +// fn foo() { |
| 29 | +// let mut i = 3; |
| 30 | +// while i < 7 { |
| 31 | +// foo(i); |
| 32 | +// i += 1; |
| 33 | +// } |
| 34 | +// } |
| 35 | +// ``` |
| 36 | +pub(crate) fn convert_range_for_to_while(acc: &mut Assists, ctx: &AssistContext<'_>) -> Option<()> { |
| 37 | + let for_kw = ctx.find_token_syntax_at_offset(T![for])?; |
| 38 | + let for_ = ast::ForExpr::cast(for_kw.parent()?)?; |
| 39 | + let ast::Pat::IdentPat(pat) = for_.pat()? else { return None }; |
| 40 | + let iterable = for_.iterable()?; |
| 41 | + let (start, end, step, inclusive) = extract_range(&iterable)?; |
| 42 | + let name = pat.name()?; |
| 43 | + let body = for_.loop_body()?; |
| 44 | + let last = previous_non_trivia_token(body.stmt_list()?.r_curly_token()?)?; |
| 45 | + |
| 46 | + let description = if end.is_some() { |
| 47 | + "Replace with while expression" |
| 48 | + } else { |
| 49 | + "Replace with loop expression" |
| 50 | + }; |
| 51 | + acc.add( |
| 52 | + AssistId::refactor("convert_range_for_to_while"), |
| 53 | + description, |
| 54 | + for_.syntax().text_range(), |
| 55 | + |builder| { |
| 56 | + let mut edit = builder.make_editor(for_.syntax()); |
| 57 | + let make = SyntaxFactory::with_mappings(); |
| 58 | + |
| 59 | + let indent = for_.indent_level(); |
| 60 | + let pat = make.ident_pat(pat.ref_token().is_some(), true, name.clone()); |
| 61 | + let let_stmt = make.let_stmt(pat.into(), None, Some(start)); |
| 62 | + edit.insert_all( |
| 63 | + Position::before(for_.syntax()), |
| 64 | + vec![ |
| 65 | + let_stmt.syntax().syntax_element(), |
| 66 | + make.whitespace(&format!("\n{}", indent)).syntax_element(), |
| 67 | + ], |
| 68 | + ); |
| 69 | + |
| 70 | + let mut elements = vec![]; |
| 71 | + |
| 72 | + let var_expr = make.expr_path(make.ident_path(&name.text())); |
| 73 | + let op = ast::BinaryOp::CmpOp(ast::CmpOp::Ord { |
| 74 | + ordering: ast::Ordering::Less, |
| 75 | + strict: !inclusive, |
| 76 | + }); |
| 77 | + if let Some(end) = end { |
| 78 | + elements.extend([ |
| 79 | + make.token(T![while]).syntax_element(), |
| 80 | + make.whitespace(" ").syntax_element(), |
| 81 | + make.expr_bin(var_expr.clone(), op, end).syntax().syntax_element(), |
| 82 | + ]); |
| 83 | + } else { |
| 84 | + elements.push(make.token(T![loop]).syntax_element()); |
| 85 | + } |
| 86 | + |
| 87 | + edit.replace_all( |
| 88 | + for_kw.syntax_element()..=iterable.syntax().syntax_element(), |
| 89 | + elements, |
| 90 | + ); |
| 91 | + |
| 92 | + let op = ast::BinaryOp::Assignment { op: Some(ast::ArithOp::Add) }; |
| 93 | + edit.insert_all( |
| 94 | + Position::after(last), |
| 95 | + vec![ |
| 96 | + make.whitespace(&format!("\n{}", indent + 1)).syntax_element(), |
| 97 | + make.expr_bin(var_expr, op, step).syntax().syntax_element(), |
| 98 | + make.token(T![;]).syntax_element(), |
| 99 | + ], |
| 100 | + ); |
| 101 | + |
| 102 | + edit.add_mappings(make.finish_with_mappings()); |
| 103 | + builder.add_file_edits(ctx.vfs_file_id(), edit); |
| 104 | + }, |
| 105 | + ) |
| 106 | +} |
| 107 | + |
| 108 | +fn extract_range(iterable: &ast::Expr) -> Option<(ast::Expr, Option<ast::Expr>, ast::Expr, bool)> { |
| 109 | + Some(match iterable { |
| 110 | + ast::Expr::ParenExpr(expr) => extract_range(&expr.expr()?)?, |
| 111 | + ast::Expr::RangeExpr(range) => { |
| 112 | + let inclusive = range.op_kind()? == ast::RangeOp::Inclusive; |
| 113 | + (range.start()?, range.end(), make::expr_literal("1").into(), inclusive) |
| 114 | + } |
| 115 | + ast::Expr::MethodCallExpr(call) if call.name_ref()?.text() == "step_by" => { |
| 116 | + let [step] = call.arg_list()?.args().collect_array()?; |
| 117 | + let (start, end, _, inclusive) = extract_range(&call.receiver()?)?; |
| 118 | + (start, end, step, inclusive) |
| 119 | + } |
| 120 | + _ => return None, |
| 121 | + }) |
| 122 | +} |
| 123 | + |
| 124 | +#[cfg(test)] |
| 125 | +mod tests { |
| 126 | + use crate::tests::{check_assist, check_assist_not_applicable}; |
| 127 | + |
| 128 | + use super::*; |
| 129 | + |
| 130 | + #[test] |
| 131 | + fn test_convert_range_for_to_while() { |
| 132 | + check_assist( |
| 133 | + convert_range_for_to_while, |
| 134 | + " |
| 135 | +fn foo() { |
| 136 | + $0for i in 3..7 { |
| 137 | + foo(i); |
| 138 | + } |
| 139 | +} |
| 140 | + ", |
| 141 | + " |
| 142 | +fn foo() { |
| 143 | + let mut i = 3; |
| 144 | + while i < 7 { |
| 145 | + foo(i); |
| 146 | + i += 1; |
| 147 | + } |
| 148 | +} |
| 149 | + ", |
| 150 | + ); |
| 151 | + } |
| 152 | + |
| 153 | + #[test] |
| 154 | + fn test_convert_range_for_to_while_no_end_bound() { |
| 155 | + check_assist( |
| 156 | + convert_range_for_to_while, |
| 157 | + " |
| 158 | +fn foo() { |
| 159 | + $0for i in 3.. { |
| 160 | + foo(i); |
| 161 | + } |
| 162 | +} |
| 163 | + ", |
| 164 | + " |
| 165 | +fn foo() { |
| 166 | + let mut i = 3; |
| 167 | + loop { |
| 168 | + foo(i); |
| 169 | + i += 1; |
| 170 | + } |
| 171 | +} |
| 172 | + ", |
| 173 | + ); |
| 174 | + } |
| 175 | + |
| 176 | + #[test] |
| 177 | + fn test_convert_range_for_to_while_with_mut_binding() { |
| 178 | + check_assist( |
| 179 | + convert_range_for_to_while, |
| 180 | + " |
| 181 | +fn foo() { |
| 182 | + $0for mut i in 3..7 { |
| 183 | + foo(i); |
| 184 | + } |
| 185 | +} |
| 186 | + ", |
| 187 | + " |
| 188 | +fn foo() { |
| 189 | + let mut i = 3; |
| 190 | + while i < 7 { |
| 191 | + foo(i); |
| 192 | + i += 1; |
| 193 | + } |
| 194 | +} |
| 195 | + ", |
| 196 | + ); |
| 197 | + } |
| 198 | + |
| 199 | + #[test] |
| 200 | + fn test_convert_range_for_to_while_with_label() { |
| 201 | + check_assist( |
| 202 | + convert_range_for_to_while, |
| 203 | + " |
| 204 | +fn foo() { |
| 205 | + 'a: $0for mut i in 3..7 { |
| 206 | + foo(i); |
| 207 | + } |
| 208 | +} |
| 209 | + ", |
| 210 | + " |
| 211 | +fn foo() { |
| 212 | + let mut i = 3; |
| 213 | + 'a: while i < 7 { |
| 214 | + foo(i); |
| 215 | + i += 1; |
| 216 | + } |
| 217 | +} |
| 218 | + ", |
| 219 | + ); |
| 220 | + } |
| 221 | + |
| 222 | + #[test] |
| 223 | + fn test_convert_range_for_to_while_step_by() { |
| 224 | + check_assist( |
| 225 | + convert_range_for_to_while, |
| 226 | + " |
| 227 | +fn foo() { |
| 228 | + $0for mut i in (3..7).step_by(2) { |
| 229 | + foo(i); |
| 230 | + } |
| 231 | +} |
| 232 | + ", |
| 233 | + " |
| 234 | +fn foo() { |
| 235 | + let mut i = 3; |
| 236 | + while i < 7 { |
| 237 | + foo(i); |
| 238 | + i += 2; |
| 239 | + } |
| 240 | +} |
| 241 | + ", |
| 242 | + ); |
| 243 | + } |
| 244 | + |
| 245 | + #[test] |
| 246 | + fn test_convert_range_for_to_while_not_applicable_non_range() { |
| 247 | + check_assist_not_applicable( |
| 248 | + convert_range_for_to_while, |
| 249 | + " |
| 250 | +fn foo() { |
| 251 | + let ident = 3..7; |
| 252 | + $0for mut i in ident { |
| 253 | + foo(i); |
| 254 | + } |
| 255 | +} |
| 256 | + ", |
| 257 | + ); |
| 258 | + } |
| 259 | +} |
0 commit comments