Skip to content

Commit e07d638

Browse files
committed
add_missing_impl_members no longer breaks indentation
1 parent a0db478 commit e07d638

File tree

3 files changed

+106
-4
lines changed

3 files changed

+106
-4
lines changed

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

Lines changed: 102 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1511,11 +1511,111 @@ fn main() {
15111511
struct S;
15121512
impl Tr for S {
15131513
fn method() {
1514-
${0:todo!()}
1515-
}
1514+
${0:todo!()}
1515+
}
15161516
}
15171517
}
15181518
"#,
15191519
);
15201520
}
1521+
1522+
#[test]
1523+
fn test_add_missing_impl_members_indentation() {
1524+
// few trait members, no braces
1525+
check_assist(
1526+
add_missing_impl_members,
1527+
r#"
1528+
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+
}"#,
1543+
);
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+
}"#,
1553+
r#"
1554+
mod m {
1555+
trait Foo { fn foo(&self); }
1556+
struct S;
1557+
impl Foo for S {
1558+
fn foo(&self) {
1559+
${0:todo!()}
1560+
}
1561+
}
1562+
}"#,
1563+
);
1564+
// todo - in mod and outside
1565+
check_assist(
1566+
add_missing_impl_members,
1567+
r#"
1568+
mod m {
1569+
trait Foo {
1570+
type Output;
1571+
1572+
const CONST: usize = 42;
1573+
const CONST_2: i32;
1574+
1575+
fn foo(&self);
1576+
fn bar(&self);
1577+
fn baz(&self);
1578+
}
1579+
1580+
struct S;
1581+
1582+
impl Foo for S {
1583+
fn bar(&self) {}
1584+
$0
1585+
}
1586+
}"#,
1587+
r#"
1588+
mod m {
1589+
trait Foo {
1590+
type Output;
1591+
1592+
const CONST: usize = 42;
1593+
const CONST_2: i32;
1594+
1595+
fn foo(&self);
1596+
fn bar(&self);
1597+
fn baz(&self);
1598+
}
1599+
1600+
struct S;
1601+
1602+
impl Foo for S {
1603+
fn bar(&self) {}
1604+
1605+
$0type Output;
1606+
1607+
const CONST_2: i32;
1608+
1609+
fn foo(&self) {
1610+
todo!()
1611+
}
1612+
1613+
fn baz(&self) {
1614+
todo!()
1615+
}
1616+
1617+
}
1618+
}"#,
1619+
);
1620+
}
15211621
}

crates/ide-assists/src/utils.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ use stdx::format_to;
99
use syntax::{
1010
ast::{
1111
self,
12-
edit::{self, AstNodeEdit},
12+
edit::{AstNodeEdit, IndentLevel},
1313
edit_in_place::{AttrsOwnerEdit, Removable},
1414
make, HasArgList, HasAttrs, HasGenericParams, HasName, HasTypeBounds, Whitespace,
1515
},
@@ -154,7 +154,7 @@ pub fn add_trait_assoc_items_to_impl(
154154
match &item {
155155
ast::AssocItem::Fn(fn_) if fn_.body().is_none() => {
156156
let body = make::block_expr(None, Some(make::ext::expr_todo()))
157-
.indent(edit::IndentLevel(1));
157+
.indent(IndentLevel::from_node(impl_.syntax()) + 1);
158158
ted::replace(fn_.get_or_create_body().syntax(), body.clone_for_update().syntax())
159159
}
160160
ast::AssocItem::TypeAlias(type_alias) => {

crates/syntax/src/ast/edit_in_place.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -465,6 +465,8 @@ impl ast::Impl {
465465
}
466466

467467
impl ast::AssocItemList {
468+
/// Attention! This function does align the first line of `item` with respect to `self`,
469+
/// but it does _not_ change indentation of other lines (if any).
468470
pub fn add_item(&self, item: ast::AssocItem) {
469471
let (indent, position, whitespace) = match self.assoc_items().last() {
470472
Some(last_item) => (

0 commit comments

Comments
 (0)