From e3d26cdbb6b02aa2524044d7e2db202a940ce891 Mon Sep 17 00:00:00 2001 From: BazookaMusic Date: Sun, 1 Jun 2025 00:44:33 +0200 Subject: [PATCH 1/6] Allow assist edit for converting structs to appear also on struct keyword and on visibility --- .../convert_named_struct_to_tuple_struct.rs | 88 ++++++++++++++++++- .../convert_tuple_struct_to_named_struct.rs | 87 +++++++++++++++++- .../crates/ide-assists/src/utils.rs | 23 +++++ 3 files changed, 192 insertions(+), 6 deletions(-) diff --git a/src/tools/rust-analyzer/crates/ide-assists/src/handlers/convert_named_struct_to_tuple_struct.rs b/src/tools/rust-analyzer/crates/ide-assists/src/handlers/convert_named_struct_to_tuple_struct.rs index 5d75e445861e..fa9fab7d78b6 100644 --- a/src/tools/rust-analyzer/crates/ide-assists/src/handlers/convert_named_struct_to_tuple_struct.rs +++ b/src/tools/rust-analyzer/crates/ide-assists/src/handlers/convert_named_struct_to_tuple_struct.rs @@ -7,7 +7,7 @@ use syntax::{ match_ast, ted, }; -use crate::{AssistContext, AssistId, Assists, assist_context::SourceChangeBuilder}; +use crate::{AssistContext, AssistId, Assists, assist_context::SourceChangeBuilder, utils::find_struct_definition_from_cursor}; // Assist: convert_named_struct_to_tuple_struct // @@ -56,8 +56,7 @@ pub(crate) fn convert_named_struct_to_tuple_struct( // XXX: We don't currently provide this assist for struct definitions inside macros, but if we // are to lift this limitation, don't forget to make `edit_struct_def()` consider macro files // too. - let name = ctx.find_node_at_offset::()?; - let strukt = name.syntax().parent().and_then(>::cast)?; + let strukt = find_struct_definition_from_cursor(ctx)?; let field_list = strukt.as_ref().either(|s| s.field_list(), |v| v.field_list())?; let record_fields = match field_list { ast::FieldList::RecordFieldList(it) => it, @@ -293,6 +292,89 @@ impl A { ); } + #[test] + fn convert_simple_struct_cursor_on_struct_keyword() { + check_assist( + convert_named_struct_to_tuple_struct, + r#" +struct Inner; +struct$0 A { inner: Inner } + +impl A { + fn new(inner: Inner) -> A { + A { inner } + } + + fn new_with_default() -> A { + A::new(Inner) + } + + fn into_inner(self) -> Inner { + self.inner + } +}"#, + r#" +struct Inner; +struct A(Inner); + +impl A { + fn new(inner: Inner) -> A { + A(inner) + } + + fn new_with_default() -> A { + A::new(Inner) + } + + fn into_inner(self) -> Inner { + self.0 + } +}"#, + ); + } + + #[test] + fn convert_simple_struct_cursor_on_visibility_keyword() { + check_assist( + convert_named_struct_to_tuple_struct, + r#" +struct Inner; +pub$0 struct A { inner: Inner } + +impl A { + fn new(inner: Inner) -> A { + A { inner } + } + + fn new_with_default() -> A { + A::new(Inner) + } + + fn into_inner(self) -> Inner { + self.inner + } +}"#, + r#" +struct Inner; +struct A(Inner); + +impl A { + fn new(inner: Inner) -> A { + A(inner) + } + + fn new_with_default() -> A { + A::new(Inner) + } + + fn into_inner(self) -> Inner { + self.0 + } +}"#, + ); + } + + #[test] fn convert_struct_referenced_via_self_kw() { check_assist( diff --git a/src/tools/rust-analyzer/crates/ide-assists/src/handlers/convert_tuple_struct_to_named_struct.rs b/src/tools/rust-analyzer/crates/ide-assists/src/handlers/convert_tuple_struct_to_named_struct.rs index 0c0b93bcfbc1..5c2a3cdc3051 100644 --- a/src/tools/rust-analyzer/crates/ide-assists/src/handlers/convert_tuple_struct_to_named_struct.rs +++ b/src/tools/rust-analyzer/crates/ide-assists/src/handlers/convert_tuple_struct_to_named_struct.rs @@ -6,7 +6,7 @@ use syntax::{ match_ast, ted, }; -use crate::{AssistContext, AssistId, Assists, assist_context::SourceChangeBuilder}; +use crate::{AssistContext, AssistId, Assists, assist_context::SourceChangeBuilder, utils::find_struct_definition_from_cursor}; // Assist: convert_tuple_struct_to_named_struct // @@ -51,8 +51,7 @@ pub(crate) fn convert_tuple_struct_to_named_struct( acc: &mut Assists, ctx: &AssistContext<'_>, ) -> Option<()> { - let name = ctx.find_node_at_offset::()?; - let strukt = name.syntax().parent().and_then(>::cast)?; + let strukt = find_struct_definition_from_cursor(ctx)?; let field_list = strukt.as_ref().either(|s| s.field_list(), |v| v.field_list())?; let tuple_fields = match field_list { ast::FieldList::TupleFieldList(it) => it, @@ -300,6 +299,88 @@ impl A { struct Inner; struct A { field1: Inner } +impl A { + fn new(inner: Inner) -> A { + A { field1: inner } + } + + fn new_with_default() -> A { + A::new(Inner) + } + + fn into_inner(self) -> Inner { + self.field1 + } +}"#, + ); + } + + #[test] + fn convert_simple_struct_cursor_on_struct_keyword() { + check_assist( + convert_tuple_struct_to_named_struct, + r#" +struct Inner; +struct$0 A(Inner); + +impl A { + fn new(inner: Inner) -> A { + A(inner) + } + + fn new_with_default() -> A { + A::new(Inner) + } + + fn into_inner(self) -> Inner { + self.0 + } +}"#, + r#" +struct Inner; +struct A { field1: Inner } + +impl A { + fn new(inner: Inner) -> A { + A { field1: inner } + } + + fn new_with_default() -> A { + A::new(Inner) + } + + fn into_inner(self) -> Inner { + self.field1 + } +}"#, + ); + } + + #[test] + fn convert_simple_struct_cursor_on_visibility_keyword() { + check_assist( + convert_tuple_struct_to_named_struct, + r#" +struct Inner; +pub$0 struct A(Inner); + +impl A { + fn new(inner: Inner) -> A { + A(inner) + } + + fn new_with_default() -> A { + A::new(Inner) + } + + fn into_inner(self) -> Inner { + self.0 + } +}"#, + r#" +struct Inner; +pub struct A { field1: Inner } + impl A { fn new(inner: Inner) -> A { A { field1: inner } diff --git a/src/tools/rust-analyzer/crates/ide-assists/src/utils.rs b/src/tools/rust-analyzer/crates/ide-assists/src/utils.rs index ef6914fda1d5..1c0e022d01e0 100644 --- a/src/tools/rust-analyzer/crates/ide-assists/src/utils.rs +++ b/src/tools/rust-analyzer/crates/ide-assists/src/utils.rs @@ -1,5 +1,7 @@ //! Assorted functions shared by several assists. +use either::Either; + pub(crate) use gen_trait_fn_body::gen_trait_fn_body; use hir::{ DisplayTarget, HasAttrs as HirHasAttrs, HirDisplay, InFile, ModuleDef, PathResolution, @@ -1146,3 +1148,24 @@ pub fn is_body_const(sema: &Semantics<'_, RootDatabase>, expr: &ast::Expr) -> bo }); is_const } + +/// Gets the struct definition from a context +pub(crate) fn find_struct_definition_from_cursor(ctx: &AssistContext<'_>) +-> Option> +{ + ctx.find_node_at_offset::().and_then(|name| name.syntax().parent()) + .or(find_struct_keyword(ctx).and_then(|kw| kw.parent())) + .or(ctx.find_node_at_offset::().and_then(|visibility| visibility.syntax().parent())) + .and_then(>::cast) +} + +fn find_struct_keyword(ctx: &AssistContext<'_>) -> Option { + // Attempt to find the token at the current cursor offset + ctx + .token_at_offset() + .find(|leaf| match leaf.kind() { + STRUCT_KW => true, + _ => false, + }) +} + From a241d8ebd1402116272503f2aaf9610fddc48441 Mon Sep 17 00:00:00 2001 From: BazookaMusic Date: Sun, 1 Jun 2025 00:47:43 +0200 Subject: [PATCH 2/6] add missing public keyword --- .../src/handlers/convert_named_struct_to_tuple_struct.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/tools/rust-analyzer/crates/ide-assists/src/handlers/convert_named_struct_to_tuple_struct.rs b/src/tools/rust-analyzer/crates/ide-assists/src/handlers/convert_named_struct_to_tuple_struct.rs index fa9fab7d78b6..1d6450a9754c 100644 --- a/src/tools/rust-analyzer/crates/ide-assists/src/handlers/convert_named_struct_to_tuple_struct.rs +++ b/src/tools/rust-analyzer/crates/ide-assists/src/handlers/convert_named_struct_to_tuple_struct.rs @@ -356,7 +356,7 @@ impl A { }"#, r#" struct Inner; -struct A(Inner); +pub struct A(Inner); impl A { fn new(inner: Inner) -> A { From 01837bc0c489d56bc0c22e46edea08937e3128f7 Mon Sep 17 00:00:00 2001 From: BazookaMusic Date: Sun, 1 Jun 2025 01:09:23 +0200 Subject: [PATCH 3/6] Formatting issues resolved --- .../convert_named_struct_to_tuple_struct.rs | 6 ++++-- .../convert_tuple_struct_to_named_struct.rs | 7 +++++-- .../crates/ide-assists/src/utils.rs | 18 +++++++++--------- 3 files changed, 18 insertions(+), 13 deletions(-) diff --git a/src/tools/rust-analyzer/crates/ide-assists/src/handlers/convert_named_struct_to_tuple_struct.rs b/src/tools/rust-analyzer/crates/ide-assists/src/handlers/convert_named_struct_to_tuple_struct.rs index 1d6450a9754c..0a23bd6d7177 100644 --- a/src/tools/rust-analyzer/crates/ide-assists/src/handlers/convert_named_struct_to_tuple_struct.rs +++ b/src/tools/rust-analyzer/crates/ide-assists/src/handlers/convert_named_struct_to_tuple_struct.rs @@ -7,7 +7,10 @@ use syntax::{ match_ast, ted, }; -use crate::{AssistContext, AssistId, Assists, assist_context::SourceChangeBuilder, utils::find_struct_definition_from_cursor}; +use crate::{ + AssistContext, AssistId, Assists, assist_context::SourceChangeBuilder, + utils::find_struct_definition_from_cursor, +}; // Assist: convert_named_struct_to_tuple_struct // @@ -374,7 +377,6 @@ impl A { ); } - #[test] fn convert_struct_referenced_via_self_kw() { check_assist( diff --git a/src/tools/rust-analyzer/crates/ide-assists/src/handlers/convert_tuple_struct_to_named_struct.rs b/src/tools/rust-analyzer/crates/ide-assists/src/handlers/convert_tuple_struct_to_named_struct.rs index 5c2a3cdc3051..464309dadb45 100644 --- a/src/tools/rust-analyzer/crates/ide-assists/src/handlers/convert_tuple_struct_to_named_struct.rs +++ b/src/tools/rust-analyzer/crates/ide-assists/src/handlers/convert_tuple_struct_to_named_struct.rs @@ -6,7 +6,10 @@ use syntax::{ match_ast, ted, }; -use crate::{AssistContext, AssistId, Assists, assist_context::SourceChangeBuilder, utils::find_struct_definition_from_cursor}; +use crate::{ + AssistContext, AssistId, Assists, assist_context::SourceChangeBuilder, + utils::find_struct_definition_from_cursor, +}; // Assist: convert_tuple_struct_to_named_struct // @@ -356,7 +359,7 @@ impl A { ); } - #[test] + #[test] fn convert_simple_struct_cursor_on_visibility_keyword() { check_assist( convert_tuple_struct_to_named_struct, diff --git a/src/tools/rust-analyzer/crates/ide-assists/src/utils.rs b/src/tools/rust-analyzer/crates/ide-assists/src/utils.rs index 1c0e022d01e0..5741b15fdce9 100644 --- a/src/tools/rust-analyzer/crates/ide-assists/src/utils.rs +++ b/src/tools/rust-analyzer/crates/ide-assists/src/utils.rs @@ -1150,22 +1150,22 @@ pub fn is_body_const(sema: &Semantics<'_, RootDatabase>, expr: &ast::Expr) -> bo } /// Gets the struct definition from a context -pub(crate) fn find_struct_definition_from_cursor(ctx: &AssistContext<'_>) --> Option> -{ - ctx.find_node_at_offset::().and_then(|name| name.syntax().parent()) +pub(crate) fn find_struct_definition_from_cursor( + ctx: &AssistContext<'_>, +) -> Option> { + ctx.find_node_at_offset::() + .and_then(|name| name.syntax().parent()) .or(find_struct_keyword(ctx).and_then(|kw| kw.parent())) - .or(ctx.find_node_at_offset::().and_then(|visibility| visibility.syntax().parent())) + .or(ctx + .find_node_at_offset::() + .and_then(|visibility| visibility.syntax().parent())) .and_then(>::cast) } fn find_struct_keyword(ctx: &AssistContext<'_>) -> Option { // Attempt to find the token at the current cursor offset - ctx - .token_at_offset() - .find(|leaf| match leaf.kind() { + ctx.token_at_offset().find(|leaf| match leaf.kind() { STRUCT_KW => true, _ => false, }) } - From b34e36bc8b0ba4c27e4d9a7e0b5677a2b2294699 Mon Sep 17 00:00:00 2001 From: BazookaMusic Date: Sun, 1 Jun 2025 01:16:37 +0200 Subject: [PATCH 4/6] rename function so it makes more sense --- .../src/handlers/convert_named_struct_to_tuple_struct.rs | 4 ++-- .../src/handlers/convert_tuple_struct_to_named_struct.rs | 4 ++-- src/tools/rust-analyzer/crates/ide-assists/src/utils.rs | 2 +- 3 files changed, 5 insertions(+), 5 deletions(-) diff --git a/src/tools/rust-analyzer/crates/ide-assists/src/handlers/convert_named_struct_to_tuple_struct.rs b/src/tools/rust-analyzer/crates/ide-assists/src/handlers/convert_named_struct_to_tuple_struct.rs index 0a23bd6d7177..6435cb25f287 100644 --- a/src/tools/rust-analyzer/crates/ide-assists/src/handlers/convert_named_struct_to_tuple_struct.rs +++ b/src/tools/rust-analyzer/crates/ide-assists/src/handlers/convert_named_struct_to_tuple_struct.rs @@ -9,7 +9,7 @@ use syntax::{ use crate::{ AssistContext, AssistId, Assists, assist_context::SourceChangeBuilder, - utils::find_struct_definition_from_cursor, + utils::get_struct_definition_from_context, }; // Assist: convert_named_struct_to_tuple_struct @@ -59,7 +59,7 @@ pub(crate) fn convert_named_struct_to_tuple_struct( // XXX: We don't currently provide this assist for struct definitions inside macros, but if we // are to lift this limitation, don't forget to make `edit_struct_def()` consider macro files // too. - let strukt = find_struct_definition_from_cursor(ctx)?; + let strukt = get_struct_definition_from_context(ctx)?; let field_list = strukt.as_ref().either(|s| s.field_list(), |v| v.field_list())?; let record_fields = match field_list { ast::FieldList::RecordFieldList(it) => it, diff --git a/src/tools/rust-analyzer/crates/ide-assists/src/handlers/convert_tuple_struct_to_named_struct.rs b/src/tools/rust-analyzer/crates/ide-assists/src/handlers/convert_tuple_struct_to_named_struct.rs index 464309dadb45..2f7b1ec8db74 100644 --- a/src/tools/rust-analyzer/crates/ide-assists/src/handlers/convert_tuple_struct_to_named_struct.rs +++ b/src/tools/rust-analyzer/crates/ide-assists/src/handlers/convert_tuple_struct_to_named_struct.rs @@ -8,7 +8,7 @@ use syntax::{ use crate::{ AssistContext, AssistId, Assists, assist_context::SourceChangeBuilder, - utils::find_struct_definition_from_cursor, + utils::get_struct_definition_from_context, }; // Assist: convert_tuple_struct_to_named_struct @@ -54,7 +54,7 @@ pub(crate) fn convert_tuple_struct_to_named_struct( acc: &mut Assists, ctx: &AssistContext<'_>, ) -> Option<()> { - let strukt = find_struct_definition_from_cursor(ctx)?; + let strukt = get_struct_definition_from_context(ctx)?; let field_list = strukt.as_ref().either(|s| s.field_list(), |v| v.field_list())?; let tuple_fields = match field_list { ast::FieldList::TupleFieldList(it) => it, diff --git a/src/tools/rust-analyzer/crates/ide-assists/src/utils.rs b/src/tools/rust-analyzer/crates/ide-assists/src/utils.rs index 5741b15fdce9..676937c8168d 100644 --- a/src/tools/rust-analyzer/crates/ide-assists/src/utils.rs +++ b/src/tools/rust-analyzer/crates/ide-assists/src/utils.rs @@ -1150,7 +1150,7 @@ pub fn is_body_const(sema: &Semantics<'_, RootDatabase>, expr: &ast::Expr) -> bo } /// Gets the struct definition from a context -pub(crate) fn find_struct_definition_from_cursor( +pub(crate) fn get_struct_definition_from_context( ctx: &AssistContext<'_>, ) -> Option> { ctx.find_node_at_offset::() From 4f018d84a994e05ad8dd6f2a9445bba2126ca059 Mon Sep 17 00:00:00 2001 From: BazookaMusic Date: Sun, 1 Jun 2025 01:20:45 +0200 Subject: [PATCH 5/6] clippy and more formatting --- .../rust-analyzer/crates/ide-assists/src/utils.rs | 13 ++++--------- 1 file changed, 4 insertions(+), 9 deletions(-) diff --git a/src/tools/rust-analyzer/crates/ide-assists/src/utils.rs b/src/tools/rust-analyzer/crates/ide-assists/src/utils.rs index 676937c8168d..c7a6c2d5326b 100644 --- a/src/tools/rust-analyzer/crates/ide-assists/src/utils.rs +++ b/src/tools/rust-analyzer/crates/ide-assists/src/utils.rs @@ -1155,17 +1155,12 @@ pub(crate) fn get_struct_definition_from_context( ) -> Option> { ctx.find_node_at_offset::() .and_then(|name| name.syntax().parent()) - .or(find_struct_keyword(ctx).and_then(|kw| kw.parent())) + .or(ctx + .token_at_offset() + .find(|leaf| matches!(leaf.kind(), STRUCT_KW)) + .and_then(|kw| kw.parent())) .or(ctx .find_node_at_offset::() .and_then(|visibility| visibility.syntax().parent())) .and_then(>::cast) } - -fn find_struct_keyword(ctx: &AssistContext<'_>) -> Option { - // Attempt to find the token at the current cursor offset - ctx.token_at_offset().find(|leaf| match leaf.kind() { - STRUCT_KW => true, - _ => false, - }) -} From d122bd43da8aca656c8024ed62920207d368ba45 Mon Sep 17 00:00:00 2001 From: BazookaMusic Date: Mon, 2 Jun 2025 23:28:26 +0200 Subject: [PATCH 6/6] simplify expression that checks the offset --- .../convert_named_struct_to_tuple_struct.rs | 24 ++++++++++++------- .../convert_tuple_struct_to_named_struct.rs | 24 ++++++++++++------- .../crates/ide-assists/src/utils.rs | 18 -------------- 3 files changed, 30 insertions(+), 36 deletions(-) diff --git a/src/tools/rust-analyzer/crates/ide-assists/src/handlers/convert_named_struct_to_tuple_struct.rs b/src/tools/rust-analyzer/crates/ide-assists/src/handlers/convert_named_struct_to_tuple_struct.rs index 6435cb25f287..32c4ae2e869e 100644 --- a/src/tools/rust-analyzer/crates/ide-assists/src/handlers/convert_named_struct_to_tuple_struct.rs +++ b/src/tools/rust-analyzer/crates/ide-assists/src/handlers/convert_named_struct_to_tuple_struct.rs @@ -7,10 +7,7 @@ use syntax::{ match_ast, ted, }; -use crate::{ - AssistContext, AssistId, Assists, assist_context::SourceChangeBuilder, - utils::get_struct_definition_from_context, -}; +use crate::{AssistContext, AssistId, Assists, assist_context::SourceChangeBuilder}; // Assist: convert_named_struct_to_tuple_struct // @@ -59,13 +56,22 @@ pub(crate) fn convert_named_struct_to_tuple_struct( // XXX: We don't currently provide this assist for struct definitions inside macros, but if we // are to lift this limitation, don't forget to make `edit_struct_def()` consider macro files // too. - let strukt = get_struct_definition_from_context(ctx)?; - let field_list = strukt.as_ref().either(|s| s.field_list(), |v| v.field_list())?; + let strukt_or_variant = ctx + .find_node_at_offset::() + .map(Either::Left) + .or_else(|| ctx.find_node_at_offset::().map(Either::Right))?; + let field_list = strukt_or_variant.as_ref().either(|s| s.field_list(), |v| v.field_list())?; + + if ctx.offset() > field_list.syntax().text_range().start() { + // Assist could be distracting after the braces + return None; + } + let record_fields = match field_list { ast::FieldList::RecordFieldList(it) => it, ast::FieldList::TupleFieldList(_) => return None, }; - let strukt_def = match &strukt { + let strukt_def = match &strukt_or_variant { Either::Left(s) => Either::Left(ctx.sema.to_def(s)?), Either::Right(v) => Either::Right(ctx.sema.to_def(v)?), }; @@ -73,11 +79,11 @@ pub(crate) fn convert_named_struct_to_tuple_struct( acc.add( AssistId::refactor_rewrite("convert_named_struct_to_tuple_struct"), "Convert to tuple struct", - strukt.syntax().text_range(), + strukt_or_variant.syntax().text_range(), |edit| { edit_field_references(ctx, edit, record_fields.fields()); edit_struct_references(ctx, edit, strukt_def); - edit_struct_def(ctx, edit, &strukt, record_fields); + edit_struct_def(ctx, edit, &strukt_or_variant, record_fields); }, ) } diff --git a/src/tools/rust-analyzer/crates/ide-assists/src/handlers/convert_tuple_struct_to_named_struct.rs b/src/tools/rust-analyzer/crates/ide-assists/src/handlers/convert_tuple_struct_to_named_struct.rs index 2f7b1ec8db74..80756197fb70 100644 --- a/src/tools/rust-analyzer/crates/ide-assists/src/handlers/convert_tuple_struct_to_named_struct.rs +++ b/src/tools/rust-analyzer/crates/ide-assists/src/handlers/convert_tuple_struct_to_named_struct.rs @@ -6,10 +6,7 @@ use syntax::{ match_ast, ted, }; -use crate::{ - AssistContext, AssistId, Assists, assist_context::SourceChangeBuilder, - utils::get_struct_definition_from_context, -}; +use crate::{AssistContext, AssistId, Assists, assist_context::SourceChangeBuilder}; // Assist: convert_tuple_struct_to_named_struct // @@ -54,17 +51,26 @@ pub(crate) fn convert_tuple_struct_to_named_struct( acc: &mut Assists, ctx: &AssistContext<'_>, ) -> Option<()> { - let strukt = get_struct_definition_from_context(ctx)?; - let field_list = strukt.as_ref().either(|s| s.field_list(), |v| v.field_list())?; + let strukt_or_variant = ctx + .find_node_at_offset::() + .map(Either::Left) + .or_else(|| ctx.find_node_at_offset::().map(Either::Right))?; + let field_list = strukt_or_variant.as_ref().either(|s| s.field_list(), |v| v.field_list())?; + + if ctx.offset() > field_list.syntax().text_range().start() { + // Assist could be distracting after the braces + return None; + } + let tuple_fields = match field_list { ast::FieldList::TupleFieldList(it) => it, ast::FieldList::RecordFieldList(_) => return None, }; - let strukt_def = match &strukt { + let strukt_def = match &strukt_or_variant { Either::Left(s) => Either::Left(ctx.sema.to_def(s)?), Either::Right(v) => Either::Right(ctx.sema.to_def(v)?), }; - let target = strukt.as_ref().either(|s| s.syntax(), |v| v.syntax()).text_range(); + let target = strukt_or_variant.as_ref().either(|s| s.syntax(), |v| v.syntax()).text_range(); acc.add( AssistId::refactor_rewrite("convert_tuple_struct_to_named_struct"), @@ -74,7 +80,7 @@ pub(crate) fn convert_tuple_struct_to_named_struct( let names = generate_names(tuple_fields.fields()); edit_field_references(ctx, edit, tuple_fields.fields(), &names); edit_struct_references(ctx, edit, strukt_def, &names); - edit_struct_def(ctx, edit, &strukt, tuple_fields, names); + edit_struct_def(ctx, edit, &strukt_or_variant, tuple_fields, names); }, ) } diff --git a/src/tools/rust-analyzer/crates/ide-assists/src/utils.rs b/src/tools/rust-analyzer/crates/ide-assists/src/utils.rs index c7a6c2d5326b..ef6914fda1d5 100644 --- a/src/tools/rust-analyzer/crates/ide-assists/src/utils.rs +++ b/src/tools/rust-analyzer/crates/ide-assists/src/utils.rs @@ -1,7 +1,5 @@ //! Assorted functions shared by several assists. -use either::Either; - pub(crate) use gen_trait_fn_body::gen_trait_fn_body; use hir::{ DisplayTarget, HasAttrs as HirHasAttrs, HirDisplay, InFile, ModuleDef, PathResolution, @@ -1148,19 +1146,3 @@ pub fn is_body_const(sema: &Semantics<'_, RootDatabase>, expr: &ast::Expr) -> bo }); is_const } - -/// Gets the struct definition from a context -pub(crate) fn get_struct_definition_from_context( - ctx: &AssistContext<'_>, -) -> Option> { - ctx.find_node_at_offset::() - .and_then(|name| name.syntax().parent()) - .or(ctx - .token_at_offset() - .find(|leaf| matches!(leaf.kind(), STRUCT_KW)) - .and_then(|kw| kw.parent())) - .or(ctx - .find_node_at_offset::() - .and_then(|visibility| visibility.syntax().parent())) - .and_then(>::cast) -}