Repository navigation
Conversation
Offer a refactor that rewrites `const foo = @as(T, value);` into `const foo: T = value;` and `const foo = T{ ... };` into `const foo: T = .{ ... };`. Array types are skipped because `[_]T` is not a valid type annotation.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Description
Adds a refactor code action, "simplify variable initialization", for the rewrite proposed in #2098:
It is offered (kind
refactor) for the innermost variable declaration that contains the requested position, when:@as(T, value)or a typed struct initializationT{ ... }.The type annotation is inserted right after the name, so declarations with
align(...)/linksection(...)keep those after the type (pub const foo: u32 align(4) = 5;). Array types are skipped (conservatively, including[N]T{}), since[_]u8{}is parsed as a struct init but[_]u8is not a valid type annotation. If the text that would be removed contains a comment (e.g.@as(u32, // noteorS // note\n{}), no action is offered rather than silently dropping it. Other builtins, anonymous literals, already-annotated declarations and expressions where@asis only part of the initializer get no action.The action is generated from
generateCodeActionsInRangebefore the string-literal context check, next to the existing string-literal refactors. I kept it as a cursor-basedrefactorrather than a whole-filesource.*action; happy to change the kind or title if you'd prefer something else.Tests: new cases in
tests/lsp_features/code_actions.zigcover@as(local/global, complex types,align), struct inits (empty, with fields, genericGeneric(u8){ ... }spanning multiple lines), picking the innermost declaration, and the "no action" cases above (including the comment cases). Without the change insrc/features/code_actions.zigthe three positive tests fail; the[_]u8{}case also fails if the array-type guard is removed.Motivation / disclosure: I had some spare AI-assistant quota (Claude Code) and am using it to try to help projects with open good-first-issues. The change was prepared with Claude Code and verified as listed below. If it doesn't fit, isn't up to your bar, or you'd simply rather not take it, please feel free to close it, no hard feelings at all 🙂
Related issue
Closes #2098
Checklist
zig build test→ 586 passed, 11 skipped, 0 failed;zig build test -Dtest-filter="simplify variable"→ 4 passed)zig fmt --check src tests build.zigpassesCHANGELOG.mdis updated (if applicable) — n/a, the repo has no changelog