Skip to content

Commit 4d7b904

Browse files
authored
Merge pull request #20455 from A4-Tacks/fix-indent-conv-match-to-let-else
Fix indent for convert_match_to_let_else
2 parents 83b8523 + e797f81 commit 4d7b904

File tree

1 file changed

+58
-6
lines changed

1 file changed

+58
-6
lines changed

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

Lines changed: 58 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
use ide_db::defs::{Definition, NameRefClass};
22
use syntax::{
33
AstNode, SyntaxNode,
4-
ast::{self, HasName, Name, syntax_factory::SyntaxFactory},
4+
ast::{self, HasName, Name, edit::AstNodeEdit, syntax_factory::SyntaxFactory},
55
syntax_editor::SyntaxEditor,
66
};
77

@@ -45,7 +45,7 @@ pub(crate) fn convert_match_to_let_else(acc: &mut Assists, ctx: &AssistContext<'
4545
return None;
4646
}
4747

48-
let diverging_arm_expr = match diverging_arm.expr()? {
48+
let diverging_arm_expr = match diverging_arm.expr()?.dedent(1.into()) {
4949
ast::Expr::BlockExpr(block) if block.modifier().is_none() && block.label().is_none() => {
5050
block.to_string()
5151
}
@@ -150,7 +150,12 @@ fn rename_variable(pat: &ast::Pat, extracted: &[Name], binding: ast::Pat) -> Syn
150150
}
151151
}
152152
editor.add_mappings(make.finish_with_mappings());
153-
editor.finish().new_root().clone()
153+
let new_node = editor.finish().new_root().clone();
154+
if let Some(pat) = ast::Pat::cast(new_node.clone()) {
155+
pat.dedent(1.into()).syntax().clone()
156+
} else {
157+
new_node
158+
}
154159
}
155160

156161
#[cfg(test)]
@@ -209,6 +214,53 @@ fn foo(opt: Option<Foo>) -> Result<u32, ()> {
209214
);
210215
}
211216

217+
#[test]
218+
fn indent_level() {
219+
check_assist(
220+
convert_match_to_let_else,
221+
r#"
222+
//- minicore: option
223+
enum Foo {
224+
A(u32),
225+
B(u32),
226+
C(String),
227+
}
228+
229+
fn foo(opt: Option<Foo>) -> Result<u32, ()> {
230+
let mut state = 2;
231+
let va$0lue = match opt {
232+
Some(
233+
Foo::A(it)
234+
| Foo::B(it)
235+
) => it,
236+
_ => {
237+
state = 3;
238+
return Err(())
239+
},
240+
};
241+
}
242+
"#,
243+
r#"
244+
enum Foo {
245+
A(u32),
246+
B(u32),
247+
C(String),
248+
}
249+
250+
fn foo(opt: Option<Foo>) -> Result<u32, ()> {
251+
let mut state = 2;
252+
let Some(
253+
Foo::A(value)
254+
| Foo::B(value)
255+
) = opt else {
256+
state = 3;
257+
return Err(())
258+
};
259+
}
260+
"#,
261+
);
262+
}
263+
212264
#[test]
213265
fn should_not_be_applicable_if_extracting_arm_is_not_an_identity_expr() {
214266
cov_mark::check_count!(extracting_arm_is_not_an_identity_expr, 2);
@@ -489,9 +541,9 @@ fn f() {
489541
r#"
490542
fn f() {
491543
let Some(x) = Some(()) else {//comment
492-
println!("nope");
493-
return
494-
};
544+
println!("nope");
545+
return
546+
};
495547
}
496548
"#,
497549
);

0 commit comments

Comments
 (0)