zls icon indicating copy to clipboard operation
zls copied to clipboard

add code action that simplifies variable initialization

Open Techatrix opened this issue 1 year ago • 3 comments

Before:

const foo = @as(T, something);
const bar = T{};

After:

const foo: T = something;
const bar: T = .{};

This feature has been suggested by @mlugg a while ago but I never found the time to implement it.

Thanks to https://github.com/zigtools/zls/pull/2063 it becomes trivial to optionally enable certain code actions to be run on save.

Related https://github.com/ziglang/zig/issues/5038

Techatrix avatar Nov 30 '24 19:11 Techatrix

Hm. This would be nice to have. If you haven't taken a look by next weekend, I can take a stab at it.

srijan-paul avatar Nov 30 '24 19:11 srijan-paul

@mlugg Since you know so much about RLS, could you tell me whether the following conversion is also valid?

const foo = T{}; // Before
const foo: T = .{}; // After

Techatrix avatar Nov 30 '24 19:11 Techatrix

Yep, T{ ...} is identical to @as(T, .{ ... }), so that's a valid conversion.

mlugg avatar Nov 30 '24 21:11 mlugg