Add 'Add Explicit Raw Values' code action#2668
Open
ayush-that wants to merge 1 commit into
Open
Conversation
Contributor
There was a problem hiding this comment.
Pull request overview
Note
Copilot was unable to run its full agentic suite in this review.
This PR introduces a new SwiftSyntax-based refactoring code action to add explicit raw values to enum cases, along with targeted test coverage and build/registration wiring.
Changes:
- Add
AddExplicitEnumRawValuescode action provider and register it with the code action registry. - Add a suite of LSP tests covering Int/String raw value insertion and “not offered” scenarios.
- Introduce a test helper to assert that a code action is not offered at a marker.
Reviewed changes
Copilot reviewed 4 out of 4 changed files in this pull request and generated 3 comments.
| File | Description |
|---|---|
| Tests/SourceKitLSPTests/CodeActionTests.swift | Adds tests for the new refactor and a helper to assert non-offering. |
| Sources/SwiftSyntaxCodeActions/SyntaxCodeActions.swift | Registers the new provider in the global provider list. |
| Sources/SwiftSyntaxCodeActions/CMakeLists.txt | Adds the new Swift source file to the CMake target. |
| Sources/SwiftSyntaxCodeActions/AddExplicitEnumRawValues.swift | Implements the refactor logic for adding explicit enum raw values. |
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
ahoppen
reviewed
May 26, 2026
Member
ahoppen
left a comment
There was a problem hiding this comment.
This looks great. Thank you @ayush-that. I just have two
3236b69 to
a8765a4
Compare
Adds a syntactic refactoring action that converts implicit raw values to explicit ones for enums whose first inherited type is an integer type (any width up to Int128/UInt128) or String. Resolves swiftlang#2516. Before: enum Status: Int { case active case inactive case pending = 10 case archived } After: enum Status: Int { case active = 0 case inactive = 1 case pending = 10 case archived = 11 } The action is exposed through `SyntaxRefactoringCodeActionProvider` and performs all transformations as a list of `SourceEdit`s, leaving the rest of the enum (including trailing trivia and member ordering) untouched. ### Motivation: For enums with implicit raw values, making the values explicit is a common refactoring step before serialising the values or relying on their numeric identity. Doing the renumbering by hand is error-prone once any cases already have explicit values, because the implicit continuation rule has to be reapplied for every gap. ### Modifications: - New `Sources/SwiftSyntaxCodeActions/AddExplicitEnumRawValues.swift` conforming to `EditRefactoringProvider` and `SyntaxRefactoringCodeActionProvider`. Handles negative integer literals, hex/binary/octal literals with underscore separators, and backticked case identifiers in String enums. - Registered in `Sources/SwiftSyntaxCodeActions/SyntaxCodeActions.swift` and `Sources/SwiftSyntaxCodeActions/CMakeLists.txt`. - Twelve LSP-level tests in `Tests/SourceKitLSPTests/CodeActionTests.swift` covering positive cases (Int, String, negative continuation, hex continuation, backtick stripping) and negative cases (no raw value type, raw value type not first in inheritance clause, all cases already explicit, unsupported raw value expression, #if directives, freestanding macro expansions, associated value cases). ### Result: When the cursor is on an enum declaration whose raw value type is supported and at least one case is missing an explicit raw value, SourceKit-LSP offers the "Add Explicit Raw Values" code action and inserts the missing raw values in place. The action is suppressed in situations where the implicit numbering cannot be computed safely (unsupported raw value expression, #if blocks, freestanding macros, associated value cases).
a8765a4 to
28e69b6
Compare
ahoppen
approved these changes
May 27, 2026
Member
ahoppen
left a comment
There was a problem hiding this comment.
This looks great. Thank you 🙏🏽
Member
|
@swift-ci Please test |
Member
|
@swift-ci Please test Windows |
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.
resolves #2516.
adds a syntactic refactoring action that converts implicit raw values to explicit ones for enums whose first inherited type is an integer (any width up to Int128/UInt128) or String.
before:
after:
design follows the feedback @ahoppen left on #2536: implemented as
SyntaxRefactoringCodeActionProvider, only the first inherited type is consulted for the raw value type, raw value expressions are validated as integer literals and the action is suppressed when they cannot be parsed, Int128 and UInt128 are accepted in the type list, and the action is suppressed when there is nothing to transform.beyond #2536, the action also handles:
case low = -5followed by implicit cases continues at -4)case bit0 = 0x10continues at 17)case `defaultbecomes= "default", not= "default")#ifdirectives (rintaro flagged this on the original issue)case payload = 0(String))motivation
making implicit raw values explicit is a common refactor before serialising values or relying on their numeric identity. doing it by hand is error-prone once any cases already have explicit values, because the continuation rule must be reapplied for every gap.
modifications
Sources/SwiftSyntaxCodeActions/AddExplicitEnumRawValues.swiftSources/SwiftSyntaxCodeActions/SyntaxCodeActions.swiftandSources/SwiftSyntaxCodeActions/CMakeLists.txtTests/SourceKitLSPTests/CodeActionTests.swiftcovering positive cases (Int, String, negative continuation, hex continuation, backtick stripping) and negative cases (no raw value type, raw value type not first, all cases explicit, unsupported raw value expression,#ifdirectives, freestanding macros, associated value cases)result
when the cursor is on an enum declaration with a supported raw value type and at least one case missing an explicit raw value, sourcekit-lsp offers "Add Explicit Raw Values" and inserts the missing values in place. existing positive and negative cases continue to behave as before.
all 12 new tests pass locally.