add_missing_impl_members and add_missing_default_members break indentation no longer

This commit is contained in:
ponyii 2023-05-10 22:27:05 +04:00
parent e07d6382de
commit 49f1092f14
2 changed files with 106 additions and 38 deletions

View File

@ -1346,8 +1346,8 @@ struct SomeStruct {
} }
impl PartialEq for SomeStruct { impl PartialEq for SomeStruct {
$0fn ne(&self, other: &Self) -> bool { $0fn ne(&self, other: &Self) -> bool {
!self.eq(other) !self.eq(other)
} }
} }
"#, "#,
); );
@ -1520,48 +1520,47 @@ fn main() {
} }
#[test] #[test]
fn test_add_missing_impl_members_indentation() { fn test_add_missing_preserves_indentation() {
// few trait members, no braces // in different modules
check_assist( check_assist(
add_missing_impl_members, add_missing_impl_members,
r#" r#"
mod m { mod m {
trait Foo { fn foo(&self); } pub trait Foo {
struct S; const CONST_MULTILINE: (
impl Foo for S$0 i32,
}"#, i32
);
fn foo(&self);
}
}
struct S;
impl m::Foo for S { $0 }"#,
r#" r#"
mod m { mod m {
trait Foo { fn foo(&self); } pub trait Foo {
struct S; const CONST_MULTILINE: (
impl Foo for S { i32,
fn foo(&self) { i32
${0:todo!()} );
}
fn foo(&self);
}
}
struct S;
impl m::Foo for S {
$0const CONST_MULTILINE: (
i32,
i32
);
fn foo(&self) {
todo!()
} }
}"#, }"#,
); );
// few trait members, empty impl def. // in the same module
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( check_assist(
add_missing_impl_members, add_missing_impl_members,
r#" r#"
@ -1571,6 +1570,10 @@ mod m {
const CONST: usize = 42; const CONST: usize = 42;
const CONST_2: i32; const CONST_2: i32;
const CONST_MULTILINE: (
i32,
i32
);
fn foo(&self); fn foo(&self);
fn bar(&self); fn bar(&self);
@ -1591,6 +1594,10 @@ mod m {
const CONST: usize = 42; const CONST: usize = 42;
const CONST_2: i32; const CONST_2: i32;
const CONST_MULTILINE: (
i32,
i32
);
fn foo(&self); fn foo(&self);
fn bar(&self); fn bar(&self);
@ -1606,6 +1613,11 @@ mod m {
const CONST_2: i32; const CONST_2: i32;
const CONST_MULTILINE: (
i32,
i32
);
fn foo(&self) { fn foo(&self) {
todo!() todo!()
} }
@ -1618,4 +1630,56 @@ mod m {
}"#, }"#,
); );
} }
#[test]
fn test_add_default_preserves_indentation() {
check_assist(
add_missing_default_members,
r#"
mod m {
pub trait Foo {
type Output;
const CONST: usize = 42;
const CONST_2: i32;
const CONST_MULTILINE: = (
i32,
i32,
) = (3, 14);
fn valid(some: u32) -> bool { false }
fn foo(some: u32) -> bool;
}
}
struct S;
impl m::Foo for S { $0 }"#,
r#"
mod m {
pub trait Foo {
type Output;
const CONST: usize = 42;
const CONST_2: i32;
const CONST_MULTILINE: = (
i32,
i32,
) = (3, 14);
fn valid(some: u32) -> bool { false }
fn foo(some: u32) -> bool;
}
}
struct S;
impl m::Foo for S {
$0const CONST: usize = 42;
const CONST_MULTILINE: = (
i32,
i32,
) = (3, 14);
fn valid(some: u32) -> bool { false }
}"#,
)
}
} }

View File

@ -10,7 +10,7 @@ use syntax::{
ast::{ ast::{
self, self,
edit::{AstNodeEdit, IndentLevel}, edit::{AstNodeEdit, IndentLevel},
edit_in_place::{AttrsOwnerEdit, Removable}, edit_in_place::{AttrsOwnerEdit, Indent, Removable},
make, HasArgList, HasAttrs, HasGenericParams, HasName, HasTypeBounds, Whitespace, make, HasArgList, HasAttrs, HasGenericParams, HasName, HasTypeBounds, Whitespace,
}, },
ted, AstNode, AstToken, Direction, SourceFile, ted, AstNode, AstToken, Direction, SourceFile,
@ -139,9 +139,11 @@ pub fn add_trait_assoc_items_to_impl(
let transform = PathTransform::trait_impl(&target_scope, &source_scope, trait_, impl_.clone()); let transform = PathTransform::trait_impl(&target_scope, &source_scope, trait_, impl_.clone());
let new_indent_level = IndentLevel::from_node(impl_.syntax()) + 1;
let items = items.into_iter().map(|assoc_item| { let items = items.into_iter().map(|assoc_item| {
transform.apply(assoc_item.syntax()); transform.apply(assoc_item.syntax());
assoc_item.remove_attrs_and_docs(); assoc_item.remove_attrs_and_docs();
assoc_item.reindent_to(new_indent_level);
assoc_item assoc_item
}); });
@ -153,8 +155,10 @@ pub fn add_trait_assoc_items_to_impl(
first_item.get_or_insert_with(|| item.clone()); first_item.get_or_insert_with(|| item.clone());
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 = AstNodeEdit::indent(
.indent(IndentLevel::from_node(impl_.syntax()) + 1); &make::block_expr(None, Some(make::ext::expr_todo())),
new_indent_level,
);
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) => {