mirror of
https://github.com/rust-lang/rust-analyzer.git
synced 2025-10-01 11:31:15 +00:00
Fix indent for move_guard_to_arm_body
Input: ```rust fn main() { match 92 { x $0if true && true && true => { { false } }, _ => true } } ``` Old output: ```rust fn main() { match 92 { x => if true && true && true { { { false } } }, _ => true }; } ``` This PR fixed: ```rust fn main() { match 92 { x => if true && true && true { { { false } } }, _ => true } } ```
This commit is contained in:
parent
e10fa9393e
commit
f5f797e2d3
@ -40,28 +40,34 @@ pub(crate) fn move_guard_to_arm_body(acc: &mut Assists, ctx: &AssistContext<'_>)
|
|||||||
return None;
|
return None;
|
||||||
}
|
}
|
||||||
let space_before_guard = guard.syntax().prev_sibling_or_token();
|
let space_before_guard = guard.syntax().prev_sibling_or_token();
|
||||||
|
let space_after_arrow = match_arm.fat_arrow_token()?.next_sibling_or_token();
|
||||||
|
|
||||||
let guard_condition = guard.condition()?;
|
let guard_condition = guard.condition()?.reset_indent();
|
||||||
let arm_expr = match_arm.expr()?;
|
let arm_expr = match_arm.expr()?;
|
||||||
let if_expr =
|
let then_branch = make::block_expr(None, Some(arm_expr.reset_indent().indent(1.into())));
|
||||||
make::expr_if(guard_condition, make::block_expr(None, Some(arm_expr.clone())), None)
|
let if_expr = make::expr_if(guard_condition, then_branch, None).indent(arm_expr.indent_level());
|
||||||
.indent(arm_expr.indent_level());
|
|
||||||
|
|
||||||
let target = guard.syntax().text_range();
|
let target = guard.syntax().text_range();
|
||||||
acc.add(
|
acc.add(
|
||||||
AssistId::refactor_rewrite("move_guard_to_arm_body"),
|
AssistId::refactor_rewrite("move_guard_to_arm_body"),
|
||||||
"Move guard to arm body",
|
"Move guard to arm body",
|
||||||
target,
|
target,
|
||||||
|edit| {
|
|builder| {
|
||||||
match space_before_guard {
|
let mut edit = builder.make_editor(match_arm.syntax());
|
||||||
Some(element) if element.kind() == WHITESPACE => {
|
if let Some(element) = space_before_guard
|
||||||
edit.delete(element.text_range());
|
&& element.kind() == WHITESPACE
|
||||||
|
{
|
||||||
|
edit.delete(element);
|
||||||
|
}
|
||||||
|
if let Some(element) = space_after_arrow
|
||||||
|
&& element.kind() == WHITESPACE
|
||||||
|
{
|
||||||
|
edit.replace(element, make::tokens::single_space());
|
||||||
}
|
}
|
||||||
_ => (),
|
|
||||||
};
|
|
||||||
|
|
||||||
edit.delete(guard.syntax().text_range());
|
edit.delete(guard.syntax());
|
||||||
edit.replace_ast(arm_expr, if_expr.into());
|
edit.replace(arm_expr.syntax(), if_expr.syntax());
|
||||||
|
builder.add_file_edits(ctx.vfs_file_id(), edit);
|
||||||
},
|
},
|
||||||
)
|
)
|
||||||
}
|
}
|
||||||
@ -298,6 +304,44 @@ fn main() {
|
|||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn move_multiline_guard_to_arm_body_works() {
|
||||||
|
check_assist(
|
||||||
|
move_guard_to_arm_body,
|
||||||
|
r#"
|
||||||
|
fn main() {
|
||||||
|
match 92 {
|
||||||
|
x $0if true
|
||||||
|
&& true
|
||||||
|
&& true =>
|
||||||
|
{
|
||||||
|
{
|
||||||
|
false
|
||||||
|
}
|
||||||
|
},
|
||||||
|
_ => true
|
||||||
|
}
|
||||||
|
}
|
||||||
|
"#,
|
||||||
|
r#"
|
||||||
|
fn main() {
|
||||||
|
match 92 {
|
||||||
|
x => if true
|
||||||
|
&& true
|
||||||
|
&& true {
|
||||||
|
{
|
||||||
|
{
|
||||||
|
false
|
||||||
|
}
|
||||||
|
}
|
||||||
|
},
|
||||||
|
_ => true
|
||||||
|
}
|
||||||
|
}
|
||||||
|
"#,
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn move_guard_to_arm_body_works_complex_match() {
|
fn move_guard_to_arm_body_works_complex_match() {
|
||||||
check_assist(
|
check_assist(
|
||||||
|
Loading…
x
Reference in New Issue
Block a user