Skip to content

Commit e4c019f

Browse files
Account for traits
1 parent e5c86ee commit e4c019f

File tree

1 file changed

+29
-10
lines changed

1 file changed

+29
-10
lines changed

crates/ide_assists/src/handlers/extract_type_alias.rs

Lines changed: 29 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,7 @@
1-
use syntax::ast::{self, AstNode};
1+
use syntax::{
2+
ast::{self, AstNode},
3+
match_ast,
4+
};
25

36
use crate::{AssistContext, AssistId, AssistKind, Assists};
47

@@ -25,12 +28,13 @@ pub(crate) fn extract_type_alias(acc: &mut Assists, ctx: &AssistContext) -> Opti
2528
}
2629

2730
let node = ctx.find_node_at_range::<ast::Type>()?;
28-
let insert = ctx
29-
.find_node_at_offset::<ast::Impl>()
30-
.map(|imp| imp.syntax().clone())
31-
.or_else(|| ctx.find_node_at_offset::<ast::Item>().map(|item| item.syntax().clone()))?
32-
.text_range()
33-
.start();
31+
let item = ctx.find_node_at_offset::<ast::Item>()?;
32+
let insert = match_ast! {
33+
match (item.syntax().parent()?) {
34+
ast::AssocItemList(it) => it.syntax().parent()?.text_range().start(),
35+
_ => item.syntax().text_range().start(),
36+
}
37+
};
3438
let target = node.syntax().text_range();
3539

3640
acc.add(
@@ -153,9 +157,9 @@ struct S {
153157
}
154158

155159
#[test]
156-
fn extract_from_impl() {
157-
// When invoked in an impl, extracted type alias should be placed next to the impl, not
158-
// inside.
160+
fn extract_from_impl_or_trait() {
161+
// When invoked in an impl/trait, extracted type alias should be placed next to the
162+
// impl/trait, not inside.
159163
check_assist(
160164
extract_type_alias,
161165
r#"
@@ -168,6 +172,21 @@ type $0Type = (u8, u8);
168172
169173
impl S {
170174
fn f() -> Type {}
175+
}
176+
"#,
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 {}
171190
}
172191
"#,
173192
);

0 commit comments

Comments
 (0)