mirror of
https://github.com/apple/sourcekit-lsp.git
synced 2026-03-02 18:23:24 +01:00
Add a syntactic action that takes JSON pasted into a Swift file or
placed in a string literal, then turns it into a set of Codable
structs that can represent the JSON. Our typical example starts like
this:
```
{
"name": "Produce",
"shelves": [
{
"name": "Discount Produce",
"product": {
"name": "Banana",
"points": 200,
"description": "A banana that's perfectly ripe."
}
}
]
}
```
and turns into this:
```swift
struct JSONValue: Codable {
var name: String
var shelves: [Shelves]
struct Shelves: Codable {
var name: String
var product: Product
struct Product: Codable {
var description: String
var name: String
var points: Double
}
}
}
```
When converting to JSON, we attempt to reason about multiple JSON
objects on the same level to detect when there are optional fields,
due to either an explicit null or due to the absence of fields in some
of the JSON objects that are conceptually stored together.
The refactoring itself would live down in the swift-syntax package if
not for its dependency on Foundation. We'll move it when appropriate.
28 lines
1.0 KiB
Swift
28 lines
1.0 KiB
Swift
//===----------------------------------------------------------------------===//
|
|
//
|
|
// This source file is part of the Swift.org open source project
|
|
//
|
|
// Copyright (c) 2014 - 2024 Apple Inc. and the Swift project authors
|
|
// Licensed under Apache License v2.0 with Runtime Library Exception
|
|
//
|
|
// See https://swift.org/LICENSE.txt for license information
|
|
// See https://swift.org/CONTRIBUTORS.txt for the list of Swift project authors
|
|
//
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
import SwiftRefactor
|
|
|
|
/// List of all of the syntactic code action providers, which can be used
|
|
/// to produce code actions using only the swift-syntax tree of a file.
|
|
let allSyntaxCodeActions: [SyntaxCodeActionProvider.Type] = [
|
|
AddDocumentation.self,
|
|
AddSeparatorsToIntegerLiteral.self,
|
|
ConvertIntegerLiteral.self,
|
|
ConvertJSONToCodableStruct.self,
|
|
FormatRawStringLiteral.self,
|
|
MigrateToNewIfLetSyntax.self,
|
|
OpaqueParameterToGeneric.self,
|
|
PackageManifestEdits.self,
|
|
RemoveSeparatorsFromIntegerLiteral.self,
|
|
]
|