Skip to content

Commit 4e079e3

Browse files
committed
fix: ide-assists, generate mut trait impl indent
1 parent 2bafe9d commit 4e079e3

File tree

1 file changed

+91
-3
lines changed

1 file changed

+91
-3
lines changed

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

Lines changed: 91 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
use ide_db::famous_defs::FamousDefs;
22
use syntax::{
33
AstNode,
4-
ast::{self, make},
4+
ast::{self, edit_in_place::Indent, make},
55
ted,
66
};
77

@@ -46,6 +46,7 @@ use crate::{AssistContext, AssistId, Assists};
4646
// ```
4747
pub(crate) fn generate_mut_trait_impl(acc: &mut Assists, ctx: &AssistContext<'_>) -> Option<()> {
4848
let impl_def = ctx.find_node_at_offset::<ast::Impl>()?.clone_for_update();
49+
let indent = impl_def.indent_level();
4950

5051
let trait_ = impl_def.trait_()?;
5152
if let ast::Type::PathType(trait_path) = trait_ {
@@ -97,16 +98,16 @@ pub(crate) fn generate_mut_trait_impl(acc: &mut Assists, ctx: &AssistContext<'_>
9798
})?;
9899

99100
let assoc_list = make::assoc_item_list().clone_for_update();
100-
assoc_list.add_item(syntax::ast::AssocItem::Fn(fn_));
101101
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_));
102103

103104
let target = impl_def.syntax().text_range();
104105
acc.add(
105106
AssistId::generate("generate_mut_trait_impl"),
106107
"Generate `IndexMut` impl from this `Index` trait",
107108
target,
108109
|edit| {
109-
edit.insert(target.start(), format!("$0{impl_def}\n\n"));
110+
edit.insert(target.start(), format!("$0{impl_def}\n\n{indent}"));
110111
},
111112
)
112113
}
@@ -189,6 +190,93 @@ impl<T> core::ops::Index<Axis> for [T; 3] where T: Copy {
189190
);
190191
}
191192

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+
235+
check_assist(
236+
generate_mut_trait_impl,
237+
r#"
238+
//- minicore: index
239+
mod foo {
240+
mod bar {
241+
pub enum Axis { X = 0, Y = 1, Z = 2 }
242+
243+
impl<T> core::ops::Index$0<Axis> for [T; 3] where T: Copy {
244+
type Output = T;
245+
246+
fn index(&self, index: Axis) -> &Self::Output {
247+
let var_name = &self[index as usize];
248+
var_name
249+
}
250+
}
251+
}
252+
}
253+
"#,
254+
r#"
255+
mod foo {
256+
mod bar {
257+
pub enum Axis { X = 0, Y = 1, Z = 2 }
258+
259+
$0impl<T> core::ops::IndexMut<Axis> for [T; 3] where T: Copy {
260+
fn index_mut(&mut self, index: Axis) -> &mut Self::Output {
261+
let var_name = &self[index as usize];
262+
var_name
263+
}
264+
}
265+
266+
impl<T> core::ops::Index<Axis> for [T; 3] where T: Copy {
267+
type Output = T;
268+
269+
fn index(&self, index: Axis) -> &Self::Output {
270+
let var_name = &self[index as usize];
271+
var_name
272+
}
273+
}
274+
}
275+
}
276+
"#,
277+
);
278+
}
279+
192280
#[test]
193281
fn test_generate_mut_trait_impl_not_applicable() {
194282
check_assist_not_applicable(

0 commit comments

Comments
 (0)