From 66a5fd375a60ec98c35acd9b4d5b03c6057aa36c Mon Sep 17 00:00:00 2001 From: Jonas Schievink Date: Wed, 2 Jun 2021 16:50:44 +0200 Subject: Place extracted type alias outside of impl --- .../ide_assists/src/handlers/extract_type_alias.rs | 28 +++++++++++++++++++++- 1 file changed, 27 insertions(+), 1 deletion(-) diff --git a/crates/ide_assists/src/handlers/extract_type_alias.rs b/crates/ide_assists/src/handlers/extract_type_alias.rs index 442a209b9..998e0de7b 100644 --- a/crates/ide_assists/src/handlers/extract_type_alias.rs +++ b/crates/ide_assists/src/handlers/extract_type_alias.rs @@ -25,7 +25,12 @@ 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::()?.syntax().text_range().start(); + 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 target = node.syntax().text_range(); acc.add( @@ -142,6 +147,27 @@ type $0Type = u8; struct S { field: (Type,), +} + "#, + ); + } + + #[test] + fn extract_from_impl() { + // When invoked in an impl, extracted type alias should be placed next to the impl, not + // inside. + check_assist( + extract_type_alias, + r#" +impl S { + fn f() -> $0(u8, u8)$0 {} +} + "#, + r#" +type $0Type = (u8, u8); + +impl S { + fn f() -> Type {} } "#, ); -- cgit v1.2.3