add_missing_impl_members no longer breaks indentation

This commit is contained in:
ponyii 2023-05-08 22:26:50 +04:00
parent a0db4781dc
commit e07d6382de
3 changed files with 106 additions and 4 deletions

View File

@ -1518,4 +1518,104 @@ fn method() {
"#, "#,
); );
} }
#[test]
fn test_add_missing_impl_members_indentation() {
// few trait members, no braces
check_assist(
add_missing_impl_members,
r#"
mod m {
trait Foo { fn foo(&self); }
struct S;
impl Foo for S$0
}"#,
r#"
mod m {
trait Foo { fn foo(&self); }
struct S;
impl Foo for S {
fn foo(&self) {
${0:todo!()}
}
}
}"#,
);
// few trait members, empty impl def.
check_assist(
add_missing_impl_members,
r#"
mod m {
trait Foo { fn foo(&self); }
struct S;
impl Foo for S { $0 }
}"#,
r#"
mod m {
trait Foo { fn foo(&self); }
struct S;
impl Foo for S {
fn foo(&self) {
${0:todo!()}
}
}
}"#,
);
// todo - in mod and outside
check_assist(
add_missing_impl_members,
r#"
mod m {
trait Foo {
type Output;
const CONST: usize = 42;
const CONST_2: i32;
fn foo(&self);
fn bar(&self);
fn baz(&self);
}
struct S;
impl Foo for S {
fn bar(&self) {}
$0
}
}"#,
r#"
mod m {
trait Foo {
type Output;
const CONST: usize = 42;
const CONST_2: i32;
fn foo(&self);
fn bar(&self);
fn baz(&self);
}
struct S;
impl Foo for S {
fn bar(&self) {}
$0type Output;
const CONST_2: i32;
fn foo(&self) {
todo!()
}
fn baz(&self) {
todo!()
}
}
}"#,
);
}
} }

View File

@ -9,7 +9,7 @@
use syntax::{ use syntax::{
ast::{ ast::{
self, self,
edit::{self, AstNodeEdit}, edit::{AstNodeEdit, IndentLevel},
edit_in_place::{AttrsOwnerEdit, Removable}, edit_in_place::{AttrsOwnerEdit, Removable},
make, HasArgList, HasAttrs, HasGenericParams, HasName, HasTypeBounds, Whitespace, make, HasArgList, HasAttrs, HasGenericParams, HasName, HasTypeBounds, Whitespace,
}, },
@ -154,7 +154,7 @@ pub fn add_trait_assoc_items_to_impl(
match &item { match &item {
ast::AssocItem::Fn(fn_) if fn_.body().is_none() => { ast::AssocItem::Fn(fn_) if fn_.body().is_none() => {
let body = make::block_expr(None, Some(make::ext::expr_todo())) let body = make::block_expr(None, Some(make::ext::expr_todo()))
.indent(edit::IndentLevel(1)); .indent(IndentLevel::from_node(impl_.syntax()) + 1);
ted::replace(fn_.get_or_create_body().syntax(), body.clone_for_update().syntax()) ted::replace(fn_.get_or_create_body().syntax(), body.clone_for_update().syntax())
} }
ast::AssocItem::TypeAlias(type_alias) => { ast::AssocItem::TypeAlias(type_alias) => {

View File

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