diff options
author | Jonas Schievink <[email protected]> | 2021-06-02 21:20:41 +0100 |
---|---|---|
committer | Jonas Schievink <[email protected]> | 2021-06-02 21:20:41 +0100 |
commit | e4c019fcaa5ecc135054d789466aaa2614132592 (patch) | |
tree | bb004f515e95e138f8d98e8e85fe0deabe1f8614 /crates | |
parent | e5c86ee3ebec8886a1ac472064208a92c64c4c93 (diff) |
Account for traits
Diffstat (limited to 'crates')
-rw-r--r-- | crates/ide_assists/src/handlers/extract_type_alias.rs | 39 |
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 @@ | |||
1 | use syntax::ast::{self, AstNode}; | 1 | use syntax::{ |
2 | ast::{self, AstNode}, | ||
3 | match_ast, | ||
4 | }; | ||
2 | 5 | ||
3 | use crate::{AssistContext, AssistId, AssistKind, Assists}; | 6 | use 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#" | ||
181 | trait Tr { | ||
182 | fn f() -> $0(u8, u8)$0 {} | ||
183 | } | ||
184 | "#, | ||
185 | r#" | ||
186 | type $0Type = (u8, u8); | ||
187 | |||
188 | trait Tr { | ||
189 | fn f() -> Type {} | ||
190 | } | ||
191 | "#, | ||
192 | ); | ||
174 | } | 193 | } |
175 | } | 194 | } |