remove edit_in_place from replace_if_let_with_match assist with edit::AstNodeEdit
This commit is contained in:
parent
96d6a3e077
commit
c8e284bf01
4 changed files with 43 additions and 33 deletions
|
|
@ -3,7 +3,8 @@ use stdx::{format_to, to_lower_snake_case};
|
|||
use syntax::{
|
||||
TextRange,
|
||||
ast::{
|
||||
self, AstNode, HasGenericParams, HasName, HasVisibility, edit::AstNodeEdit, syntax_factory::SyntaxFactory
|
||||
self, AstNode, HasGenericParams, HasName, HasVisibility, edit::AstNodeEdit,
|
||||
syntax_factory::SyntaxFactory,
|
||||
},
|
||||
syntax_editor::Position,
|
||||
};
|
||||
|
|
|
|||
|
|
@ -2,7 +2,10 @@ use hir::{AsAssocItem, AssocItemContainer, FileRange, HasSource};
|
|||
use ide_db::{assists::AssistId, defs::Definition, search::SearchScope};
|
||||
use syntax::{
|
||||
SyntaxKind,
|
||||
ast::{self, AstNode, edit::{AstNodeEdit, IndentLevel}},
|
||||
ast::{
|
||||
self, AstNode,
|
||||
edit::{AstNodeEdit, IndentLevel},
|
||||
},
|
||||
};
|
||||
|
||||
use crate::assist_context::{AssistContext, Assists};
|
||||
|
|
|
|||
|
|
@ -3,7 +3,11 @@ use std::iter::successors;
|
|||
use ide_db::{RootDatabase, defs::NameClass, ty_filter::TryEnum};
|
||||
use syntax::{
|
||||
AstNode, Edition, SyntaxKind, T, TextRange,
|
||||
ast::{self, HasName, edit::IndentLevel, edit_in_place::Indent, syntax_factory::SyntaxFactory},
|
||||
ast::{
|
||||
self, HasName,
|
||||
edit::{AstNodeEdit, IndentLevel},
|
||||
syntax_factory::SyntaxFactory,
|
||||
},
|
||||
syntax_editor::SyntaxEditor,
|
||||
};
|
||||
|
||||
|
|
@ -54,8 +58,7 @@ pub(crate) fn replace_if_let_with_match(acc: &mut Assists, ctx: &AssistContext<'
|
|||
ast::ElseBranch::IfExpr(expr) => Some(expr),
|
||||
ast::ElseBranch::Block(block) => {
|
||||
let block = unwrap_trivial_block(block).clone_for_update();
|
||||
block.reindent_to(IndentLevel(1));
|
||||
else_block = Some(block);
|
||||
else_block = Some(block.reset_indent().indent(IndentLevel(1)));
|
||||
None
|
||||
}
|
||||
});
|
||||
|
|
@ -82,12 +85,13 @@ pub(crate) fn replace_if_let_with_match(acc: &mut Assists, ctx: &AssistContext<'
|
|||
(Some(pat), guard)
|
||||
}
|
||||
};
|
||||
if let Some(guard) = &guard {
|
||||
guard.dedent(indent);
|
||||
guard.indent(IndentLevel(1));
|
||||
}
|
||||
let body = if_expr.then_branch()?.clone_for_update();
|
||||
body.indent(IndentLevel(1));
|
||||
let guard = if let Some(guard) = &guard {
|
||||
Some(guard.dedent(indent).indent(IndentLevel(1)))
|
||||
} else {
|
||||
guard
|
||||
};
|
||||
|
||||
let body = if_expr.then_branch()?.clone_for_update().indent(IndentLevel(1));
|
||||
cond_bodies.push((cond, guard, body));
|
||||
}
|
||||
|
||||
|
|
@ -109,7 +113,8 @@ pub(crate) fn replace_if_let_with_match(acc: &mut Assists, ctx: &AssistContext<'
|
|||
let else_arm = make_else_arm(ctx, &make, else_block, &cond_bodies);
|
||||
let make_match_arm =
|
||||
|(pat, guard, body): (_, Option<ast::Expr>, ast::BlockExpr)| {
|
||||
body.reindent_to(IndentLevel::single());
|
||||
// Dedent from original position, then indent for match arm
|
||||
let body = body.dedent(indent).indent(IndentLevel::single());
|
||||
let body = unwrap_trivial_block(body);
|
||||
match (pat, guard.map(|it| make.match_guard(it))) {
|
||||
(Some(pat), guard) => make.match_arm(pat, guard, body),
|
||||
|
|
@ -122,8 +127,8 @@ pub(crate) fn replace_if_let_with_match(acc: &mut Assists, ctx: &AssistContext<'
|
|||
}
|
||||
};
|
||||
let arms = cond_bodies.into_iter().map(make_match_arm).chain([else_arm]);
|
||||
let match_expr = make.expr_match(scrutinee_to_be_expr, make.match_arm_list(arms));
|
||||
match_expr.indent(indent);
|
||||
let match_expr =
|
||||
make.expr_match(scrutinee_to_be_expr, make.match_arm_list(arms)).indent(indent);
|
||||
match_expr.into()
|
||||
};
|
||||
|
||||
|
|
@ -131,10 +136,9 @@ pub(crate) fn replace_if_let_with_match(acc: &mut Assists, ctx: &AssistContext<'
|
|||
if_expr.syntax().parent().is_some_and(|it| ast::IfExpr::can_cast(it.kind()));
|
||||
let expr = if has_preceding_if_expr {
|
||||
// make sure we replace the `else if let ...` with a block so we don't end up with `else expr`
|
||||
match_expr.dedent(indent);
|
||||
match_expr.indent(IndentLevel(1));
|
||||
let block_expr = make.block_expr([], Some(match_expr));
|
||||
block_expr.indent(indent);
|
||||
let block_expr = make
|
||||
.block_expr([], Some(match_expr.dedent(indent).indent(IndentLevel(1))))
|
||||
.indent(indent);
|
||||
block_expr.into()
|
||||
} else {
|
||||
match_expr
|
||||
|
|
@ -267,10 +271,7 @@ pub(crate) fn replace_match_with_if_let(acc: &mut Assists, ctx: &AssistContext<'
|
|||
// wrap them in another BlockExpr.
|
||||
match expr {
|
||||
ast::Expr::BlockExpr(block) if block.modifier().is_none() => block,
|
||||
expr => {
|
||||
expr.indent(IndentLevel(1));
|
||||
make.block_expr([], Some(expr))
|
||||
}
|
||||
expr => make.block_expr([], Some(expr.indent(IndentLevel(1)))),
|
||||
}
|
||||
};
|
||||
|
||||
|
|
@ -292,18 +293,19 @@ pub(crate) fn replace_match_with_if_let(acc: &mut Assists, ctx: &AssistContext<'
|
|||
} else {
|
||||
condition
|
||||
};
|
||||
let then_expr = then_expr.clone_for_update();
|
||||
let else_expr = else_expr.clone_for_update();
|
||||
then_expr.reindent_to(IndentLevel::single());
|
||||
else_expr.reindent_to(IndentLevel::single());
|
||||
let then_expr =
|
||||
then_expr.clone_for_update().reset_indent().indent(IndentLevel::single());
|
||||
let else_expr =
|
||||
else_expr.clone_for_update().reset_indent().indent(IndentLevel::single());
|
||||
let then_block = make_block_expr(then_expr);
|
||||
let else_expr = if is_empty_expr(&else_expr) { None } else { Some(else_expr) };
|
||||
let if_let_expr = make.expr_if(
|
||||
condition,
|
||||
then_block,
|
||||
else_expr.map(make_block_expr).map(ast::ElseBranch::Block),
|
||||
);
|
||||
if_let_expr.indent(IndentLevel::from_node(match_expr.syntax()));
|
||||
let if_let_expr = make
|
||||
.expr_if(
|
||||
condition,
|
||||
then_block,
|
||||
else_expr.map(make_block_expr).map(ast::ElseBranch::Block),
|
||||
)
|
||||
.indent(IndentLevel::from_node(match_expr.syntax()));
|
||||
|
||||
let mut editor = builder.make_editor(match_expr.syntax());
|
||||
editor.replace(match_expr.syntax(), if_let_expr.syntax());
|
||||
|
|
|
|||
|
|
@ -1,7 +1,11 @@
|
|||
use ide_db::ty_filter::TryEnum;
|
||||
use syntax::{
|
||||
AstNode, T,
|
||||
ast::{self, edit::{AstNodeEdit, IndentLevel}, syntax_factory::SyntaxFactory},
|
||||
ast::{
|
||||
self,
|
||||
edit::{AstNodeEdit, IndentLevel},
|
||||
syntax_factory::SyntaxFactory,
|
||||
},
|
||||
};
|
||||
|
||||
use crate::{AssistContext, AssistId, Assists};
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue