Don't mutate the tree while traversing in reorder_impl

This commit is contained in:
Lukas Tobias Wirth 2021-05-03 18:22:18 +02:00
parent 544a93ee08
commit 3d6d4e9855

View file

@ -79,9 +79,12 @@ pub(crate) fn reorder_impl(acc: &mut Assists, ctx: &AssistContext) -> Option<()>
"Sort methods", "Sort methods",
target, target,
|builder| { |builder| {
methods.into_iter().zip(sorted).for_each(|(old, new)| { let methods =
ted::replace(builder.make_ast_mut(old).syntax(), new.clone_for_update().syntax()) methods.into_iter().map(|fn_| builder.make_ast_mut(fn_)).collect::<Vec<_>>();
}); methods
.into_iter()
.zip(sorted)
.for_each(|(old, new)| ted::replace(old.syntax(), new.clone_for_update().syntax()));
}, },
) )
} }
@ -160,7 +163,7 @@ $0impl Bar for Foo {}
} }
#[test] #[test]
fn reorder_impl_trait_methods() { fn reorder_impl_trait_functions() {
check_assist( check_assist(
reorder_impl, reorder_impl,
r#" r#"
@ -197,4 +200,33 @@ impl Bar for Foo {
"#, "#,
) )
} }
#[test]
fn reorder_impl_trait_methods_uneven_ident_lengths() {
check_assist(
reorder_impl,
r#"
trait Bar {
fn foo(&mut self) {}
fn fooo(&mut self) {}
}
struct Foo;
impl Bar for Foo {
fn fooo(&mut self) {}
fn foo(&mut self) {$0}
}"#,
r#"
trait Bar {
fn foo(&mut self) {}
fn fooo(&mut self) {}
}
struct Foo;
impl Bar for Foo {
fn foo(&mut self) {}
fn fooo(&mut self) {}
}"#,
)
}
} }