1
+ use ide_db:: famous_defs:: FamousDefs ;
1
2
use syntax:: {
2
3
ast:: { self , make} ,
3
4
ted, AstNode ,
4
5
} ;
5
6
6
7
use crate :: { AssistContext , AssistId , AssistKind , Assists } ;
7
8
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].
9
10
// Here just leave the `index_mut` method body be same as `index` method body, user can modify it manually to meet their need.
10
11
11
12
// Assist: generate_mut_trait_impl
12
13
//
13
14
// Adds a IndexMut impl from the `Index` trait.
14
15
//
15
16
// ```
17
+ // # //- minicore: index
16
18
// pub enum Axis { X = 0, Y = 1, Z = 2 }
17
19
//
18
- // impl<T> Index$0<Axis> for [T; 3] {
20
+ // impl<T> core::ops:: Index$0<Axis> for [T; 3] {
19
21
// type Output = T;
20
22
//
21
23
// fn index(&self, index: Axis) -> &Self::Output {
@@ -27,13 +29,13 @@ use crate::{AssistContext, AssistId, AssistKind, Assists};
27
29
// ```
28
30
// pub enum Axis { X = 0, Y = 1, Z = 2 }
29
31
//
30
- // $0impl<T> IndexMut<Axis> for [T; 3] {
32
+ // $0impl<T> core::ops:: IndexMut<Axis> for [T; 3] {
31
33
// fn index_mut(&mut self, index: Axis) -> &mut Self::Output {
32
34
// &self[index as usize]
33
35
// }
34
36
// }
35
37
//
36
- // impl<T> Index<Axis> for [T; 3] {
38
+ // impl<T> core::ops:: Index<Axis> for [T; 3] {
37
39
// type Output = T;
38
40
//
39
41
// fn index(&self, index: Axis) -> &Self::Output {
@@ -45,9 +47,10 @@ pub(crate) fn generate_mut_trait_impl(acc: &mut Assists, ctx: &AssistContext<'_>
45
47
let impl_def = ctx. find_node_at_offset :: < ast:: Impl > ( ) ?. clone_for_update ( ) ;
46
48
47
49
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 ( ) ? {
51
54
return None ;
52
55
}
53
56
}
@@ -118,9 +121,10 @@ mod tests {
118
121
check_assist (
119
122
generate_mut_trait_impl,
120
123
r#"
124
+ //- minicore: index
121
125
pub enum Axis { X = 0, Y = 1, Z = 2 }
122
126
123
- impl<T> Index$0<Axis> for [T; 3] {
127
+ impl<T> core::ops:: Index$0<Axis> for [T; 3] {
124
128
type Output = T;
125
129
126
130
fn index(&self, index: Axis) -> &Self::Output {
@@ -131,13 +135,13 @@ impl<T> Index$0<Axis> for [T; 3] {
131
135
r#"
132
136
pub enum Axis { X = 0, Y = 1, Z = 2 }
133
137
134
- $0impl<T> IndexMut<Axis> for [T; 3] {
138
+ $0impl<T> core::ops:: IndexMut<Axis> for [T; 3] {
135
139
fn index_mut(&mut self, index: Axis) -> &mut Self::Output {
136
140
&self[index as usize]
137
141
}
138
142
}
139
143
140
- impl<T> Index<Axis> for [T; 3] {
144
+ impl<T> core::ops:: Index<Axis> for [T; 3] {
141
145
type Output = T;
142
146
143
147
fn index(&self, index: Axis) -> &Self::Output {
@@ -150,9 +154,10 @@ impl<T> Index<Axis> for [T; 3] {
150
154
check_assist (
151
155
generate_mut_trait_impl,
152
156
r#"
157
+ //- minicore: index
153
158
pub enum Axis { X = 0, Y = 1, Z = 2 }
154
159
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 {
156
161
type Output = T;
157
162
158
163
fn index(&self, index: Axis) -> &Self::Output {
@@ -164,14 +169,14 @@ impl<T> Index$0<Axis> for [T; 3] where T: Copy {
164
169
r#"
165
170
pub enum Axis { X = 0, Y = 1, Z = 2 }
166
171
167
- $0impl<T> IndexMut<Axis> for [T; 3] where T: Copy {
172
+ $0impl<T> core::ops:: IndexMut<Axis> for [T; 3] where T: Copy {
168
173
fn index_mut(&mut self, index: Axis) -> &mut Self::Output {
169
174
let var_name = &self[index as usize];
170
175
var_name
171
176
}
172
177
}
173
178
174
- impl<T> Index<Axis> for [T; 3] where T: Copy {
179
+ impl<T> core::ops:: Index<Axis> for [T; 3] where T: Copy {
175
180
type Output = T;
176
181
177
182
fn index(&self, index: Axis) -> &Self::Output {
@@ -188,7 +193,9 @@ impl<T> Index<Axis> for [T; 3] where T: Copy {
188
193
check_assist_not_applicable (
189
194
generate_mut_trait_impl,
190
195
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] {}
192
199
"# ,
193
200
) ;
194
201
}
0 commit comments