Skip to content

Commit 092396c

Browse files
Merge #9119
9119: fix: some minor "extract type alias" fixes r=jonas-schievink a=jonas-schievink It now correctly works inside traits, and no longer messes up the indentation of the original node bors r+ Co-authored-by: Jonas Schievink <jonasschievink@gmail.com>
2 parents c2157f5 + 6c0e58d commit 092396c

File tree

1 file changed

+56
-12
lines changed

1 file changed

+56
-12
lines changed

crates/ide_assists/src/handlers/extract_type_alias.rs

Lines changed: 56 additions & 12 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, edit::IndentLevel, AstNode},
3+
match_ast,
4+
};
25

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

@@ -25,12 +28,15 @@ 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()?.clone(),
35+
_ => item.syntax().clone(),
36+
}
37+
};
38+
let indent = IndentLevel::from_node(&insert);
39+
let insert = insert.text_range().start();
3440
let target = node.syntax().text_range();
3541

3642
acc.add(
@@ -42,10 +48,14 @@ pub(crate) fn extract_type_alias(acc: &mut Assists, ctx: &AssistContext) -> Opti
4248
builder.replace(target, "Type");
4349
match ctx.config.snippet_cap {
4450
Some(cap) => {
45-
builder.insert_snippet(cap, insert, format!("type $0Type = {};\n\n", node));
51+
builder.insert_snippet(
52+
cap,
53+
insert,
54+
format!("type $0Type = {};\n\n{}", node, indent),
55+
);
4656
}
4757
None => {
48-
builder.insert(insert, format!("type Type = {};\n\n", node));
58+
builder.insert(insert, format!("type Type = {};\n\n{}", node, indent));
4959
}
5060
}
5161
},
@@ -153,9 +163,9 @@ struct S {
153163
}
154164

155165
#[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.
166+
fn extract_from_impl_or_trait() {
167+
// When invoked in an impl/trait, extracted type alias should be placed next to the
168+
// impl/trait, not inside.
159169
check_assist(
160170
extract_type_alias,
161171
r#"
@@ -167,6 +177,40 @@ impl S {
167177
type $0Type = (u8, u8);
168178
169179
impl S {
180+
fn f() -> Type {}
181+
}
182+
"#,
183+
);
184+
check_assist(
185+
extract_type_alias,
186+
r#"
187+
trait Tr {
188+
fn f() -> $0(u8, u8)$0 {}
189+
}
190+
"#,
191+
r#"
192+
type $0Type = (u8, u8);
193+
194+
trait Tr {
195+
fn f() -> Type {}
196+
}
197+
"#,
198+
);
199+
}
200+
201+
#[test]
202+
fn indentation() {
203+
check_assist(
204+
extract_type_alias,
205+
r#"
206+
mod m {
207+
fn f() -> $0u8$0 {}
208+
}
209+
"#,
210+
r#"
211+
mod m {
212+
type $0Type = u8;
213+
170214
fn f() -> Type {}
171215
}
172216
"#,

0 commit comments

Comments
 (0)