|
1 | 1 | use ide_db::defs::{Definition, NameRefClass};
|
2 | 2 | use syntax::{
|
3 | 3 | AstNode, SyntaxNode,
|
4 |
| - ast::{self, HasName, Name, syntax_factory::SyntaxFactory}, |
| 4 | + ast::{self, HasName, Name, edit::AstNodeEdit, syntax_factory::SyntaxFactory}, |
5 | 5 | syntax_editor::SyntaxEditor,
|
6 | 6 | };
|
7 | 7 |
|
@@ -45,7 +45,7 @@ pub(crate) fn convert_match_to_let_else(acc: &mut Assists, ctx: &AssistContext<'
|
45 | 45 | return None;
|
46 | 46 | }
|
47 | 47 |
|
48 |
| - let diverging_arm_expr = match diverging_arm.expr()? { |
| 48 | + let diverging_arm_expr = match diverging_arm.expr()?.dedent(1.into()) { |
49 | 49 | ast::Expr::BlockExpr(block) if block.modifier().is_none() && block.label().is_none() => {
|
50 | 50 | block.to_string()
|
51 | 51 | }
|
@@ -150,7 +150,12 @@ fn rename_variable(pat: &ast::Pat, extracted: &[Name], binding: ast::Pat) -> Syn
|
150 | 150 | }
|
151 | 151 | }
|
152 | 152 | 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 | + } |
154 | 159 | }
|
155 | 160 |
|
156 | 161 | #[cfg(test)]
|
@@ -209,6 +214,53 @@ fn foo(opt: Option<Foo>) -> Result<u32, ()> {
|
209 | 214 | );
|
210 | 215 | }
|
211 | 216 |
|
| 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 | + |
212 | 264 | #[test]
|
213 | 265 | fn should_not_be_applicable_if_extracting_arm_is_not_an_identity_expr() {
|
214 | 266 | cov_mark::check_count!(extracting_arm_is_not_an_identity_expr, 2);
|
@@ -489,9 +541,9 @@ fn f() {
|
489 | 541 | r#"
|
490 | 542 | fn f() {
|
491 | 543 | let Some(x) = Some(()) else {//comment
|
492 |
| - println!("nope"); |
493 |
| - return |
494 |
| - }; |
| 544 | + println!("nope"); |
| 545 | + return |
| 546 | + }; |
495 | 547 | }
|
496 | 548 | "#,
|
497 | 549 | );
|
|
0 commit comments