From e90e1901efe4d98c6209e74a098a54b4d4d73a97 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E8=94=A1=E7=95=A5?= Date: Sun, 9 Apr 2023 00:14:48 +0800 Subject: [PATCH 1/2] feat: restrict applicable range of `reorder-impl-trait-items` --- .../src/handlers/reorder_impl_items.rs | 41 +++++++++++++++++-- crates/ide-assists/src/tests/generated.rs | 2 +- 2 files changed, 39 insertions(+), 4 deletions(-) diff --git a/crates/ide-assists/src/handlers/reorder_impl_items.rs b/crates/ide-assists/src/handlers/reorder_impl_items.rs index 208c3e109d..4fd1d2a081 100644 --- a/crates/ide-assists/src/handlers/reorder_impl_items.rs +++ b/crates/ide-assists/src/handlers/reorder_impl_items.rs @@ -21,7 +21,7 @@ use crate::{AssistContext, AssistId, AssistKind, Assists}; // } // // struct Bar; -// $0impl Foo for Bar { +// $0impl Foo for Bar$0 { // const B: u8 = 17; // fn c() {} // type A = String; @@ -45,6 +45,16 @@ use crate::{AssistContext, AssistId, AssistKind, Assists}; pub(crate) fn reorder_impl_items(acc: &mut Assists, ctx: &AssistContext<'_>) -> Option<()> { let impl_ast = ctx.find_node_at_offset::()?; let items = impl_ast.assoc_item_list()?; + + // restrict the range + // if cursor is in assoc_items, abort + let assoc_range = items.syntax().text_range(); + let cursor_position = ctx.offset(); + if assoc_range.contains_inclusive(cursor_position) { + cov_mark::hit!(not_applicable_editing_assoc_items); + return None; + } + let assoc_items = items.assoc_items().collect::>(); let path = impl_ast @@ -264,9 +274,9 @@ trait Bar { } struct Foo; -impl Bar for Foo { +$0impl Bar for Foo { type Fooo = (); - type Foo = ();$0 + type Foo = (); }"#, r#" trait Bar { @@ -281,4 +291,29 @@ impl Bar for Foo { }"#, ) } + + #[test] + fn not_applicable_editing_assoc_items() { + cov_mark::check!(not_applicable_editing_assoc_items); + check_assist_not_applicable( + reorder_impl_items, + r#" +trait Bar { + type T; + const C: (); + fn a() {} + fn z() {} + fn b() {} +} +struct Foo; +impl Bar for Foo { + type T = ();$0 + const C: () = (); + fn a() {} + fn z() {} + fn b() {} +} + "#, + ) + } } diff --git a/crates/ide-assists/src/tests/generated.rs b/crates/ide-assists/src/tests/generated.rs index f093dfddfa..0096254ecb 100644 --- a/crates/ide-assists/src/tests/generated.rs +++ b/crates/ide-assists/src/tests/generated.rs @@ -2141,7 +2141,7 @@ trait Foo { } struct Bar; -$0impl Foo for Bar { +$0impl Foo for Bar$0 { const B: u8 = 17; fn c() {} type A = String; From 475aa2839f3edd814369f067fc382f576dbb31de Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E8=94=A1=E7=95=A5?= Date: Sun, 9 Apr 2023 00:24:25 +0800 Subject: [PATCH 2/2] refactor: correct test sample MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit giving a sorted file is useless Signed-off-by: 蔡略 --- crates/ide-assists/src/handlers/reorder_impl_items.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/crates/ide-assists/src/handlers/reorder_impl_items.rs b/crates/ide-assists/src/handlers/reorder_impl_items.rs index 4fd1d2a081..af96950761 100644 --- a/crates/ide-assists/src/handlers/reorder_impl_items.rs +++ b/crates/ide-assists/src/handlers/reorder_impl_items.rs @@ -309,8 +309,8 @@ struct Foo; impl Bar for Foo { type T = ();$0 const C: () = (); - fn a() {} fn z() {} + fn a() {} fn b() {} } "#,