Skip to content

Commit b9e5dff

Browse files
committed
Fix some generate ide-assists indentation
1 parent 17aa5f6 commit b9e5dff

8 files changed

+462
-40
lines changed

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

Lines changed: 41 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
use ide_db::{RootDatabase, famous_defs::FamousDefs};
2-
use syntax::ast::{self, AstNode, HasName};
2+
use syntax::ast::{self, AstNode, HasName, edit::AstNodeEdit};
33

4-
use crate::{AssistContext, AssistId, Assists};
4+
use crate::{AssistContext, AssistId, Assists, utils::indent_string};
55

66
// Assist: generate_default_from_enum_variant
77
//
@@ -34,7 +34,8 @@ pub(crate) fn generate_default_from_enum_variant(
3434
) -> Option<()> {
3535
let variant = ctx.find_node_at_offset::<ast::Variant>()?;
3636
let variant_name = variant.name()?;
37-
let enum_name = variant.parent_enum().name()?;
37+
let enum_adt = variant.parent_enum();
38+
let enum_name = enum_adt.name()?;
3839
if !matches!(variant.kind(), ast::StructKind::Unit) {
3940
cov_mark::hit!(test_gen_default_on_non_unit_variant_not_implemented);
4041
return None;
@@ -61,7 +62,7 @@ impl Default for {enum_name} {{
6162
}}
6263
}}"#,
6364
);
64-
edit.insert(start_offset, buf);
65+
edit.insert(start_offset, indent_string(&buf, enum_adt.indent_level()));
6566
},
6667
)
6768
}
@@ -114,6 +115,42 @@ impl Default for Variant {
114115
);
115116
}
116117

118+
#[test]
119+
fn test_generate_default_from_variant_with_indent() {
120+
check_assist(
121+
generate_default_from_enum_variant,
122+
r#"
123+
//- minicore: default
124+
mod foo {
125+
mod bar {
126+
enum Variant {
127+
Undefined,
128+
Minor$0,
129+
Major,
130+
}
131+
}
132+
}
133+
"#,
134+
r#"
135+
mod foo {
136+
mod bar {
137+
enum Variant {
138+
Undefined,
139+
Minor,
140+
Major,
141+
}
142+
143+
impl Default for Variant {
144+
fn default() -> Self {
145+
Self::Minor
146+
}
147+
}
148+
}
149+
}
150+
"#,
151+
);
152+
}
153+
117154
#[test]
118155
fn test_generate_default_already_implemented() {
119156
cov_mark::check!(test_gen_default_impl_already_exists);

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

Lines changed: 65 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2,12 +2,13 @@ use ide_db::famous_defs::FamousDefs;
22
use stdx::format_to;
33
use syntax::{
44
AstNode,
5-
ast::{self, HasGenericParams, HasName, Impl, make},
5+
ast::{self, HasGenericParams, HasName, Impl, edit::AstNodeEdit, make},
66
};
77

88
use crate::{
99
AssistId,
1010
assist_context::{AssistContext, Assists},
11+
utils::indent_string,
1112
};
1213

1314
// Assist: generate_default_from_new
@@ -72,8 +73,10 @@ pub(crate) fn generate_default_from_new(acc: &mut Assists, ctx: &AssistContext<'
7273
let default_code = " fn default() -> Self {
7374
Self::new()
7475
}";
76+
let mut indent_level = fn_node.indent_level();
77+
indent_level.0 = indent_level.0.saturating_sub(1);
7578
let code = generate_trait_impl_text_from_impl(&impl_, self_ty, "Default", default_code);
76-
builder.insert(insert_location.end(), code);
79+
builder.insert(insert_location.end(), indent_string(&code, indent_level));
7780
},
7881
)
7982
}
@@ -119,6 +122,7 @@ fn generate_trait_impl_text_from_impl(
119122

120123
match impl_.where_clause() {
121124
Some(where_clause) => {
125+
let where_clause = where_clause.reset_indent();
122126
format_to!(buf, "\n{where_clause}\n{{\n{code}\n}}");
123127
}
124128
None => {
@@ -417,6 +421,61 @@ where
417421
);
418422
}
419423

424+
#[test]
425+
fn new_function_with_indent() {
426+
check_assist(
427+
generate_default_from_new,
428+
r#"
429+
//- minicore: default
430+
mod foo {
431+
mod bar {
432+
pub struct Foo<T, B> {
433+
_tars: T,
434+
_bar: B,
435+
}
436+
437+
impl<T: From<i32>, B: From<i64>> Foo<T, B>
438+
where
439+
Option<T>: Debug, Option<B>: Debug,
440+
{
441+
pub fn ne$0w() -> Self {
442+
unimplemented!()
443+
}
444+
}
445+
}
446+
}
447+
"#,
448+
r#"
449+
mod foo {
450+
mod bar {
451+
pub struct Foo<T, B> {
452+
_tars: T,
453+
_bar: B,
454+
}
455+
456+
impl<T: From<i32>, B: From<i64>> Foo<T, B>
457+
where
458+
Option<T>: Debug, Option<B>: Debug,
459+
{
460+
pub fn new() -> Self {
461+
unimplemented!()
462+
}
463+
}
464+
465+
impl<T: From<i32>, B: From<i64>> Default for Foo<T, B>
466+
where
467+
Option<T>: Debug, Option<B>: Debug,
468+
{
469+
fn default() -> Self {
470+
Self::new()
471+
}
472+
}
473+
}
474+
}
475+
"#,
476+
);
477+
}
478+
420479
#[test]
421480
fn new_function_with_generics_and_where() {
422481
check_assist(
@@ -629,12 +688,12 @@ mod test {
629688
}
630689
}
631690
632-
impl Default for Example {
633-
fn default() -> Self {
634-
Self::new()
691+
impl Default for Example {
692+
fn default() -> Self {
693+
Self::new()
694+
}
635695
}
636696
}
637-
}
638697
"#,
639698
);
640699
}

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

