Skip to content

fix: ide-assists, some generate_\* indentations #19789

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 5 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
use ide_db::{RootDatabase, famous_defs::FamousDefs};
use syntax::ast::{self, AstNode, HasName};
use syntax::ast::{self, AstNode, HasName, edit::AstNodeEdit};

use crate::{AssistContext, AssistId, Assists};
use crate::{AssistContext, AssistId, Assists, utils::indent_string};

// Assist: generate_default_from_enum_variant
//
Expand Down Expand Up @@ -34,7 +34,8 @@ pub(crate) fn generate_default_from_enum_variant(
) -> Option<()> {
let variant = ctx.find_node_at_offset::<ast::Variant>()?;
let variant_name = variant.name()?;
let enum_name = variant.parent_enum().name()?;
let enum_adt = variant.parent_enum();
let enum_name = enum_adt.name()?;
if !matches!(variant.kind(), ast::StructKind::Unit) {
cov_mark::hit!(test_gen_default_on_non_unit_variant_not_implemented);
return None;
Expand All @@ -61,7 +62,7 @@ impl Default for {enum_name} {{
}}
}}"#,
);
edit.insert(start_offset, buf);
edit.insert(start_offset, indent_string(&buf, enum_adt.indent_level()));
},
)
}
Expand Down Expand Up @@ -114,6 +115,42 @@ impl Default for Variant {
);
}

#[test]
fn test_generate_default_from_variant_with_indent() {
check_assist(
generate_default_from_enum_variant,
r#"
//- minicore: default
mod foo {
mod bar {
enum Variant {
Undefined,
Minor$0,
Major,
}
}
}
"#,
r#"
mod foo {
mod bar {
enum Variant {
Undefined,
Minor,
Major,
}

impl Default for Variant {
fn default() -> Self {
Self::Minor
}
}
}
}
"#,
);
}

#[test]
fn test_generate_default_already_implemented() {
cov_mark::check!(test_gen_default_impl_already_exists);
Expand Down
71 changes: 65 additions & 6 deletions crates/ide-assists/src/handlers/generate_default_from_new.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,13 @@ use ide_db::famous_defs::FamousDefs;
use stdx::format_to;
use syntax::{
AstNode,
ast::{self, HasGenericParams, HasName, Impl, make},
ast::{self, HasGenericParams, HasName, Impl, edit::AstNodeEdit, make},
};

use crate::{
AssistId,
assist_context::{AssistContext, Assists},
utils::indent_string,
};

// Assist: generate_default_from_new
Expand Down Expand Up @@ -72,8 +73,10 @@ pub(crate) fn generate_default_from_new(acc: &mut Assists, ctx: &AssistContext<'
let default_code = " fn default() -> Self {
Self::new()
}";
let mut indent_level = fn_node.indent_level();
indent_level.0 = indent_level.0.saturating_sub(1);
let code = generate_trait_impl_text_from_impl(&impl_, self_ty, "Default", default_code);
builder.insert(insert_location.end(), code);
builder.insert(insert_location.end(), indent_string(&code, indent_level));
},
)
}
Expand Down Expand Up @@ -119,6 +122,7 @@ fn generate_trait_impl_text_from_impl(

match impl_.where_clause() {
Some(where_clause) => {
let where_clause = where_clause.reset_indent();
format_to!(buf, "\n{where_clause}\n{{\n{code}\n}}");
}
None => {
Expand Down Expand Up @@ -417,6 +421,61 @@ where
);
}

#[test]
fn new_function_with_indent() {
check_assist(
generate_default_from_new,
r#"
//- minicore: default
mod foo {
mod bar {
pub struct Foo<T, B> {
_tars: T,
_bar: B,
}

impl<T: From<i32>, B: From<i64>> Foo<T, B>
where
Option<T>: Debug, Option<B>: Debug,
{
pub fn ne$0w() -> Self {
unimplemented!()
}
}
}
}
"#,
r#"
mod foo {
mod bar {
pub struct Foo<T, B> {
_tars: T,
_bar: B,
}

impl<T: From<i32>, B: From<i64>> Foo<T, B>
where
Option<T>: Debug, Option<B>: Debug,
{
pub fn new() -> Self {
unimplemented!()
}
}

impl<T: From<i32>, B: From<i64>> Default for Foo<T, B>
where
Option<T>: Debug, Option<B>: Debug,
{
fn default() -> Self {
Self::new()
}
}
}
}
"#,
);
}

#[test]
fn new_function_with_generics_and_where() {
check_assist(
Expand Down Expand Up @@ -629,12 +688,12 @@ mod test {
}
}

impl Default for Example {
fn default() -> Self {
Self::new()
impl Default for Example {
fn default() -> Self {
Self::new()
}
}
}
}
"#,
);
}
Expand Down
38 changes: 35 additions & 3 deletions crates/ide-assists/src/handlers/generate_deref.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,13 @@ use hir::{ModPath, ModuleDef};
use ide_db::{RootDatabase, famous_defs::FamousDefs};
use syntax::{
AstNode, Edition, SyntaxNode,
ast::{self, HasName},
ast::{self, HasName, edit::AstNodeEdit},
};

