Skip to content

Commit b84940b

Browse files
committed
make generate_mut_trait_impl assist trigged for std trait only
1 parent 8d8d121 commit b84940b

File tree

3 files changed

+29
-17
lines changed

3 files changed

+29
-17
lines changed

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

Lines changed: 21 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,23 @@
1+
use ide_db::famous_defs::FamousDefs;
12
use syntax::{
23
ast::{self, make},
34
ted, AstNode,
45
};
56

67
use crate::{AssistContext, AssistId, AssistKind, Assists};
78

8-
// Generate proper `index_mut` method body refer to `index` method body may impossible due to the unpredicable case [#15581].
9+
// FIXME: Generate proper `index_mut` method body refer to `index` method body may impossible due to the unpredicable case [#15581].
910
// Here just leave the `index_mut` method body be same as `index` method body, user can modify it manually to meet their need.
1011

1112
// Assist: generate_mut_trait_impl
1213
//
1314
// Adds a IndexMut impl from the `Index` trait.
1415
//
1516
// ```
17+
// # //- minicore: index
1618
// pub enum Axis { X = 0, Y = 1, Z = 2 }
1719
//
18-
// impl<T> Index$0<Axis> for [T; 3] {
20+
// impl<T> core::ops::Index$0<Axis> for [T; 3] {
1921
// type Output = T;
2022
//
2123
// fn index(&self, index: Axis) -> &Self::Output {
@@ -27,13 +29,13 @@ use crate::{AssistContext, AssistId, AssistKind, Assists};
2729
// ```
2830
// pub enum Axis { X = 0, Y = 1, Z = 2 }
2931
//
30-
// $0impl<T> IndexMut<Axis> for [T; 3] {
32+
// $0impl<T> core::ops::IndexMut<Axis> for [T; 3] {
3133
// fn index_mut(&mut self, index: Axis) -> &mut Self::Output {
3234
// &self[index as usize]
3335
// }
3436
// }
3537
//
36-
// impl<T> Index<Axis> for [T; 3] {
38+
// impl<T> core::ops::Index<Axis> for [T; 3] {
3739
// type Output = T;
3840
//
3941
// fn index(&self, index: Axis) -> &Self::Output {
@@ -45,9 +47,10 @@ pub(crate) fn generate_mut_trait_impl(acc: &mut Assists, ctx: &AssistContext<'_>
4547
let impl_def = ctx.find_node_at_offset::<ast::Impl>()?.clone_for_update();
4648

4749
let trait_ = impl_def.trait_()?;
48-
if let ast::Type::PathType(trait_type) = trait_.clone() {
49-
let trait_name = trait_type.path()?.segment()?.name_ref()?.to_string();
50-
if trait_name != "Index" {
50+
if let ast::Type::PathType(trait_path) = trait_.clone() {
51+
let trait_type = ctx.sema.resolve_trait(&trait_path.path()?)?;
52+
let scope = ctx.sema.scope(trait_path.syntax())?;
53+
if trait_type != FamousDefs(&ctx.sema, scope.krate()).core_convert_Index()? {
5154
return None;
5255
}
5356
}
@@ -118,9 +121,10 @@ mod tests {
118121
check_assist(
119122
generate_mut_trait_impl,
120123
r#"
124+
//- minicore: index
121125
pub enum Axis { X = 0, Y = 1, Z = 2 }
122126
123-
impl<T> Index$0<Axis> for [T; 3] {
127+
impl<T> core::ops::Index$0<Axis> for [T; 3] {
124128
type Output = T;
125129
126130
fn index(&self, index: Axis) -> &Self::Output {
@@ -131,13 +135,13 @@ impl<T> Index$0<Axis> for [T; 3] {
131135
r#"
132136
pub enum Axis { X = 0, Y = 1, Z = 2 }
133137
134-
$0impl<T> IndexMut<Axis> for [T; 3] {
138+
$0impl<T> core::ops::IndexMut<Axis> for [T; 3] {
135139
fn index_mut(&mut self, index: Axis) -> &mut Self::Output {
136140
&self[index as usize]
137141
}
138142
}
139143
140-
impl<T> Index<Axis> for [T; 3] {
144+
impl<T> core::ops::Index<Axis> for [T; 3] {
141145
type Output = T;
142146
143147
fn index(&self, index: Axis) -> &Self::Output {
@@ -150,9 +154,10 @@ impl<T> Index<Axis> for [T; 3] {
150154
check_assist(
151155
generate_mut_trait_impl,
152156
r#"
157+
//- minicore: index
153158
pub enum Axis { X = 0, Y = 1, Z = 2 }
154159
155-
impl<T> Index$0<Axis> for [T; 3] where T: Copy {
160+
impl<T> core::ops::Index$0<Axis> for [T; 3] where T: Copy {
156161
type Output = T;
157162
158163
fn index(&self, index: Axis) -> &Self::Output {
@@ -164,14 +169,14 @@ impl<T> Index$0<Axis> for [T; 3] where T: Copy {
164169
r#"
165170
pub enum Axis { X = 0, Y = 1, Z = 2 }
166171
167-
$0impl<T> IndexMut<Axis> for [T; 3] where T: Copy {
172+
$0impl<T> core::ops::IndexMut<Axis> for [T; 3] where T: Copy {
168173
fn index_mut(&mut self, index: Axis) -> &mut Self::Output {
169174
let var_name = &self[index as usize];
170175
var_name
171176
}
172177
}
173178
174-
impl<T> Index<Axis> for [T; 3] where T: Copy {
179+
impl<T> core::ops::Index<Axis> for [T; 3] where T: Copy {
175180
type Output = T;
176181
177182
fn index(&self, index: Axis) -> &Self::Output {
@@ -188,7 +193,9 @@ impl<T> Index<Axis> for [T; 3] where T: Copy {
188193
check_assist_not_applicable(
189194
generate_mut_trait_impl,
190195
r#"
191-
impl<T> Add$0<i32> for [T; 3] {}
196+
pub trait Index<Idx: ?Sized> {}
197+
198+
impl<T> Index$0<i32> for [T; 3] {}
192199
"#,
193200
);
194201
}

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

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1543,9 +1543,10 @@ fn doctest_generate_mut_trait_impl() {
15431543
check_doc_test(
15441544
"generate_mut_trait_impl",
15451545
r#####"
1546+
//- minicore: index
15461547
pub enum Axis { X = 0, Y = 1, Z = 2 }
15471548
1548-
impl<T> Index$0<Axis> for [T; 3] {
1549+
impl<T> core::ops::Index$0<Axis> for [T; 3] {
15491550
type Output = T;
15501551
15511552
fn index(&self, index: Axis) -> &Self::Output {
@@ -1556,13 +1557,13 @@ impl<T> Index$0<Axis> for [T; 3] {
15561557
r#####"
15571558
pub enum Axis { X = 0, Y = 1, Z = 2 }
15581559
1559-
$0impl<T> IndexMut<Axis> for [T; 3] {
1560+
$0impl<T> core::ops::IndexMut<Axis> for [T; 3] {
15601561
fn index_mut(&mut self, index: Axis) -> &mut Self::Output {
15611562
&self[index as usize]
15621563
}
15631564
}
15641565
1565-
impl<T> Index<Axis> for [T; 3] {
1566+
impl<T> core::ops::Index<Axis> for [T; 3] {
15661567
type Output = T;
15671568
15681569
fn index(&self, index: Axis) -> &Self::Output {

crates/ide-db/src/famous_defs.rs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,10 @@ impl FamousDefs<'_, '_> {
5454
self.find_trait("core:convert:Into")
5555
}
5656

57+
pub fn core_convert_Index(&self) -> Option<Trait> {
58+
self.find_trait("core:ops:Index")
59+
}
60+
5761
pub fn core_option_Option(&self) -> Option<Enum> {
5862
self.find_enum("core:option:Option")
5963
}

0 commit comments

Comments
 (0)