rust-analyzer icon indicating copy to clipboard operation
rust-analyzer copied to clipboard

Code action to convert between struct types

Open oriongonza opened this issue 10 months ago • 6 comments

struct S {} // Convert into unit struct
struct S; // Convert into normal struct

struct S { field: String }; // Convert into tuple struct
struct S(String); // Convert into normal struct

struct S(String, u64); // Convert into normal struct
struct S { 
  field_1: String, // These can be renamed easily later on
  field_2: u64
}

I think that this would aid in refactoring in a big way, especially for tuple -> named structs.

It's a pain to fix them manually.

oriongonza avatar Apr 23 '25 08:04 oriongonza

We already have named <-> tuple struct conversions

Veykril avatar Apr 23 '25 09:04 Veykril

We already have this assist. Note that you need to place the cursor on the name of the struct to activate it. It would be better to expand the trigger area to allow more people to discover this feature.

See

https://github.com/rust-lang/rust-analyzer/blob/6fce68edb5078ef717256ec1df346050939a7bd0/crates/ide-assists/src/handlers/convert_named_struct_to_tuple_struct.rs#L52

and

https://github.com/rust-lang/rust-analyzer/blob/6fce68edb5078ef717256ec1df346050939a7bd0/crates/ide-assists/src/handlers/convert_tuple_struct_to_named_struct.rs#L50

roife avatar Apr 24 '25 08:04 roife

I'm not sure we want to expand the area. I agree we have a discoverability problem with assists, but expanding its range will mean it more often gets in the way of people that want to invoke something else.

ChayimFriedman2 avatar May 05 '25 22:05 ChayimFriedman2

For me, it would be very intuitive to extend the range to the entire definition, or at least we could extend the range to struct kw, { and (?

roife avatar May 07 '25 09:05 roife

I'd expect this assist to trigger from the struct/visibility token up to the end of the name. The entire definition is way too annoying. Stuff that affects the entire definition of something imo should be triggerable only on the "signature" of the thing.

Veykril avatar May 07 '25 10:05 Veykril

@rustbot claim

asukaminato0721 avatar Nov 28 '25 08:11 asukaminato0721