From 6c0e58d10756742a55a04c221e8625dc8f7252b3 Mon Sep 17 00:00:00 2001 From: Jonas Schievink Date: Wed, 2 Jun 2021 22:27:12 +0200 Subject: Preserve indentation --- .../ide_assists/src/handlers/extract_type_alias.rs | 35 ++++++++++++++++++---- 1 file changed, 30 insertions(+), 5 deletions(-) (limited to 'crates') 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 @@ use syntax::{ - ast::{self, AstNode}, + ast::{self, edit::IndentLevel, AstNode}, match_ast, }; @@ -31,10 +31,12 @@ pub(crate) fn extract_type_alias(acc: &mut Assists, ctx: &AssistContext) -> Opti let item = ctx.find_node_at_offset::()?; let insert = match_ast! { match (item.syntax().parent()?) { - ast::AssocItemList(it) => it.syntax().parent()?.text_range().start(), - _ => item.syntax().text_range().start(), + ast::AssocItemList(it) => it.syntax().parent()?.clone(), + _ => item.syntax().clone(), } }; + let indent = IndentLevel::from_node(&insert); + let insert = insert.text_range().start(); let target = node.syntax().text_range(); acc.add( @@ -46,10 +48,14 @@ pub(crate) fn extract_type_alias(acc: &mut Assists, ctx: &AssistContext) -> Opti builder.replace(target, "Type"); match ctx.config.snippet_cap { Some(cap) => { - builder.insert_snippet(cap, insert, format!("type $0Type = {};\n\n", node)); + builder.insert_snippet( + cap, + insert, + format!("type $0Type = {};\n\n{}", node, indent), + ); } None => { - builder.insert(insert, format!("type Type = {};\n\n", node)); + builder.insert(insert, format!("type Type = {};\n\n{}", node, indent)); } } }, @@ -191,4 +197,23 @@ trait Tr { "#, ); } + + #[test] + fn indentation() { + check_assist( + extract_type_alias, + r#" +mod m { + fn f() -> $0u8$0 {} +} + "#, + r#" +mod m { + type $0Type = u8; + + fn f() -> Type {} +} + "#, + ); + } } -- cgit v1.2.3