Lines changed: 35 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,13 +4,13 @@ use hir::{ModPath, ModuleDef};
44
use ide_db::{RootDatabase, famous_defs::FamousDefs};
55
use syntax::{
66
AstNode, Edition, SyntaxNode,
7-
ast::{self, HasName},
7+
ast::{self, HasName, edit::AstNodeEdit},
88
};
99

1010
use crate::{
1111
AssistId,
1212
assist_context::{AssistContext, Assists, SourceChangeBuilder},
13-
utils::generate_trait_impl_text,
13+
utils::{generate_trait_impl_text, indent_string},
1414
};
1515

1616
// Assist: generate_deref
@@ -155,7 +155,7 @@ fn generate_edit(
155155
&trait_path.display(db, edition).to_string(),
156156
&impl_code,
157157
);
158-
edit.insert(start_offset, deref_impl);
158+
edit.insert(start_offset, indent_string(&deref_impl, strukt_adt.indent_level()));
159159
}
160160

161161
fn existing_deref_impl(
@@ -294,6 +294,38 @@ impl core::ops::Deref for B {
294294
);
295295
}
296296

297+
#[test]
298+
fn test_generate_field_deref_with_indent() {
299+
check_assist(
300+
generate_deref,
301+
r#"
302+
//- minicore: deref
303+
mod foo {
304+
mod bar {
305+
struct A { }
306+
struct B(u8, $0A);
307+
}
308+
}
309+
"#,
310+
r#"
311+
mod foo {
312+
mod bar {
313+
struct A { }
314+
struct B(u8, A);
315+
316+
impl core::ops::Deref for B {
317+
type Target = A;
318+
319+
fn deref(&self) -> &Self::Target {
320+
&self.1
321+
}
322+
}
323+
}
324+
}
325+
"#,
326+
);
327+
}
328+
297329
#[test]
298330
fn test_generates_derefmut_when_deref_present() {
299331
check_assist(

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

Lines changed: 96 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -111,6 +111,45 @@ impl Variant {
111111
);
112112
}
113113

114+
#[test]
115+
fn test_generate_enum_is_from_variant_with_indent() {
116+
check_assist(
117+
generate_enum_is_method,
118+
r#"
119+
mod foo {
120+
mod bar {
121+
enum Variant {
122+
Undefined,
123+
Minor$0,
124+
Major,
125+
}
126+
}
127+
}
128+
"#,
129+
r#"
130+
mod foo {
131+
mod bar {
132+
enum Variant {
133+
Undefined,
134+
Minor,
135+
Major,
136+
}
137+
138+
impl Variant {
139+
/// Returns `true` if the variant is [`Minor`].
140+
///
141+
/// [`Minor`]: Variant::Minor
142+
#[must_use]
143+
fn is_minor(&self) -> bool {
144+
matches!(self, Self::Minor)
145+
}
146+
}
147+
}
148+
}
149+
"#,
150+
);
151+
}
152+
114153
#[test]
115154
fn test_generate_enum_is_already_implemented() {
116155
check_assist_not_applicable(
@@ -280,6 +319,63 @@ impl Variant {
280319
);
281320
}
282321

322+
#[test]
323+
fn test_multiple_generate_enum_is_from_variant_with_indent() {
324+
check_assist(
325+
generate_enum_is_method,
326+
r#"
327+
mod foo {
328+
mod bar {
329+
enum Variant {
330+
Undefined,
331+
Minor,
332+
Major$0,
333+
}
334+
335+
impl Variant {
336+
/// Returns `true` if the variant is [`Minor`].
337+
///
338+
/// [`Minor`]: Variant::Minor
339+
#[must_use]
340+
fn is_minor(&self) -> bool {
341+
matches!(self, Self::Minor)
342+
}
343+
}
344+
}
345+
}
346+
"#,
347+
r#"
348+
mod foo {
349+
mod bar {
350+
enum Variant {
351+
Undefined,
352+
Minor,
353+
Major,
354+
}
355+
356+
impl Variant {
357+
/// Returns `true` if the variant is [`Minor`].
358+
///
359+
/// [`Minor`]: Variant::Minor
360+
#[must_use]
361+
fn is_minor(&self) -> bool {
362+
matches!(self, Self::Minor)
363+
}
364+
365+
/// Returns `true` if the variant is [`Major`].
366+
///
367+
/// [`Major`]: Variant::Major
368+
#[must_use]
369+
fn is_major(&self) -> bool {
370+
matches!(self, Self::Major)
371+
}
372+
}
373+
}
374+
}
375+
"#,
376+
);
377+
}
378+
283379
#[test]
284380
fn test_generate_enum_is_variant_names() {
285381
check_assist(

0 commit comments

Comments
 (0)