Skip to content

Commit 6c0e58d

Browse files
Preserve indentation
1 parent e4c019f commit 6c0e58d

File tree

1 file changed

+30
-5
lines changed

1 file changed

+30
-5
lines changed

crates/ide_assists/src/handlers/extract_type_alias.rs

Lines changed: 30 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
use syntax::{
2-
ast::{self, AstNode},
2+
ast::{self, edit::IndentLevel, AstNode},
33
match_ast,
44
};
55

@@ -31,10 +31,12 @@ pub(crate) fn extract_type_alias(acc: &mut Assists, ctx: &AssistContext) -> Opti
3131
let item = ctx.find_node_at_offset::<ast::Item>()?;
3232
let insert = match_ast! {
3333
match (item.syntax().parent()?) {
34-
ast::AssocItemList(it) => it.syntax().parent()?.text_range().start(),
35-
_ => item.syntax().text_range().start(),
34+
ast::AssocItemList(it) => it.syntax().parent()?.clone(),
35+
_ => item.syntax().clone(),
3636
}
3737
};
38+
let indent = IndentLevel::from_node(&insert);
39+
let insert = insert.text_range().start();
3840
let target = node.syntax().text_range();
3941

4042
acc.add(
@@ -46,10 +48,14 @@ pub(crate) fn extract_type_alias(acc: &mut Assists, ctx: &AssistContext) -> Opti
4648
builder.replace(target, "Type");
4749
match ctx.config.snippet_cap {
4850
Some(cap) => {
49-
builder.insert_snippet(cap, insert, format!("type $0Type = {};\n\n", node));
51+
builder.insert_snippet(
52+
cap,
53+
insert,
54+
format!("type $0Type = {};\n\n{}", node, indent),
55+
);
5056
}
5157
None => {
52-
builder.insert(insert, format!("type Type = {};\n\n", node));
58+
builder.insert(insert, format!("type Type = {};\n\n{}", node, indent));
5359
}
5460
}
5561
},
@@ -191,4 +197,23 @@ trait Tr {
191197
"#,
192198
);
193199
}
200+
201+
#[test]
202+
fn indentation() {
203+
check_assist(
204+
extract_type_alias,
205+
r#"
206+
mod m {
207+
fn f() -> $0u8$0 {}
208+
}
209+
"#,
210+
r#"
211+
mod m {
212+
type $0Type = u8;
213+
214+
fn f() -> Type {}
215+
}
216+
"#,
217+
);
218+
}
194219
}

0 commit comments

Comments
 (0)