From 3d6d4e9855db134a07763d2650ada2a7ea9cfbd0 Mon Sep 17 00:00:00 2001 From: Lukas Tobias Wirth Date: Mon, 3 May 2021 18:22:18 +0200 Subject: Don't mutate the tree while traversing in reorder_impl --- crates/ide_assists/src/handlers/reorder_impl.rs | 40 ++++++++++++++++++++++--- 1 file changed, 36 insertions(+), 4 deletions(-) (limited to 'crates/ide_assists') diff --git a/crates/ide_assists/src/handlers/reorder_impl.rs b/crates/ide_assists/src/handlers/reorder_impl.rs index 72d889248..54a9a468e 100644 --- a/crates/ide_assists/src/handlers/reorder_impl.rs +++ b/crates/ide_assists/src/handlers/reorder_impl.rs @@ -79,9 +79,12 @@ pub(crate) fn reorder_impl(acc: &mut Assists, ctx: &AssistContext) -> Option<()> "Sort methods", target, |builder| { - methods.into_iter().zip(sorted).for_each(|(old, new)| { - ted::replace(builder.make_ast_mut(old).syntax(), new.clone_for_update().syntax()) - }); + let methods = + methods.into_iter().map(|fn_| builder.make_ast_mut(fn_)).collect::>(); + 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] - fn reorder_impl_trait_methods() { + fn reorder_impl_trait_functions() { check_assist( reorder_impl, 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) {} +}"#, + ) + } } -- cgit v1.2.3