mirror of
https://github.com/rust-lang/rust-analyzer.git
synced 2025-10-01 11:31:15 +00:00
Auto merge of #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: #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 is available. And it is reordering impl items.  - cursor in `impl` associated items 
This commit is contained in:
commit
fa3db447d7
@ -21,7 +21,7 @@ use crate::{AssistContext, AssistId, AssistKind, Assists};
|
|||||||
// }
|
// }
|
||||||
//
|
//
|
||||||
// struct Bar;
|
// struct Bar;
|
||||||
// $0impl Foo for Bar {
|
// $0impl Foo for Bar$0 {
|
||||||
// const B: u8 = 17;
|
// const B: u8 = 17;
|
||||||
// fn c() {}
|
// fn c() {}
|
||||||
// type A = String;
|
// 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<()> {
|
pub(crate) fn reorder_impl_items(acc: &mut Assists, ctx: &AssistContext<'_>) -> Option<()> {
|
||||||
let impl_ast = ctx.find_node_at_offset::<ast::Impl>()?;
|
let impl_ast = ctx.find_node_at_offset::<ast::Impl>()?;
|
||||||
let items = impl_ast.assoc_item_list()?;
|
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 assoc_items = items.assoc_items().collect::<Vec<_>>();
|
||||||
|
|
||||||
let path = impl_ast
|
let path = impl_ast
|
||||||
@ -264,9 +274,9 @@ trait Bar {
|
|||||||
}
|
}
|
||||||
|
|
||||||
struct Foo;
|
struct Foo;
|
||||||
impl Bar for Foo {
|
$0impl Bar for Foo {
|
||||||
type Fooo = ();
|
type Fooo = ();
|
||||||
type Foo = ();$0
|
type Foo = ();
|
||||||
}"#,
|
}"#,
|
||||||
r#"
|
r#"
|
||||||
trait Bar {
|
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 z() {}
|
||||||
|
fn a() {}
|
||||||
|
fn b() {}
|
||||||
|
}
|
||||||
|
"#,
|
||||||
|
)
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
@ -2141,7 +2141,7 @@ trait Foo {
|
|||||||
}
|
}
|
||||||
|
|
||||||
struct Bar;
|
struct Bar;
|
||||||
$0impl Foo for Bar {
|
$0impl Foo for Bar$0 {
|
||||||
const B: u8 = 17;
|
const B: u8 = 17;
|
||||||
fn c() {}
|
fn c() {}
|
||||||
type A = String;
|
type A = String;
|
||||||
|
Loading…
x
Reference in New Issue
Block a user