use crate::{
AssistId,
assist_context::{AssistContext, Assists, SourceChangeBuilder},
utils::generate_trait_impl_text,
utils::{generate_trait_impl_text, indent_string},
};

// Assist: generate_deref
Expand Down Expand Up @@ -155,7 +155,7 @@ fn generate_edit(
&trait_path.display(db, edition).to_string(),
&impl_code,
);
edit.insert(start_offset, deref_impl);
edit.insert(start_offset, indent_string(&deref_impl, strukt_adt.indent_level()));
}

fn existing_deref_impl(
Expand Down Expand Up @@ -294,6 +294,38 @@ impl core::ops::Deref for B {
);
}

#[test]
fn test_generate_field_deref_with_indent() {
check_assist(
generate_deref,
r#"
//- minicore: deref
mod foo {
mod bar {
struct A { }
struct B(u8, $0A);
}
}
"#,
r#"
mod foo {
mod bar {
struct A { }
struct B(u8, A);

impl core::ops::Deref for B {
type Target = A;

fn deref(&self) -> &Self::Target {
&self.1
}
}
}
}
"#,
);
}

#[test]
fn test_generates_derefmut_when_deref_present() {
check_assist(
Expand Down
96 changes: 96 additions & 0 deletions crates/ide-assists/src/handlers/generate_enum_is_method.rs
Original file line number Diff line number Diff line change
Expand Up @@ -111,6 +111,45 @@ impl Variant {
);
}

#[test]
fn test_generate_enum_is_from_variant_with_indent() {
check_assist(
generate_enum_is_method,
r#"
mod foo {
mod bar {
enum Variant {
Undefined,
Minor$0,
Major,
}
}
}
"#,
r#"
mod foo {
mod bar {
enum Variant {
Undefined,
Minor,
Major,
}

impl Variant {
/// Returns `true` if the variant is [`Minor`].
///
/// [`Minor`]: Variant::Minor
#[must_use]
fn is_minor(&self) -> bool {
matches!(self, Self::Minor)
}
}
}
}
"#,
);
}

#[test]
fn test_generate_enum_is_already_implemented() {
check_assist_not_applicable(
Expand Down Expand Up @@ -280,6 +319,63 @@ impl Variant {
);
}

#[test]
fn test_multiple_generate_enum_is_from_variant_with_indent() {
check_assist(
generate_enum_is_method,
r#"
mod foo {
mod bar {
enum Variant {
Undefined,
Minor,
Major$0,
}

impl Variant {
/// Returns `true` if the variant is [`Minor`].
///
/// [`Minor`]: Variant::Minor
#[must_use]
fn is_minor(&self) -> bool {
matches!(self, Self::Minor)
}
}
}
}
"#,
r#"
mod foo {
mod bar {
enum Variant {
Undefined,
Minor,
Major,
}

impl Variant {
/// Returns `true` if the variant is [`Minor`].
///
/// [`Minor`]: Variant::Minor
#[must_use]
fn is_minor(&self) -> bool {
matches!(self, Self::Minor)
}

/// Returns `true` if the variant is [`Major`].
///
/// [`Major`]: Variant::Major
#[must_use]
fn is_major(&self) -> bool {
matches!(self, Self::Major)
}
}
}
}
"#,
);
}

#[test]
fn test_generate_enum_is_variant_names() {
check_assist(
Expand Down
Loading