Skip to content

Commit 49f1092

Browse files
committed
add_missing_impl_members and add_missing_default_members break indentation no longer
1 parent e07d638 commit 49f1092

File tree

2 files changed

+105
-37
lines changed

2 files changed

+105
-37
lines changed

crates/ide-assists/src/handlers/add_missing_impl_members.rs

Lines changed: 98 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -1346,8 +1346,8 @@ struct SomeStruct {
13461346
}
13471347
impl PartialEq for SomeStruct {
13481348
$0fn ne(&self, other: &Self) -> bool {
1349-
!self.eq(other)
1350-
}
1349+
!self.eq(other)
1350+
}
13511351
}
13521352
"#,
13531353
);
@@ -1520,48 +1520,47 @@ fn main() {
15201520
}
15211521

15221522
#[test]
1523-
fn test_add_missing_impl_members_indentation() {
1524-
// few trait members, no braces
1523+
fn test_add_missing_preserves_indentation() {
1524+
// in different modules
15251525
check_assist(
15261526
add_missing_impl_members,
15271527
r#"
15281528
mod m {
1529-
trait Foo { fn foo(&self); }
1530-
struct S;
1531-
impl Foo for S$0
1532-
}"#,
1533-
r#"
1534-
mod m {
1535-
trait Foo { fn foo(&self); }
1536-
struct S;
1537-
impl Foo for S {
1538-
fn foo(&self) {
1539-
${0:todo!()}
1540-
}
1541-
}
1542-
}"#,
1529+
pub trait Foo {
1530+
const CONST_MULTILINE: (
1531+
i32,
1532+
i32
15431533
);
1544-
// few trait members, empty impl def.
1545-
check_assist(
1546-
add_missing_impl_members,
1547-
r#"
1548-
mod m {
1549-
trait Foo { fn foo(&self); }
1550-
struct S;
1551-
impl Foo for S { $0 }
1552-
}"#,
1534+
1535+
fn foo(&self);
1536+
}
1537+
}
1538+
struct S;
1539+
impl m::Foo for S { $0 }"#,
15531540
r#"
15541541
mod m {
1555-
trait Foo { fn foo(&self); }
1556-
struct S;
1557-
impl Foo for S {
1558-
fn foo(&self) {
1559-
${0:todo!()}
1560-
}
1542+
pub trait Foo {
1543+
const CONST_MULTILINE: (
1544+
i32,
1545+
i32
1546+
);
1547+
1548+
fn foo(&self);
1549+
}
1550+
}
1551+
struct S;
1552+
impl m::Foo for S {
1553+
$0const CONST_MULTILINE: (
1554+
i32,
1555+
i32
1556+
);
1557+
1558+
fn foo(&self) {
1559+
todo!()
15611560
}
15621561
}"#,
15631562
);
1564-
// todo - in mod and outside
1563+
// in the same module
15651564
check_assist(
15661565
add_missing_impl_members,
15671566
r#"
@@ -1571,6 +1570,10 @@ mod m {
15711570
15721571
const CONST: usize = 42;
15731572
const CONST_2: i32;
1573+
const CONST_MULTILINE: (
1574+
i32,
1575+
i32
1576+
);
15741577
15751578
fn foo(&self);
15761579
fn bar(&self);
@@ -1591,6 +1594,10 @@ mod m {
15911594
15921595
const CONST: usize = 42;
15931596
const CONST_2: i32;
1597+
const CONST_MULTILINE: (
1598+
i32,
1599+
i32
1600+
);
15941601
15951602
fn foo(&self);
15961603
fn bar(&self);
@@ -1606,6 +1613,11 @@ mod m {
16061613
16071614
const CONST_2: i32;
16081615
1616+
const CONST_MULTILINE: (
1617+
i32,
1618+
i32
1619+
);
1620+
16091621
fn foo(&self) {
16101622
todo!()
16111623
}
@@ -1618,4 +1630,56 @@ mod m {
16181630
}"#,
16191631
);
16201632
}
1633+
1634+
#[test]
1635+
fn test_add_default_preserves_indentation() {
1636+
check_assist(
1637+
add_missing_default_members,
1638+
r#"
1639+
mod m {
1640+
pub trait Foo {
1641+
type Output;
1642+
1643+
const CONST: usize = 42;
1644+
const CONST_2: i32;
1645+
const CONST_MULTILINE: = (
1646+
i32,
1647+
i32,
1648+
) = (3, 14);
1649+
1650+
fn valid(some: u32) -> bool { false }
1651+
fn foo(some: u32) -> bool;
1652+
}
1653+
}
1654+
struct S;
1655+
impl m::Foo for S { $0 }"#,
1656+
r#"
1657+
mod m {
1658+
pub trait Foo {
1659+
type Output;
1660+
1661+
const CONST: usize = 42;
1662+
const CONST_2: i32;
1663+
const CONST_MULTILINE: = (
1664+
i32,
1665+
i32,
1666+
) = (3, 14);
1667+
1668+
fn valid(some: u32) -> bool { false }
1669+
fn foo(some: u32) -> bool;
1670+
}
1671+
}
1672+
struct S;
1673+
impl m::Foo for S {
1674+
$0const CONST: usize = 42;
1675+
1676+
const CONST_MULTILINE: = (
1677+
i32,
1678+
i32,
1679+
) = (3, 14);
1680+
1681+
fn valid(some: u32) -> bool { false }
1682+
}"#,
1683+
)
1684+
}
16211685
}

crates/ide-assists/src/utils.rs

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ use syntax::{
1010
ast::{
1111
self,
1212
edit::{AstNodeEdit, IndentLevel},
13-
edit_in_place::{AttrsOwnerEdit, Removable},
13+
edit_in_place::{AttrsOwnerEdit, Indent, Removable},
1414
make, HasArgList, HasAttrs, HasGenericParams, HasName, HasTypeBounds, Whitespace,
1515
},
1616
ted, AstNode, AstToken, Direction, SourceFile,
@@ -139,9 +139,11 @@ pub fn add_trait_assoc_items_to_impl(
139139

140140
let transform = PathTransform::trait_impl(&target_scope, &source_scope, trait_, impl_.clone());
141141

142+
let new_indent_level = IndentLevel::from_node(impl_.syntax()) + 1;
142143
let items = items.into_iter().map(|assoc_item| {
143144
transform.apply(assoc_item.syntax());
144145
assoc_item.remove_attrs_and_docs();
146+
assoc_item.reindent_to(new_indent_level);
145147
assoc_item
146148
});
147149

@@ -153,8 +155,10 @@ pub fn add_trait_assoc_items_to_impl(
153155
first_item.get_or_insert_with(|| item.clone());
154156
match &item {
155157
ast::AssocItem::Fn(fn_) if fn_.body().is_none() => {
156-
let body = make::block_expr(None, Some(make::ext::expr_todo()))
157-
.indent(IndentLevel::from_node(impl_.syntax()) + 1);
158+
let body = AstNodeEdit::indent(
159+
&make::block_expr(None, Some(make::ext::expr_todo())),
160+
new_indent_level,
161+
);
158162
ted::replace(fn_.get_or_create_body().syntax(), body.clone_for_update().syntax())
159163
}
160164
ast::AssocItem::TypeAlias(type_alias) => {

0 commit comments

Comments
 (0)