diff options
author | Jonas Schievink <[email protected]> | 2021-06-02 21:27:12 +0100 |
---|---|---|
committer | Jonas Schievink <[email protected]> | 2021-06-02 21:27:12 +0100 |
commit | 6c0e58d10756742a55a04c221e8625dc8f7252b3 (patch) | |
tree | 6ec0526fdc716803afe1ec1b3e93059b878ad8d0 | |
parent | e4c019fcaa5ecc135054d789466aaa2614132592 (diff) |
Preserve indentation
-rw-r--r-- | crates/ide_assists/src/handlers/extract_type_alias.rs | 35 |
1 files changed, 30 insertions, 5 deletions
diff --git a/crates/ide_assists/src/handlers/extract_type_alias.rs b/crates/ide_assists/src/handlers/extract_type_alias.rs index a66f6abea..4bccf5984 100644 --- a/crates/ide_assists/src/handlers/extract_type_alias.rs +++ b/crates/ide_assists/src/handlers/extract_type_alias.rs | |||
@@ -1,5 +1,5 @@ | |||
1 | use syntax::{ | 1 | use syntax::{ |
2 | ast::{self, AstNode}, | 2 | ast::{self, edit::IndentLevel, AstNode}, |
3 | match_ast, | 3 | match_ast, |
4 | }; | 4 | }; |
5 | 5 | ||
@@ -31,10 +31,12 @@ pub(crate) fn extract_type_alias(acc: &mut Assists, ctx: &AssistContext) -> Opti | |||
31 | let item = ctx.find_node_at_offset::<ast::Item>()?; | 31 | let item = ctx.find_node_at_offset::<ast::Item>()?; |
32 | let insert = match_ast! { | 32 | let insert = match_ast! { |
33 | match (item.syntax().parent()?) { | 33 | match (item.syntax().parent()?) { |
34 | ast::AssocItemList(it) => it.syntax().parent()?.text_range().start(), | 34 | ast::AssocItemList(it) => it.syntax().parent()?.clone(), |
35 | _ => item.syntax().text_range().start(), | 35 | _ => item.syntax().clone(), |
36 | } | 36 | } |
37 | }; | 37 | }; |
38 | let indent = IndentLevel::from_node(&insert); | ||
39 | let insert = insert.text_range().start(); | ||
38 | let target = node.syntax().text_range(); | 40 | let target = node.syntax().text_range(); |
39 | 41 | ||
40 | acc.add( | 42 | acc.add( |
@@ -46,10 +48,14 @@ pub(crate) fn extract_type_alias(acc: &mut Assists, ctx: &AssistContext) -> Opti | |||
46 | builder.replace(target, "Type"); | 48 | builder.replace(target, "Type"); |
47 | match ctx.config.snippet_cap { | 49 | match ctx.config.snippet_cap { |
48 | Some(cap) => { | 50 | 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 | ); | ||
50 | } | 56 | } |
51 | None => { | 57 | None => { |
52 | builder.insert(insert, format!("type Type = {};\n\n", node)); | 58 | builder.insert(insert, format!("type Type = {};\n\n{}", node, indent)); |
53 | } | 59 | } |
54 | } | 60 | } |
55 | }, | 61 | }, |
@@ -191,4 +197,23 @@ trait Tr { | |||
191 | "#, | 197 | "#, |
192 | ); | 198 | ); |
193 | } | 199 | } |
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 | } | ||
194 | } | 219 | } |