diff options
| author | 蔡略 <cailue@bupt.edu.cn> | 2023-04-09 00:14:48 +0800 |
|---|---|---|
| committer | 蔡略 <cailue@bupt.edu.cn> | 2023-04-09 00:14:48 +0800 |
| commit | e90e1901efe4d98c6209e74a098a54b4d4d73a97 (patch) | |
| tree | d32804c221747cbb34f976cedc7ab07d893754f2 | |
| parent | 01120f1213ad928de7300a8acf9f41bed72d0422 (diff) | |
| download | rust-e90e1901efe4d98c6209e74a098a54b4d4d73a97.tar.gz rust-e90e1901efe4d98c6209e74a098a54b4d4d73a97.zip | |
feat: restrict applicable range of `reorder-impl-trait-items`
| -rw-r--r-- | crates/ide-assists/src/handlers/reorder_impl_items.rs | 41 | ||||
| -rw-r--r-- | 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 208c3e109dd..4fd1d2a0816 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::<ast::Impl>()?; 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::<Vec<_>>(); 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 f093dfddfae..0096254ecb7 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; |
