Files
Ayush Thakur 28e69b6c25 Add 'Add Explicit Raw Values' code action
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 #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).
2026-05-26 20:07:03 +05:30

33 lines
992 B
CMake

add_library(SwiftSyntaxCodeActions STATIC
AddDocumentation.swift
AddExplicitEnumRawValues.swift
ApplyDeMorganLaw.swift
ConvertCommentToDocComment.swift
ConvertIfLetToGuard.swift
ConvertIntegerLiteral.swift
ConvertJSONToCodableStruct.swift
ConvertStringConcatenationToStringInterpolation.swift
IndentationRemover.swift
PackageManifestEdits.swift
SyntaxCodeActionProvider.swift
SyntaxCodeActions.swift
SyntaxRefactoringCodeActionProvider.swift
)
set_target_properties(SwiftSyntaxCodeActions PROPERTIES
INTERFACE_INCLUDE_DIRECTORIES ${CMAKE_Swift_MODULE_DIRECTORY})
target_link_libraries(SwiftSyntaxCodeActions PUBLIC
SourceKitLSP
SwiftToolsProtocols::LanguageServerProtocol
SwiftSyntax::SwiftSyntax
)
target_link_libraries(SwiftSyntaxCodeActions PRIVATE
SwiftToolsProtocols::SKLogging
SwiftExtensions
SwiftSyntax::SwiftBasicFormat
SwiftSyntax::SwiftOperators
SwiftSyntax::SwiftParser
SwiftSyntax::SwiftRefactor
SwiftSyntax::SwiftSyntaxBuilder
)