Skip to content

Commit fa3db44

Browse files
committed
Auto merge of rust-lang#14536 - ClSlaid:feat/reorder-impl-items/not-applicative-editing-assoc-items, r=Veykril
fix: restrict applicable range of `reorder-impl-trait-items` This PR should complete the need for restricting the applicable range of `reorder-impl-trait-items`. When the cursor is in the associated items of the `impl` range, the assist will be disabled. Fix: rust-lang#14515 ## Showcases Note: If there is any available `code-action` (`ide-assist`) available, a lightbulb icon from `lspsaga` will show in the left. - cursor in `impl` headers ![code action available](https://user-images.githubusercontent.com/44747719/230756854-7b236018-cfa8-4005-b589-2996ec42917f.png) Code action is available. And it is reordering impl items. ![code action detail](https://user-images.githubusercontent.com/44747719/230756971-341c7fbc-f2ba-4715-a1e5-b1add984d4dd.png) - cursor in `impl` associated items ![code action unavailable]( https://user-images.githubusercontent.com/44747719/230756906-bee7784e-bd9d-49b2-801b-743c94b4af54.png)
2 parents 208a74c + 475aa28 commit fa3db44

File tree

2 files changed

+39
-4
lines changed

2 files changed

+39
-4
lines changed

crates/ide-assists/src/handlers/reorder_impl_items.rs

+38-3
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ use crate::{AssistContext, AssistId, AssistKind, Assists};
2121
// }
2222
//
2323
// struct Bar;
24-
// $0impl Foo for Bar {
24+
// $0impl Foo for Bar$0 {
2525
// const B: u8 = 17;
2626
// fn c() {}
2727
// type A = String;
@@ -45,6 +45,16 @@ use crate::{AssistContext, AssistId, AssistKind, Assists};
4545
pub(crate) fn reorder_impl_items(acc: &mut Assists, ctx: &AssistContext<'_>) -> Option<()> {
4646
let impl_ast = ctx.find_node_at_offset::<ast::Impl>()?;
4747
let items = impl_ast.assoc_item_list()?;
48+
49+
// restrict the range
50+
// if cursor is in assoc_items, abort
51+
let assoc_range = items.syntax().text_range();
52+
let cursor_position = ctx.offset();
53+
if assoc_range.contains_inclusive(cursor_position) {
54+
cov_mark::hit!(not_applicable_editing_assoc_items);
55+
return None;
56+
}
57+
4858
let assoc_items = items.assoc_items().collect::<Vec<_>>();
4959

5060
let path = impl_ast
@@ -264,9 +274,9 @@ trait Bar {
264274
}
265275
266276
struct Foo;
267-
impl Bar for Foo {
277+
$0impl Bar for Foo {
268278
type Fooo = ();
269-
type Foo = ();$0
279+
type Foo = ();
270280
}"#,
271281
r#"
272282
trait Bar {
@@ -281,4 +291,29 @@ impl Bar for Foo {
281291
}"#,
282292
)
283293
}
294+
295+
#[test]
296+
fn not_applicable_editing_assoc_items() {
297+
cov_mark::check!(not_applicable_editing_assoc_items);
298+
check_assist_not_applicable(
299+
reorder_impl_items,
300+
r#"
301+
trait Bar {
302+
type T;
303+
const C: ();
304+
fn a() {}
305+
fn z() {}
306+
fn b() {}
307+
}
308+
struct Foo;
309+
impl Bar for Foo {
310+
type T = ();$0
311+
const C: () = ();
312+
fn z() {}
313+
fn a() {}
314+
fn b() {}
315+
}
316+
"#,
317+
)
318+
}
284319
}

crates/ide-assists/src/tests/generated.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -2141,7 +2141,7 @@ trait Foo {
21412141
}
21422142
21432143
struct Bar;
2144-
$0impl Foo for Bar {
2144+
$0impl Foo for Bar$0 {
21452145
const B: u8 = 17;
21462146
fn c() {}
21472147
type A = String;

0 commit comments

Comments
 (0)