Skip to content

Commit 9b07134

Browse files
Generate JSON Schema and Markdown doc for .sourcekit-lsp/config.json
Providing a JSON schema for the configuration file should improve the developer experience thanks to better auto-completion and diagnostics support provided by some editors. Additionally, we have been manually maintaining the configuration file format documentation in `Documentation/Configuration File.md`, but it's easy for the documentation to get out of sync with the actual schema. This change introduces a new tool, `ConfigSchemaGen`, that generates a JSON schema and a Markdown document for the configuration file based on the Swift type definitions in the `SKOptions` module by using swift-syntax.
1 parent 3b6ced8 commit 9b07134

File tree

11 files changed

+986
-25
lines changed

11 files changed

+986
-25
lines changed

Documentation/Configuration File.md

Lines changed: 41 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
<!-- DO NOT EDIT THIS FILE. This file is generated by ConfigSchemaGen/OptionDocument.swift. -->
2+
13
# Configuration File
24

35
`.sourcekit-lsp/config.json` configuration files can be used to modify the behavior of SourceKit-LSP in various ways. The following locations are checked. Settings in later configuration files override settings in earlier configuration files
@@ -12,44 +14,64 @@ The structure of the file is currently not guaranteed to be stable. Options may
1214
## Structure
1315

1416
`config.json` is a JSON file with the following structure. All keys are optional and unknown keys are ignored.
15-
16-
- `swiftPM`: Dictionary with the following keys, defining options for SwiftPM workspaces
17-
- `configuration: "debug"|"release"`: The configuration to build the project for during background indexing and the configuration whose build folder should be used for Swift modules if background indexing is disabled. Equivalent to SwiftPM's `--configuration` option.
18-
- `scratchPath: string`: Build artifacts directory path. If nil, the build system may choose a default value. This path can be specified as a relative path, which will be interpreted relative to the project root. Equivalent to SwiftPM's `--scratch-path` option.
17+
- `swiftPM`: Options for SwiftPM workspaces
18+
- `configuration: "debug"|"release"`: The configuration to build the project for during background indexing
19+
and the configuration whose build folder should be used for Swift
20+
modules if background indexing is disabled.
21+
Equivalent to SwiftPM's `--configuration` option.
22+
- `scratchPath: string`: Build artifacts directory path. If nil, the build system may choose a default value.
23+
This path can be specified as a relative path, which will be interpreted relative to the project root.
24+
Equivalent to SwiftPM's `--scratch-path` option.
1925
- `swiftSDKsDirectory: string`: Equivalent to SwiftPM's `--swift-sdks-path` option
2026
- `swiftSDK: string`: Equivalent to SwiftPM's `--swift-sdk` option
2127
- `triple: string`: Equivalent to SwiftPM's `--triple` option
2228
- `cCompilerFlags: string[]`: Extra arguments passed to the compiler for C files. Equivalent to SwiftPM's `-Xcc` option.
2329
- `cxxCompilerFlags: string[]`: Extra arguments passed to the compiler for C++ files. Equivalent to SwiftPM's `-Xcxx` option.
2430
- `swiftCompilerFlags: string[]`: Extra arguments passed to the compiler for Swift files. Equivalent to SwiftPM's `-Xswiftc` option.
2531
- `linkerFlags: string[]`: Extra arguments passed to the linker. Equivalent to SwiftPM's `-Xlinker` option.
26-
- `disableSandbox: bool`: Disables running subprocesses from SwiftPM in a sandbox. Useful when running `sourcekit-lsp` in a sandbox because nested sandboxes are not supported.
32+
- `disableSandbox: boolean`: Disables running subprocesses from SwiftPM in a sandbox. Equivalent to SwiftPM's `--disable-sandbox` option
33+
Useful when running `sourcekit-lsp` in a sandbox because nested sandboxes are not supported.
2734
- `compilationDatabase`: Dictionary with the following keys, defining options for workspaces with a compilation database
2835
- `searchPaths: string[]`: Additional paths to search for a compilation database, relative to a workspace root.
2936
- `fallbackBuildSystem`: Dictionary with the following keys, defining options for files that aren't managed by any build system
3037
- `cCompilerFlags: string[]`: Extra arguments passed to the compiler for C files
3138
- `cxxCompilerFlags: string[]`: Extra arguments passed to the compiler for C++ files
3239
- `swiftCompilerFlags: string[]`: Extra arguments passed to the compiler for Swift files
3340
- `sdk: string`: The SDK to use for fallback arguments. Default is to infer the SDK using `xcrun`.
34-
- `buildSettingsTimeout: int`: Number of milliseconds to wait for build settings from the build system before using fallback build settings.
41+
- `buildSettingsTimeout: integer`: Number of milliseconds to wait for build settings from the build system before using fallback build settings.
3542
- `clangdOptions: string[]`: Extra command line arguments passed to `clangd` when launching it
36-
- `index`: Dictionary with the following keys, defining options related to indexing
43+
- `index`: Options related to indexing
3744
- `indexStorePath: string`: Directory in which a separate compilation stores the index store. By default, inferred from the build system.
3845
- `indexDatabasePath: string`: Directory in which the indexstore-db should be stored. By default, inferred from the build system.
3946
- `indexPrefixMap: [string: string]`: Path remappings for remapping index data for local use.
40-
- `maxCoresPercentageToUseForBackgroundIndexing: double`: A hint indicating how many cores background indexing should use at most (value between 0 and 1). Background indexing is not required to honor this setting
41-
- `updateIndexStoreTimeout: int`: Number of seconds to wait for an update index store task to finish before killing it.
42-
- `logging`: Dictionary with the following keys, changing SourceKit-LSP’s logging behavior on non-Apple platforms. On Apple platforms, logging is done through the [system log](Diagnose%20Bundle.md#Enable%20Extended%20Logging). These options can only be set globally and not per workspace.
43-
- `logLevel: "debug"|"info"|"default"|"error"|"fault"`: The level from which one onwards log messages should be written.
44-
- `privacyLevel: "public"|"private"|"sensitive"`: Whether potentially sensitive information should be redacted. Default is `public`, which redacts potentially sensitive information.
45-
- `inputMirrorDirectory: string`: Write all input received by SourceKit-LSP on stdin to a file in this directory. Useful to record and replay an entire SourceKit-LSP session.
46-
- `defaultWorkspaceType: "buildserver"|"compdb"|"swiftpm"`: Overrides workspace type selection logic.
47+
- `maxCoresPercentageToUseForBackgroundIndexing: number`: A hint indicating how many cores background indexing should use at most (value between 0 and 1). Background indexing is not required to honor this setting
48+
- `updateIndexStoreTimeout: integer`: Number of seconds to wait for an update index store task to finish before killing it.
49+
- `logging`: Options related to logging, changing SourceKit-LSP’s logging behavior on non-Apple platforms.
50+
On Apple platforms, logging is done through the [system log](Diagnose%20Bundle.md#Enable%20Extended%20Logging).
51+
These options can only be set globally and not per workspace.
52+
- `level: "debug"|"info"|"default"|"error"|"fault"`: The level from which one onwards log messages should be written.
53+
- `privacyLevel: "sensitive"|"private"|"public"`: Whether potentially sensitive information should be redacted.
54+
Default is `public`, which redacts potentially sensitive information.
55+
- `inputMirrorDirectory: string`: Write all input received by SourceKit-LSP on stdin to a file in this directory.
56+
Useful to record and replay an entire SourceKit-LSP session.
57+
- `defaultWorkspaceType: "buildServer"|"compilationDatabase"|"swiftPM"`: Default workspace type (buildserver|compdb|swiftpm). Overrides workspace type selection logic.
4758
- `generatedFilesPath: string`: Directory in which generated interfaces and macro expansions should be stored.
48-
- `backgroundIndexing: bool`: Explicitly enable or disable background indexing.
49-
- `backgroundPreparationMode: "build"|"noLazy"|"enabled"`: Determines how background indexing should prepare a target. Possible values are:
59+
- `backgroundIndexing: boolean`: Whether background indexing is enabled.
60+
- `backgroundPreparationMode: string`: Determines how background indexing should prepare a target. Possible values are:
5061
- `build`: Build a target to prepare it
5162
- `noLazy`: Prepare a target without generating object files but do not do lazy type checking and function body skipping
5263
- `enabled`: Prepare a target without generating object files and the like
53-
- `cancelTextDocumentRequestsOnEditAndClose: bool`: Whether sending a `textDocument/didChange` or `textDocument/didClose` notification for a document should cancel all pending requests for that document.
54-
- `experimentalFeatures: string[]`: Experimental features to enable. Available features: on-type-formatting
55-
- `swiftPublishDiagnosticsDebounceDuration: double`: The time that `SwiftLanguageService` should wait after an edit before starting to compute diagnostics and sending a `PublishDiagnosticsNotification`.
64+
- `cancelTextDocumentRequestsOnEditAndClose: boolean`: Whether sending a `textDocument/didChange` or `textDocument/didClose` notification for a document should cancel
65+
all pending requests for that document.
66+
- `experimentalFeatures: ("on-type-formatting")[]`: Experimental features that are enabled.
67+
- `swiftPublishDiagnosticsDebounceDuration: number`: The time that `SwiftLanguageService` should wait after an edit before starting to compute diagnostics and
68+
sending a `PublishDiagnosticsNotification`.
69+
- `workDoneProgressDebounceDuration: number`: When a task is started that should be displayed to the client as a work done progress, how many milliseconds to
70+
wait before actually starting the work done progress. This prevents flickering of the work done progress in the
71+
client for short-lived index tasks which end within this duration.
72+
- `sourcekitdRequestTimeout: number`: The maximum duration that a sourcekitd request should be allowed to execute before being declared as timed out.
73+
In general, editors should cancel requests that they are no longer interested in, but in case editors don't cancel
74+
requests, this ensures that a long-running non-cancelled request is not blocking sourcekitd and thus most semantic
75+
functionality.
76+
In particular, VS Code does not cancel the semantic tokens request, which can cause a long-running AST build that
77+
blocks sourcekitd.

Sources/SKOptions/SourceKitLSPOptions.swift

Lines changed: 34 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -31,13 +31,16 @@ import struct TSCBasic.AbsolutePath
3131
/// See `ConfigurationFile.md` for a description of the configuration file's behavior.
3232
public struct SourceKitLSPOptions: Sendable, Codable, Equatable {
3333
public struct SwiftPMOptions: Sendable, Codable, Equatable {
34-
/// Build configuration (debug|release).
34+
/// The configuration to build the project for during background indexing
35+
/// and the configuration whose build folder should be used for Swift
36+
/// modules if background indexing is disabled.
3537
///
3638
/// Equivalent to SwiftPM's `--configuration` option.
3739
public var configuration: BuildConfiguration?
3840

3941
/// Build artifacts directory path. If nil, the build system may choose a default value.
4042
///
43+
/// This path can be specified as a relative path, which will be interpreted relative to the project root.
4144
/// Equivalent to SwiftPM's `--scratch-path` option.
4245
public var scratchPath: String?
4346

@@ -50,19 +53,20 @@ public struct SourceKitLSPOptions: Sendable, Codable, Equatable {
5053
/// Equivalent to SwiftPM's `--triple` option
5154
public var triple: String?
5255

53-
/// Equivalent to SwiftPM's `-Xcc` option
56+
/// Extra arguments passed to the compiler for C files. Equivalent to SwiftPM's `-Xcc` option.
5457
public var cCompilerFlags: [String]?
5558

56-
/// Equivalent to SwiftPM's `-Xcxx` option
59+
/// Extra arguments passed to the compiler for C++ files. Equivalent to SwiftPM's `-Xcxx` option.
5760
public var cxxCompilerFlags: [String]?
5861

59-
/// Equivalent to SwiftPM's `-Xswiftc` option
62+
/// Extra arguments passed to the compiler for Swift files. Equivalent to SwiftPM's `-Xswiftc` option.
6063
public var swiftCompilerFlags: [String]?
6164

62-
/// Equivalent to SwiftPM's `-Xlinker` option
65+
/// Extra arguments passed to the linker. Equivalent to SwiftPM's `-Xlinker` option.
6366
public var linkerFlags: [String]?
6467

65-
/// Equivalent to SwiftPM's `--disable-sandbox` option
68+
/// Disables running subprocesses from SwiftPM in a sandbox. Equivalent to SwiftPM's `--disable-sandbox` option
69+
/// Useful when running `sourcekit-lsp` in a sandbox because nested sandboxes are not supported.
6670
public var disableSandbox: Bool?
6771

6872
public init(
@@ -122,9 +126,13 @@ public struct SourceKitLSPOptions: Sendable, Codable, Equatable {
122126
}
123127

124128
public struct FallbackBuildSystemOptions: Sendable, Codable, Equatable {
129+
/// Extra arguments passed to the compiler for C files
125130
public var cCompilerFlags: [String]?
131+
/// Extra arguments passed to the compiler for C++ files
126132
public var cxxCompilerFlags: [String]?
133+
/// Extra arguments passed to the compiler for Swift files
127134
public var swiftCompilerFlags: [String]?
135+
/// The SDK to use for fallback arguments. Default is to infer the SDK using `xcrun`.
128136
public var sdk: String?
129137

130138
public init(
@@ -153,10 +161,15 @@ public struct SourceKitLSPOptions: Sendable, Codable, Equatable {
153161
}
154162

155163
public struct IndexOptions: Sendable, Codable, Equatable {
164+
/// Directory in which a separate compilation stores the index store. By default, inferred from the build system.
156165
public var indexStorePath: String?
166+
/// Directory in which the indexstore-db should be stored. By default, inferred from the build system.
157167
public var indexDatabasePath: String?
168+
/// Path remappings for remapping index data for local use.
158169
public var indexPrefixMap: [String: String]?
170+
/// A hint indicating how many cores background indexing should use at most (value between 0 and 1). Background indexing is not required to honor this setting
159171
public var maxCoresPercentageToUseForBackgroundIndexing: Double?
172+
/// Number of seconds to wait for an update index store task to finish before killing it.
160173
public var updateIndexStoreTimeout: Int?
161174

162175
public var maxCoresPercentageToUseForBackgroundIndexingOrDefault: Double {
@@ -202,6 +215,7 @@ public struct SourceKitLSPOptions: Sendable, Codable, Equatable {
202215
public var level: String?
203216

204217
/// Whether potentially sensitive information should be redacted.
218+
/// Default is `public`, which redacts potentially sensitive information.
205219
public var privacyLevel: String?
206220

207221
/// Write all input received by SourceKit-LSP on stdin to a file in this directory.
@@ -241,18 +255,21 @@ public struct SourceKitLSPOptions: Sendable, Codable, Equatable {
241255
case enabled
242256
}
243257

258+
/// Options for SwiftPM workspaces
244259
private var swiftPM: SwiftPMOptions?
245260
public var swiftPMOrDefault: SwiftPMOptions {
246261
get { swiftPM ?? .init() }
247262
set { swiftPM = newValue }
248263
}
249264

265+
/// Dictionary with the following keys, defining options for workspaces with a compilation database
250266
private var compilationDatabase: CompilationDatabaseOptions?
251267
public var compilationDatabaseOrDefault: CompilationDatabaseOptions {
252268
get { compilationDatabase ?? .init() }
253269
set { compilationDatabase = newValue }
254270
}
255271

272+
/// Dictionary with the following keys, defining options for files that aren't managed by any build system
256273
private var fallbackBuildSystem: FallbackBuildSystemOptions?
257274
public var fallbackBuildSystemOrDefault: FallbackBuildSystemOptions {
258275
get { fallbackBuildSystem ?? .init() }
@@ -266,14 +283,20 @@ public struct SourceKitLSPOptions: Sendable, Codable, Equatable {
266283
get { .milliseconds(buildSettingsTimeout ?? 500) }
267284
}
268285

286+
/// Extra command line arguments passed to `clangd` when launching it
269287
public var clangdOptions: [String]?
270288

289+
/// Options related to indexing
271290
private var index: IndexOptions?
272291
public var indexOrDefault: IndexOptions {
273292
get { index ?? .init() }
274293
set { index = newValue }
275294
}
276295

296+
/// Options related to logging, changing SourceKit-LSP’s logging behavior on non-Apple platforms.
297+
///
298+
/// On Apple platforms, logging is done through the [system log](Diagnose%20Bundle.md#Enable%20Extended%20Logging).
299+
/// These options can only be set globally and not per workspace.
277300
private var logging: LoggingOptions?
278301
public var loggingOrDefault: LoggingOptions {
279302
get { logging ?? .init() }
@@ -282,6 +305,7 @@ public struct SourceKitLSPOptions: Sendable, Codable, Equatable {
282305

283306
/// Default workspace type (buildserver|compdb|swiftpm). Overrides workspace type selection logic.
284307
public var defaultWorkspaceType: WorkspaceType?
308+
/// Directory in which generated interfaces and macro expansions should be stored.
285309
public var generatedFilesPath: String?
286310

287311
/// Whether background indexing is enabled.
@@ -291,6 +315,10 @@ public struct SourceKitLSPOptions: Sendable, Codable, Equatable {
291315
return backgroundIndexing ?? true
292316
}
293317

318+
/// Determines how background indexing should prepare a target. Possible values are:
319+
/// - `build`: Build a target to prepare it
320+
/// - `noLazy`: Prepare a target without generating object files but do not do lazy type checking and function body skipping
321+
/// - `enabled`: Prepare a target without generating object files and the like
294322
public var backgroundPreparationMode: String?
295323

296324
public var backgroundPreparationModeOrDefault: BackgroundPreparationMode {

Utilities/ConfigSchemaGen/.gitignore

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
.DS_Store
2+
/.build
3+
/Packages
4+
xcuserdata/
5+
DerivedData/
6+
.swiftpm/configuration/registries.json
7+
.swiftpm/xcode/package.xcworkspace/contents.xcworkspacedata
8+
.netrc
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
// swift-tools-version: 6.0
2+
3+
import PackageDescription
4+
5+
let package = Package(
6+
name: "ConfigSchemaGen",
7+
platforms: [.macOS(.v10_15)],
8+
dependencies: [
9+
.package(url: "https://github.com/swiftlang/swift-syntax.git", from: "600.0.1")
10+
],
11+
targets: [
12+
.executableTarget(
13+
name: "ConfigSchemaGen",
14+
dependencies: [
15+
.product(name: "SwiftSyntax", package: "swift-syntax"),
16+
.product(name: "SwiftParser", package: "swift-syntax"),
17+
]
18+
)
19+
]
20+
)

Utilities/ConfigSchemaGen/README.md

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
# Configuration Schema Generation for SourceKit-LSP
2+
3+
This directory contains a tool to generate a JSON schema and Markdown
4+
documentation for the SourceKit-LSP configuration file format
5+
(`.sourcekit-lsp/config.json`) from the Swift type definitions in `SKOptions`
6+
Swift module.
7+
8+
To regenerate the schema and documentation, run the following command from the
9+
root of the repository:
10+
11+
```sh
12+
swift run --package-path Utilities/ConfigSchemaGen
13+
```
14+

0 commit comments

Comments
 (0)