1
1
use ide_db:: { famous_defs:: FamousDefs , traits:: resolve_target_trait} ;
2
2
use syntax:: {
3
- AstNode ,
3
+ AstNode , T ,
4
4
ast:: { self , edit_in_place:: Indent , make} ,
5
5
ted,
6
6
} ;
@@ -32,7 +32,7 @@ use crate::{AssistContext, AssistId, Assists};
32
32
//
33
33
// $0impl<T> core::ops::IndexMut<Axis> for [T; 3] {
34
34
// fn index_mut(&mut self, index: Axis) -> &mut Self::Output {
35
- // &self[index as usize]
35
+ // &mut self[index as usize]
36
36
// }
37
37
// }
38
38
//
@@ -92,6 +92,7 @@ pub(crate) fn generate_mut_trait_impl(acc: &mut Assists, ctx: &AssistContext<'_>
92
92
ast:: AssocItem :: Fn ( f) => Some ( f) ,
93
93
_ => None ,
94
94
} ) ?;
95
+ let _ = process_ref_mut ( & fn_) ;
95
96
96
97
let assoc_list = make:: assoc_item_list ( ) . clone_for_update ( ) ;
97
98
ted:: replace ( impl_def. assoc_item_list ( ) ?. syntax ( ) , assoc_list. syntax ( ) ) ;
@@ -108,6 +109,20 @@ pub(crate) fn generate_mut_trait_impl(acc: &mut Assists, ctx: &AssistContext<'_>
108
109
)
109
110
}
110
111
112
+ fn process_ref_mut ( fn_ : & ast:: Fn ) -> Option < ( ) > {
113
+ let expr = fn_. body ( ) ?. tail_expr ( ) ?;
114
+ match & expr {
115
+ ast:: Expr :: RefExpr ( ref_expr) if ref_expr. mut_token ( ) . is_none ( ) => {
116
+ ted:: insert_all_raw (
117
+ ted:: Position :: after ( ref_expr. amp_token ( ) ?) ,
118
+ vec ! [ make:: token( T ![ mut ] ) . into( ) , make:: tokens:: whitespace( " " ) . into( ) ] ,
119
+ ) ;
120
+ }
121
+ _ => { }
122
+ }
123
+ None
124
+ }
125
+
111
126
fn get_trait_mut ( apply_trait : & hir:: Trait , famous : FamousDefs < ' _ , ' _ > ) -> Option < & ' static str > {
112
127
let trait_ = Some ( apply_trait) ;
113
128
if trait_ == famous. core_convert_Index ( ) . as_ref ( ) {
@@ -167,7 +182,7 @@ pub enum Axis { X = 0, Y = 1, Z = 2 }
167
182
168
183
$0impl<T> core::ops::IndexMut<Axis> for [T; 3] {
169
184
fn index_mut(&mut self, index: Axis) -> &mut Self::Output {
170
- &self[index as usize]
185
+ &mut self[index as usize]
171
186
}
172
187
}
173
188
@@ -234,7 +249,7 @@ struct Foo(i32);
234
249
235
250
$0impl core::convert::AsMut<i32> for Foo {
236
251
fn as_mut(&mut self) -> &mut i32 {
237
- &self.0
252
+ &mut self.0
238
253
}
239
254
}
240
255
0 commit comments