Skip to content

Commit 8cad7d1

Browse files
committed
Use correct indent when replacing with match
1 parent 53cc2c1 commit 8cad7d1

File tree

3 files changed

+45
-7
lines changed

3 files changed

+45
-7
lines changed

crates/ra_assists/src/handlers/early_return.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -154,7 +154,7 @@ pub(crate) fn convert_to_guarded_return(acc: &mut Assists, ctx: &AssistContext)
154154
parent_block: &ast::BlockExpr,
155155
if_expr: &ast::IfExpr,
156156
) -> SyntaxNode {
157-
let then_block_items = then_block.dedent(IndentLevel::from(1));
157+
let then_block_items = then_block.dedent(IndentLevel(1));
158158
let end_of_then = then_block_items.syntax().last_child_or_token().unwrap();
159159
let end_of_then =
160160
if end_of_then.prev_sibling_or_token().map(|n| n.kind()) == Some(WHITESPACE) {

crates/ra_assists/src/handlers/replace_if_let_with_match.rs

Lines changed: 35 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,7 @@ pub(crate) fn replace_if_let_with_match(acc: &mut Assists, ctx: &AssistContext)
5151
acc.add(AssistId("replace_if_let_with_match"), "Replace with match", target, move |edit| {
5252
let match_expr = {
5353
let then_arm = {
54+
let then_block = then_block.reset_indent().indent(IndentLevel(1));
5455
let then_expr = unwrap_trivial_block(then_block);
5556
make::match_arm(vec![pat.clone()], then_expr)
5657
};
@@ -64,8 +65,8 @@ pub(crate) fn replace_if_let_with_match(acc: &mut Assists, ctx: &AssistContext)
6465
let else_expr = unwrap_trivial_block(else_block);
6566
make::match_arm(vec![pattern], else_expr)
6667
};
67-
make::expr_match(expr, make::match_arm_list(vec![then_arm, else_arm]))
68-
.indent(IndentLevel::from_node(if_expr.syntax()))
68+
let match_expr = make::expr_match(expr, make::match_arm_list(vec![then_arm, else_arm]));
69+
match_expr.indent(IndentLevel::from_node(if_expr.syntax()))
6970
};
7071

7172
edit.replace_ast::<ast::Expr>(if_expr.into(), match_expr);
@@ -213,4 +214,36 @@ fn foo(x: Result<i32, ()>) {
213214
"#,
214215
);
215216
}
217+
218+
#[test]
219+
fn nested_indent() {
220+
check_assist(
221+
replace_if_let_with_match,
222+
r#"
223+
fn main() {
224+
if true {
225+
<|>if let Ok(rel_path) = path.strip_prefix(root_path) {
226+
let rel_path = RelativePathBuf::from_path(rel_path).ok()?;
227+
Some((*id, rel_path))
228+
} else {
229+
None
230+
}
231+
}
232+
}
233+
"#,
234+
r#"
235+
fn main() {
236+
if true {
237+
match path.strip_prefix(root_path) {
238+
Ok(rel_path) => {
239+
let rel_path = RelativePathBuf::from_path(rel_path).ok()?;
240+
Some((*id, rel_path))
241+
}
242+
_ => None,
243+
}
244+
}
245+
}
246+
"#,
247+
)
248+
}
216249
}

crates/ra_syntax/src/ast/edit.rs

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -579,12 +579,17 @@ pub trait AstNodeEdit: AstNode + Clone + Sized {
579579
rewriter.rewrite_ast(self)
580580
}
581581
#[must_use]
582-
fn indent(&self, indent: IndentLevel) -> Self {
583-
Self::cast(indent.increase_indent(self.syntax().clone())).unwrap()
582+
fn indent(&self, level: IndentLevel) -> Self {
583+
Self::cast(level.increase_indent(self.syntax().clone())).unwrap()
584584
}
585585
#[must_use]
586-
fn dedent(&self, indent: IndentLevel) -> Self {
587-
Self::cast(indent.decrease_indent(self.syntax().clone())).unwrap()
586+
fn dedent(&self, level: IndentLevel) -> Self {
587+
Self::cast(level.decrease_indent(self.syntax().clone())).unwrap()
588+
}
589+
#[must_use]
590+
fn reset_indent(&self) -> Self {
591+
let level = IndentLevel::from_node(self.syntax());
592+
self.dedent(level)
588593
}
589594
}
590595

0 commit comments

Comments
 (0)