Skip to content

Commit e90e190

Browse files
committed
feat: restrict applicable range of reorder-impl-trait-items
1 parent 01120f1 commit e90e190

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

Lines changed: 38 additions & 3 deletions
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 a() {}
313+
fn z() {}
314+
fn b() {}
315+
}
316+
"#,
317+
)
318+
}
284319
}

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

Lines changed: 1 addition & 1 deletion
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)