From e4c019fcaa5ecc135054d789466aaa2614132592 Mon Sep 17 00:00:00 2001 From: Jonas Schievink Date: Wed, 2 Jun 2021 22:20:41 +0200 Subject: Account for traits --- .../ide_assists/src/handlers/extract_type_alias.rs | 39 ++++++++++++++++------ 1 file changed, 29 insertions(+), 10 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 998e0de7b..a66f6abea 100644 --- a/crates/ide_assists/src/handlers/extract_type_alias.rs +++ b/crates/ide_assists/src/handlers/extract_type_alias.rs @@ -1,4 +1,7 @@ -use syntax::ast::{self, AstNode}; +use syntax::{ + ast::{self, AstNode}, + match_ast, +}; use crate::{AssistContext, AssistId, AssistKind, Assists}; @@ -25,12 +28,13 @@ pub(crate) fn extract_type_alias(acc: &mut Assists, ctx: &AssistContext) -> Opti } let node = ctx.find_node_at_range::()?; - let insert = ctx - .find_node_at_offset::() - .map(|imp| imp.syntax().clone()) - .or_else(|| ctx.find_node_at_offset::().map(|item| item.syntax().clone()))? - .text_range() - .start(); + 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(), + } + }; let target = node.syntax().text_range(); acc.add( @@ -153,9 +157,9 @@ struct S { } #[test] - fn extract_from_impl() { - // When invoked in an impl, extracted type alias should be placed next to the impl, not - // inside. + fn extract_from_impl_or_trait() { + // When invoked in an impl/trait, extracted type alias should be placed next to the + // impl/trait, not inside. check_assist( extract_type_alias, r#" @@ -168,6 +172,21 @@ type $0Type = (u8, u8); impl S { fn f() -> Type {} +} + "#, + ); + check_assist( + extract_type_alias, + r#" +trait Tr { + fn f() -> $0(u8, u8)$0 {} +} + "#, + r#" +type $0Type = (u8, u8); + +trait Tr { + fn f() -> Type {} } "#, ); -- cgit v1.2.3 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