|
1 | 1 | use ide_db::famous_defs::FamousDefs;
|
2 | 2 | use syntax::{
|
3 | 3 | AstNode,
|
4 |
| - ast::{self, make}, |
| 4 | + ast::{self, edit_in_place::Indent, make}, |
5 | 5 | ted,
|
6 | 6 | };
|
7 | 7 |
|
@@ -46,6 +46,7 @@ use crate::{AssistContext, AssistId, Assists};
|
46 | 46 | // ```
|
47 | 47 | pub(crate) fn generate_mut_trait_impl(acc: &mut Assists, ctx: &AssistContext<'_>) -> Option<()> {
|
48 | 48 | let impl_def = ctx.find_node_at_offset::<ast::Impl>()?.clone_for_update();
|
| 49 | + let indent = impl_def.indent_level(); |
49 | 50 |
|
50 | 51 | let trait_ = impl_def.trait_()?;
|
51 | 52 | if let ast::Type::PathType(trait_path) = trait_ {
|
@@ -97,16 +98,16 @@ pub(crate) fn generate_mut_trait_impl(acc: &mut Assists, ctx: &AssistContext<'_>
|
97 | 98 | })?;
|
98 | 99 |
|
99 | 100 | let assoc_list = make::assoc_item_list().clone_for_update();
|
100 |
| - assoc_list.add_item(syntax::ast::AssocItem::Fn(fn_)); |
101 | 101 | ted::replace(impl_def.assoc_item_list()?.syntax(), assoc_list.syntax());
|
| 102 | + impl_def.get_or_create_assoc_item_list().add_item(syntax::ast::AssocItem::Fn(fn_)); |
102 | 103 |
|
103 | 104 | let target = impl_def.syntax().text_range();
|
104 | 105 | acc.add(
|
105 | 106 | AssistId::generate("generate_mut_trait_impl"),
|
106 | 107 | "Generate `IndexMut` impl from this `Index` trait",
|
107 | 108 | target,
|
108 | 109 | |edit| {
|
109 |
| - edit.insert(target.start(), format!("$0{impl_def}\n\n")); |
| 110 | + edit.insert(target.start(), format!("$0{impl_def}\n\n{indent}")); |
110 | 111 | },
|
111 | 112 | )
|
112 | 113 | }
|
@@ -189,6 +190,92 @@ impl<T> core::ops::Index<Axis> for [T; 3] where T: Copy {
|
189 | 190 | );
|
190 | 191 | }
|
191 | 192 |
|
| 193 | + #[test] |
| 194 | + fn test_generate_mut_trait_impl_non_zero_indent() { |
| 195 | + check_assist( |
| 196 | + generate_mut_trait_impl, |
| 197 | + r#" |
| 198 | +//- minicore: index |
| 199 | +mod foo { |
| 200 | + pub enum Axis { X = 0, Y = 1, Z = 2 } |
| 201 | +
|
| 202 | + impl<T> core::ops::Index$0<Axis> for [T; 3] where T: Copy { |
| 203 | + type Output = T; |
| 204 | +
|
| 205 | + fn index(&self, index: Axis) -> &Self::Output { |
| 206 | + let var_name = &self[index as usize]; |
| 207 | + var_name |
| 208 | + } |
| 209 | + } |
| 210 | +} |
| 211 | +"#, |
| 212 | + r#" |
| 213 | +mod foo { |
| 214 | + pub enum Axis { X = 0, Y = 1, Z = 2 } |
| 215 | +
|
| 216 | + $0impl<T> core::ops::IndexMut<Axis> for [T; 3] where T: Copy { |
| 217 | + fn index_mut(&mut self, index: Axis) -> &mut Self::Output { |
| 218 | + let var_name = &self[index as usize]; |
| 219 | + var_name |
| 220 | + } |
| 221 | + } |
| 222 | +
|
| 223 | + impl<T> core::ops::Index<Axis> for [T; 3] where T: Copy { |
| 224 | + type Output = T; |
| 225 | +
|
| 226 | + fn index(&self, index: Axis) -> &Self::Output { |
| 227 | + let var_name = &self[index as usize]; |
| 228 | + var_name |
| 229 | + } |
| 230 | + } |
| 231 | +} |
| 232 | +"#, |
| 233 | + ); |
| 234 | + check_assist( |
| 235 | + generate_mut_trait_impl, |
| 236 | + r#" |
| 237 | +//- minicore: index |
| 238 | +mod foo { |
| 239 | + mod bar { |
| 240 | + pub enum Axis { X = 0, Y = 1, Z = 2 } |
| 241 | +
|
| 242 | + impl<T> core::ops::Index$0<Axis> for [T; 3] where T: Copy { |
| 243 | + type Output = T; |
| 244 | +
|
| 245 | + fn index(&self, index: Axis) -> &Self::Output { |
| 246 | + let var_name = &self[index as usize]; |
| 247 | + var_name |
| 248 | + } |
| 249 | + } |
| 250 | + } |
| 251 | +} |
| 252 | +"#, |
| 253 | + r#" |
| 254 | +mod foo { |
| 255 | + mod bar { |
| 256 | + pub enum Axis { X = 0, Y = 1, Z = 2 } |
| 257 | +
|
| 258 | + $0impl<T> core::ops::IndexMut<Axis> for [T; 3] where T: Copy { |
| 259 | + fn index_mut(&mut self, index: Axis) -> &mut Self::Output { |
| 260 | + let var_name = &self[index as usize]; |
| 261 | + var_name |
| 262 | + } |
| 263 | + } |
| 264 | +
|
| 265 | + impl<T> core::ops::Index<Axis> for [T; 3] where T: Copy { |
| 266 | + type Output = T; |
| 267 | +
|
| 268 | + fn index(&self, index: Axis) -> &Self::Output { |
| 269 | + let var_name = &self[index as usize]; |
| 270 | + var_name |
| 271 | + } |
| 272 | + } |
| 273 | + } |
| 274 | +} |
| 275 | +"#, |
| 276 | + ); |
| 277 | + } |
| 278 | + |
192 | 279 | #[test]
|
193 | 280 | fn test_generate_mut_trait_impl_not_applicable() {
|
194 | 281 | check_assist_not_applicable(
|
|
0 commit comments