aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorJonas Schievink <[email protected]>2021-06-02 21:20:41 +0100
committerJonas Schievink <[email protected]>2021-06-02 21:20:41 +0100
commite4c019fcaa5ecc135054d789466aaa2614132592 (patch)
treebb004f515e95e138f8d98e8e85fe0deabe1f8614
parente5c86ee3ebec8886a1ac472064208a92c64c4c93 (diff)
Account for traits
-rw-r--r--crates/ide_assists/src/handlers/extract_type_alias.rs39
1 files changed, 29 insertions, 10 deletions
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 @@
1use syntax::ast::{self, AstNode}; 1use syntax::{
2 ast::{self, AstNode},
3 match_ast,
4};
2 5
3use crate::{AssistContext, AssistId, AssistKind, Assists}; 6use crate::{AssistContext, AssistId, AssistKind, Assists};
4 7
@@ -25,12 +28,13 @@ pub(crate) fn extract_type_alias(acc: &mut Assists, ctx: &AssistContext) -> Opti
25 } 28 }
26 29
27 let node = ctx.find_node_at_range::<ast::Type>()?; 30 let node = ctx.find_node_at_range::<ast::Type>()?;
28 let insert = ctx 31 let item = ctx.find_node_at_offset::<ast::Item>()?;
29 .find_node_at_offset::<ast::Impl>() 32 let insert = match_ast! {
30 .map(|imp| imp.syntax().clone()) 33 match (item.syntax().parent()?) {
31 .or_else(|| ctx.find_node_at_offset::<ast::Item>().map(|item| item.syntax().clone()))? 34 ast::AssocItemList(it) => it.syntax().parent()?.text_range().start(),
32 .text_range() 35 _ => item.syntax().text_range().start(),
33 .start(); 36 }
37 };
34 let target = node.syntax().text_range(); 38 let target = node.syntax().text_range();
35 39
36 acc.add( 40 acc.add(
@@ -153,9 +157,9 @@ struct S {
153 } 157 }
154 158
155 #[test] 159 #[test]
156 fn extract_from_impl() { 160 fn extract_from_impl_or_trait() {
157 // When invoked in an impl, extracted type alias should be placed next to the impl, not 161 // When invoked in an impl/trait, extracted type alias should be placed next to the
158 // inside. 162 // impl/trait, not inside.
159 check_assist( 163 check_assist(
160 extract_type_alias, 164 extract_type_alias,
161 r#" 165 r#"
@@ -171,5 +175,20 @@ impl S {
171} 175}
172 "#, 176 "#,
173 ); 177 );
178 check_assist(
179 extract_type_alias,
180 r#"
181trait Tr {
182 fn f() -> $0(u8, u8)$0 {}
183}
184 "#,
185 r#"
186type $0Type = (u8, u8);
187
188trait Tr {
189 fn f() -> Type {}
190}
191 "#,
192 );
174 } 193 }
175} 194}