mirror of
https://github.com/rust-lang/rust.git
synced 2026-04-27 18:57:42 +03:00
remove make from convert reange for to while
This commit is contained in:
+22
-15
@@ -5,7 +5,10 @@
|
||||
SyntaxKind::WHITESPACE,
|
||||
T,
|
||||
algo::previous_non_trivia_token,
|
||||
ast::{self, HasArgList, HasLoopBody, HasName, RangeItem, edit::AstNodeEdit, make},
|
||||
ast::{
|
||||
self, HasArgList, HasLoopBody, HasName, RangeItem, edit::AstNodeEdit,
|
||||
syntax_factory::SyntaxFactory,
|
||||
},
|
||||
syntax_editor::{Element, Position, SyntaxEditor},
|
||||
};
|
||||
|
||||
@@ -33,11 +36,13 @@
|
||||
// }
|
||||
// ```
|
||||
pub(crate) fn convert_range_for_to_while(acc: &mut Assists, ctx: &AssistContext<'_>) -> Option<()> {
|
||||
let (editor, _) = SyntaxEditor::new(ctx.source_file().syntax().clone());
|
||||
let make = editor.make();
|
||||
let for_kw = ctx.find_token_syntax_at_offset(T![for])?;
|
||||
let for_ = ast::ForExpr::cast(for_kw.parent()?)?;
|
||||
let ast::Pat::IdentPat(pat) = for_.pat()? else { return None };
|
||||
let iterable = for_.iterable()?;
|
||||
let (start, end, step, inclusive) = extract_range(&iterable)?;
|
||||
let (start, end, step, inclusive) = extract_range(&iterable, make)?;
|
||||
let name = pat.name()?;
|
||||
let body = for_.loop_body()?.stmt_list()?;
|
||||
let label = for_.label();
|
||||
@@ -52,9 +57,7 @@ pub(crate) fn convert_range_for_to_while(acc: &mut Assists, ctx: &AssistContext<
|
||||
description,
|
||||
for_.syntax().text_range(),
|
||||
|builder| {
|
||||
let editor = builder.make_editor(for_.syntax());
|
||||
let make = editor.make();
|
||||
|
||||
let indent = for_.indent_level();
|
||||
let pat = make.ident_pat(pat.ref_token().is_some(), true, name.clone());
|
||||
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<
|
||||
)
|
||||
}
|
||||
|
||||
fn extract_range(iterable: &ast::Expr) -> Option<(ast::Expr, Option<ast::Expr>, ast::Expr, bool)> {
|
||||
fn extract_range(
|
||||
iterable: &ast::Expr,
|
||||
make: &SyntaxFactory,
|
||||
) -> Option<(ast::Expr, Option<ast::Expr>, ast::Expr, bool)> {
|
||||
Some(match iterable {
|
||||
ast::Expr::ParenExpr(expr) => extract_range(&expr.expr()?)?,
|
||||
ast::Expr::ParenExpr(expr) => extract_range(&expr.expr()?, make)?,
|
||||
ast::Expr::RangeExpr(range) => {
|
||||
let inclusive = range.op_kind()? == ast::RangeOp::Inclusive;
|
||||
(range.start()?, range.end(), make::expr_literal("1").into(), inclusive)
|
||||
(range.start()?, range.end(), make.expr_literal("1").into(), inclusive)
|
||||
}
|
||||
ast::Expr::MethodCallExpr(call) if call.name_ref()?.text() == "step_by" => {
|
||||
let [step] = Itertools::collect_array(call.arg_list()?.args())?;
|
||||
let (start, end, _, inclusive) = extract_range(&call.receiver()?)?;
|
||||
let (start, end, _, inclusive) = extract_range(&call.receiver()?, make)?;
|
||||
(start, end, step, inclusive)
|
||||
}
|
||||
_ => return None,
|
||||
@@ -119,9 +125,10 @@ fn extract_range(iterable: &ast::Expr) -> Option<(ast::Expr, Option<ast::Expr>,
|
||||
fn process_loop_body(
|
||||
body: ast::StmtList,
|
||||
label: Option<ast::Label>,
|
||||
edit: &SyntaxEditor,
|
||||
editor: &SyntaxEditor,
|
||||
incrementer: Vec<SyntaxElement>,
|
||||
) -> Option<()> {
|
||||
let make = editor.make();
|
||||
let last = previous_non_trivia_token(body.r_curly_token()?)?.syntax_element();
|
||||
|
||||
let new_body = body.indent(1.into());
|
||||
@@ -134,7 +141,7 @@ fn process_loop_body(
|
||||
);
|
||||
|
||||
if continues.is_empty() {
|
||||
edit.insert_all(Position::after(last), incrementer);
|
||||
editor.insert_all(Position::after(last), incrementer);
|
||||
return Some(());
|
||||
}
|
||||
|
||||
@@ -145,8 +152,8 @@ fn process_loop_body(
|
||||
let first = children.next()?;
|
||||
let block_content = first.clone()..=children.last().unwrap_or(first);
|
||||
|
||||
let continue_label = make::lifetime("'cont");
|
||||
let break_expr = make::expr_break(Some(continue_label.clone()), None);
|
||||
let continue_label = make.lifetime("'cont");
|
||||
let break_expr = make.expr_break(Some(continue_label.clone()), None);
|
||||
let (new_edit, _) = SyntaxEditor::new(new_body.syntax().clone());
|
||||
for continue_expr in &continues {
|
||||
new_edit.replace(continue_expr.syntax(), break_expr.syntax());
|
||||
@@ -155,13 +162,13 @@ fn process_loop_body(
|
||||
let elements = itertools::chain(
|
||||
[
|
||||
continue_label.syntax().syntax_element(),
|
||||
make::token(T![:]).syntax_element(),
|
||||
make::tokens::single_space().syntax_element(),
|
||||
make.token(T![:]).syntax_element(),
|
||||
make.whitespace(" ").syntax_element(),
|
||||
new_body.syntax_element(),
|
||||
],
|
||||
incrementer,
|
||||
);
|
||||
edit.replace_all(block_content, elements.collect());
|
||||
editor.replace_all(block_content, elements.collect());
|
||||
|
||||
Some(())
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user