Skip to content

Commit f4d493c

Browse files
authored
Merge pull request #22095 from Shourya742/2026-04-19-replace-make-with-Syntax-factory-convert-range-for-to-while
Replace make with SyntaxFactory in convert reange for to while
2 parents 2541da9 + dbf6b3c commit f4d493c

1 file changed

Lines changed: 22 additions & 15 deletions

File tree

crates/ide-assists/src/handlers/convert_range_for_to_while.rs

Lines changed: 22 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,10 @@ use syntax::{
55
SyntaxKind::WHITESPACE,
66
T,
77
algo::previous_non_trivia_token,
8-
ast::{self, HasArgList, HasLoopBody, HasName, RangeItem, edit::AstNodeEdit, make},
8+
ast::{
9+
self, HasArgList, HasLoopBody, HasName, RangeItem, edit::AstNodeEdit,
10+
syntax_factory::SyntaxFactory,
11+
},
912
syntax_editor::{Element, Position, SyntaxEditor},
1013
};
1114

@@ -33,11 +36,13 @@ use crate::assist_context::{AssistContext, Assists};
3336
// }
3437
// ```
3538
pub(crate) fn convert_range_for_to_while(acc: &mut Assists, ctx: &AssistContext<'_>) -> Option<()> {
39+
let (editor, _) = SyntaxEditor::new(ctx.source_file().syntax().clone());
40+
let make = editor.make();
3641
let for_kw = ctx.find_token_syntax_at_offset(T![for])?;
3742
let for_ = ast::ForExpr::cast(for_kw.parent()?)?;
3843
let ast::Pat::IdentPat(pat) = for_.pat()? else { return None };
3944
let iterable = for_.iterable()?;
40-
let (start, end, step, inclusive) = extract_range(&iterable)?;
45+
let (start, end, step, inclusive) = extract_range(&iterable, make)?;
4146
let name = pat.name()?;
4247
let body = for_.loop_body()?.stmt_list()?;
4348
let label = for_.label();
@@ -52,9 +57,7 @@ pub(crate) fn convert_range_for_to_while(acc: &mut Assists, ctx: &AssistContext<
5257
description,
5358
for_.syntax().text_range(),
5459
|builder| {
55-
let editor = builder.make_editor(for_.syntax());
5660
let make = editor.make();
57-
5861
let indent = for_.indent_level();
5962
let pat = make.ident_pat(pat.ref_token().is_some(), true, name.clone());
6063
let let_stmt = make.let_stmt(pat.into(), None, Some(start));
@@ -100,16 +103,19 @@ pub(crate) fn convert_range_for_to_while(acc: &mut Assists, ctx: &AssistContext<
100103
)
101104
}
102105

103-
fn extract_range(iterable: &ast::Expr) -> Option<(ast::Expr, Option<ast::Expr>, ast::Expr, bool)> {
106+
fn extract_range(
107+
iterable: &ast::Expr,
108+
make: &SyntaxFactory,
109+
) -> Option<(ast::Expr, Option<ast::Expr>, ast::Expr, bool)> {
104110
Some(match iterable {
105-
ast::Expr::ParenExpr(expr) => extract_range(&expr.expr()?)?,
111+
ast::Expr::ParenExpr(expr) => extract_range(&expr.expr()?, make)?,
106112
ast::Expr::RangeExpr(range) => {
107113
let inclusive = range.op_kind()? == ast::RangeOp::Inclusive;
108-
(range.start()?, range.end(), make::expr_literal("1").into(), inclusive)
114+
(range.start()?, range.end(), make.expr_literal("1").into(), inclusive)
109115
}
110116
ast::Expr::MethodCallExpr(call) if call.name_ref()?.text() == "step_by" => {
111117
let [step] = Itertools::collect_array(call.arg_list()?.args())?;
112-
let (start, end, _, inclusive) = extract_range(&call.receiver()?)?;
118+
let (start, end, _, inclusive) = extract_range(&call.receiver()?, make)?;
113119
(start, end, step, inclusive)
114120
}
115121
_ => return None,
@@ -119,9 +125,10 @@ fn extract_range(iterable: &ast::Expr) -> Option<(ast::Expr, Option<ast::Expr>,
119125
fn process_loop_body(
120126
body: ast::StmtList,
121127
label: Option<ast::Label>,
122-
edit: &SyntaxEditor,
128+
editor: &SyntaxEditor,
123129
incrementer: Vec<SyntaxElement>,
124130
) -> Option<()> {
131+
let make = editor.make();
125132
let last = previous_non_trivia_token(body.r_curly_token()?)?.syntax_element();
126133

127134
let new_body = body.indent(1.into());
@@ -134,7 +141,7 @@ fn process_loop_body(
134141
);
135142

136143
if continues.is_empty() {
137-
edit.insert_all(Position::after(last), incrementer);
144+
editor.insert_all(Position::after(last), incrementer);
138145
return Some(());
139146
}
140147

@@ -145,8 +152,8 @@ fn process_loop_body(
145152
let first = children.next()?;
146153
let block_content = first.clone()..=children.last().unwrap_or(first);
147154

148-
let continue_label = make::lifetime("'cont");
149-
let break_expr = make::expr_break(Some(continue_label.clone()), None);
155+
let continue_label = make.lifetime("'cont");
156+
let break_expr = make.expr_break(Some(continue_label.clone()), None);
150157
let (new_edit, _) = SyntaxEditor::new(new_body.syntax().clone());
151158
for continue_expr in &continues {
152159
new_edit.replace(continue_expr.syntax(), break_expr.syntax());
@@ -155,13 +162,13 @@ fn process_loop_body(
155162
let elements = itertools::chain(
156163
[
157164
continue_label.syntax().syntax_element(),
158-
make::token(T![:]).syntax_element(),
159-
make::tokens::single_space().syntax_element(),
165+
make.token(T![:]).syntax_element(),
166+
make.whitespace(" ").syntax_element(),
160167
new_body.syntax_element(),
161168
],
162169
incrementer,
163170
);
164-
edit.replace_all(block_content, elements.collect());
171+
editor.replace_all(block_content, elements.collect());
165172

166173
Some(())
167174
}

0 commit comments

Comments
 (0)