diff --git a/.gitignore b/.gitignore index 7bbe02da3..6284f660e 100644 --- a/.gitignore +++ b/.gitignore @@ -1,7 +1,7 @@ .DS_Store default.profraw Package.resolved -/.build +.build /.*-build /Packages /*.xcodeproj diff --git a/CMakeLists.txt b/CMakeLists.txt index d63e0b760..fff4634e1 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -17,6 +17,7 @@ add_compile_options("$<$:SHELL:-enable-upcoming-feature find_package(dispatch QUIET) find_package(Foundation QUIET) find_package(TSC QUIET) +find_package(LMDB QUIET) find_package(IndexStoreDB QUIET) find_package(SwiftPM QUIET) find_package(LLBuild QUIET) diff --git a/CONTRIBUTING.md b/CONTRIBUTING.md index 7e12b83b8..8857329a0 100644 --- a/CONTRIBUTING.md +++ b/CONTRIBUTING.md @@ -115,6 +115,14 @@ swift package format-source-code If you are developing SourceKit-LSP in VS Code, you can also run the *Run swift-format* task from *Tasks: Run tasks* in the command palette. +## Generate configuration schema + +If you modify the configuration options in [`SKOptions`](./Sources/SKOptions), you need to regenerate the configuration the JSON schema and the documentation by running the following command: + +```bash +./sourcekit-lsp-dev-utils generate-config-schema +``` + ## Authoring commits Prefer to squash the commits of your PR (*pull request*) and avoid adding commits like “Address review comments”. This creates a clearer git history, which doesn’t need to record the history of how the PR evolved. diff --git a/Documentation/Configuration File.md b/Documentation/Configuration File.md index 48b84eb30..1aae1c83d 100644 --- a/Documentation/Configuration File.md +++ b/Documentation/Configuration File.md @@ -1,3 +1,5 @@ + + # Configuration File `.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,46 @@ The structure of the file is currently not guaranteed to be stable. Options may ## Structure `config.json` is a JSON file with the following structure. All keys are optional and unknown keys are ignored. - -- `swiftPM`: Dictionary with the following keys, defining options for SwiftPM workspaces +- `swiftPM`: Options for SwiftPM workspaces. - `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. - `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. - - `swiftSDKsDirectory: string`: Equivalent to SwiftPM's `--swift-sdks-path` option - - `swiftSDK: string`: Equivalent to SwiftPM's `--swift-sdk` option - - `triple: string`: Equivalent to SwiftPM's `--triple` option + - `swiftSDKsDirectory: string`: Equivalent to SwiftPM's `--swift-sdks-path` option. + - `swiftSDK: string`: Equivalent to SwiftPM's `--swift-sdk` option. + - `triple: string`: Equivalent to SwiftPM's `--triple` option. - `cCompilerFlags: string[]`: Extra arguments passed to the compiler for C files. Equivalent to SwiftPM's `-Xcc` option. - `cxxCompilerFlags: string[]`: Extra arguments passed to the compiler for C++ files. Equivalent to SwiftPM's `-Xcxx` option. - `swiftCompilerFlags: string[]`: Extra arguments passed to the compiler for Swift files. Equivalent to SwiftPM's `-Xswiftc` option. - `linkerFlags: string[]`: Extra arguments passed to the linker. Equivalent to SwiftPM's `-Xlinker` option. - - `disableSandbox: bool`: Disables running subprocesses from SwiftPM in a sandbox. Useful when running `sourcekit-lsp` in a sandbox because nested sandboxes are not supported. -- `compilationDatabase`: Dictionary with the following keys, defining options for workspaces with a compilation database + - `disableSandbox: boolean`: Disables running subprocesses from SwiftPM in a sandbox. Equivalent to SwiftPM's `--disable-sandbox` option. Useful when running `sourcekit-lsp` in a sandbox because nested sandboxes are not supported. +- `compilationDatabase`: Dictionary with the following keys, defining options for workspaces with a compilation database. - `searchPaths: string[]`: Additional paths to search for a compilation database, relative to a workspace root. -- `fallbackBuildSystem`: Dictionary with the following keys, defining options for files that aren't managed by any build system - - `cCompilerFlags: string[]`: Extra arguments passed to the compiler for C files - - `cxxCompilerFlags: string[]`: Extra arguments passed to the compiler for C++ files - - `swiftCompilerFlags: string[]`: Extra arguments passed to the compiler for Swift files +- `fallbackBuildSystem`: Dictionary with the following keys, defining options for files that aren't managed by any build system. + - `cCompilerFlags: string[]`: Extra arguments passed to the compiler for C files. + - `cxxCompilerFlags: string[]`: Extra arguments passed to the compiler for C++ files. + - `swiftCompilerFlags: string[]`: Extra arguments passed to the compiler for Swift files. - `sdk: string`: The SDK to use for fallback arguments. Default is to infer the SDK using `xcrun`. -- `buildSettingsTimeout: int`: Number of milliseconds to wait for build settings from the build system before using fallback build settings. -- `clangdOptions: string[]`: Extra command line arguments passed to `clangd` when launching it -- `index`: Dictionary with the following keys, defining options related to indexing +- `buildSettingsTimeout: integer`: Number of milliseconds to wait for build settings from the build system before using fallback build settings. +- `clangdOptions: string[]`: Extra command line arguments passed to `clangd` when launching it. +- `index`: Options related to indexing. - `indexStorePath: string`: Directory in which a separate compilation stores the index store. By default, inferred from the build system. - `indexDatabasePath: string`: Directory in which the indexstore-db should be stored. By default, inferred from the build system. - `indexPrefixMap: [string: string]`: Path remappings for remapping index data for local use. - - `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 - - `updateIndexStoreTimeout: int`: Number of seconds to wait for an update index store task to finish before killing it. -- `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. - - `logLevel: "debug"|"info"|"default"|"error"|"fault"`: The level from which one onwards log messages should be written. + - `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. + - `updateIndexStoreTimeout: integer`: Number of seconds to wait for an update index store task to finish before killing it. +- `logging`: Options related to logging, 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. + - `level: "debug"|"info"|"default"|"error"|"fault"`: The level from which one onwards log messages should be written. - `privacyLevel: "public"|"private"|"sensitive"`: Whether potentially sensitive information should be redacted. Default is `public`, which redacts potentially sensitive information. - `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. -- `defaultWorkspaceType: "buildserver"|"compdb"|"swiftpm"`: Overrides workspace type selection logic. + - `outputMirrorDirectory: string`: Write all data sent from SourceKit-LSP to the client to a file in this directory. Useful to record the raw communication between SourceKit-LSP and the client on a low level. +- `defaultWorkspaceType: "buildServer"|"compilationDatabase"|"swiftPM"`: Default workspace type. Overrides workspace type selection logic. - `generatedFilesPath: string`: Directory in which generated interfaces and macro expansions should be stored. -- `backgroundIndexing: bool`: Explicitly enable or disable background indexing. -- `backgroundPreparationMode: "build"|"noLazy"|"enabled"`: Determines how background indexing should prepare a target. Possible values are: - - `build`: Build a target to prepare it - - `noLazy`: Prepare a target without generating object files but do not do lazy type checking and function body skipping - - `enabled`: Prepare a target without generating object files and the like -- `cancelTextDocumentRequestsOnEditAndClose: bool`: Whether sending a `textDocument/didChange` or `textDocument/didClose` notification for a document should cancel all pending requests for that document. -- `experimentalFeatures: string[]`: Experimental features to enable. Available features: on-type-formatting -- `swiftPublishDiagnosticsDebounceDuration: double`: The time that `SwiftLanguageService` should wait after an edit before starting to compute diagnostics and sending a `PublishDiagnosticsNotification`. +- `backgroundIndexing: boolean`: Whether background indexing is enabled. +- `backgroundPreparationMode: "build"|"noLazy"|"enabled"`: Determines how background indexing should prepare a target. + - `build`: Build a target to prepare it. + - `noLazy`: Prepare a target without generating object files but do not do lazy type checking and function body skipping. This uses SwiftPM's `--experimental-prepare-for-indexing-no-lazy` flag. + - `enabled`: Prepare a target without generating object files. +- `cancelTextDocumentRequestsOnEditAndClose: boolean`: Whether sending a `textDocument/didChange` or `textDocument/didClose` notification for a document should cancel all pending requests for that document. +- `experimentalFeatures: ("on-type-formatting")[]`: Experimental features that are enabled. +- `swiftPublishDiagnosticsDebounceDuration: number`: The time that `SwiftLanguageService` should wait after an edit before starting to compute diagnostics and sending a `PublishDiagnosticsNotification`. +- `workDoneProgressDebounceDuration: number`: When a task is started that should be displayed to the client as a work done progress, how many milliseconds to wait before actually starting the work done progress. This prevents flickering of the work done progress in the client for short-lived index tasks which end within this duration. +- `sourcekitdRequestTimeout: number`: The maximum duration that a sourcekitd request should be allowed to execute before being declared as timed out. In general, editors should cancel requests that they are no longer interested in, but in case editors don't cancel requests, this ensures that a long-running non-cancelled request is not blocking sourcekitd and thus most semantic functionality. In particular, VS Code does not cancel the semantic tokens request, which can cause a long-running AST build that blocks sourcekitd. diff --git a/SourceKitLSPDevUtils/Package.swift b/SourceKitLSPDevUtils/Package.swift new file mode 100644 index 000000000..9fd8070dc --- /dev/null +++ b/SourceKitLSPDevUtils/Package.swift @@ -0,0 +1,31 @@ +// swift-tools-version: 6.0 + +import PackageDescription + +let package = Package( + name: "SourceKitLSPDevUtils", + platforms: [.macOS(.v10_15)], + products: [ + .executable(name: "sourcekit-lsp-dev-utils", targets: ["SourceKitLSPDevUtils"]) + ], + dependencies: [ + .package(url: "https://github.com/swiftlang/swift-syntax.git", from: "600.0.1"), + .package(url: "https://github.com/apple/swift-argument-parser.git", from: "1.5.0"), + ], + targets: [ + .executableTarget( + name: "SourceKitLSPDevUtils", + dependencies: [ + "ConfigSchemaGen", + .product(name: "ArgumentParser", package: "swift-argument-parser"), + ] + ), + .target( + name: "ConfigSchemaGen", + dependencies: [ + .product(name: "SwiftSyntax", package: "swift-syntax"), + .product(name: "SwiftParser", package: "swift-syntax"), + ] + ), + ] +) diff --git a/SourceKitLSPDevUtils/README.md b/SourceKitLSPDevUtils/README.md new file mode 100644 index 000000000..5b5829060 --- /dev/null +++ b/SourceKitLSPDevUtils/README.md @@ -0,0 +1,3 @@ +# sourcekit-lsp-dev-utils + +This directory contains utilities for developing SourceKit-LSP. [CONTRIBUTING.md](../CONTRIBUTING.md) covers how to use these utilities. diff --git a/SourceKitLSPDevUtils/Sources/ConfigSchemaGen/ConfigSchemaGen.swift b/SourceKitLSPDevUtils/Sources/ConfigSchemaGen/ConfigSchemaGen.swift new file mode 100644 index 000000000..8d4fe8074 --- /dev/null +++ b/SourceKitLSPDevUtils/Sources/ConfigSchemaGen/ConfigSchemaGen.swift @@ -0,0 +1,151 @@ +//===----------------------------------------------------------------------===// +// +// 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 Foundation +import SwiftParser +import SwiftSyntax + +/// The main entry point for generating a JSON schema and Markdown documentation +/// for the SourceKit-LSP configuration file format +/// (`.sourcekit-lsp/config.json`) from the Swift type definitions in +/// `SKOptions` Swift module. +package struct ConfigSchemaGen { + private struct WritePlan { + fileprivate let category: String + fileprivate let path: URL + fileprivate let contents: () throws -> Data + + fileprivate func write() throws { + try contents().write(to: path) + } + } + + private static let projectRoot = URL(fileURLWithPath: #filePath) + .deletingLastPathComponent() + .deletingLastPathComponent() + .deletingLastPathComponent() + .deletingLastPathComponent() + private static let sourceDir = + projectRoot + .appendingPathComponent("Sources") + .appendingPathComponent("SKOptions") + private static let configSchemaJSONPath = + projectRoot + .appendingPathComponent("config.schema.json") + private static let configSchemaDocPath = + projectRoot + .appendingPathComponent("Documentation") + .appendingPathComponent("Configuration File.md") + + /// Generates and writes the JSON schema and documentation for the SourceKit-LSP configuration file format. + package static func generate() throws { + let plans = try plan() + for plan in plans { + print("Writing \(plan.category) to \"\(plan.path.path)\"") + try plan.write() + } + } + + /// Verifies that the generated JSON schema and documentation in the current source tree + /// are up-to-date with the Swift type definitions in `SKOptions`. + /// - Returns: `true` if the generated files are up-to-date, `false` otherwise. + package static func verify() throws -> Bool { + let plans = try plan() + for plan in plans { + print("Verifying \(plan.category) at \"\(plan.path.path)\"") + let expectedContents = try plan.contents() + let actualContents = try Data(contentsOf: plan.path) + guard expectedContents == actualContents else { + print("error: \(plan.category) is out-of-date!") + print("Please run `./sourcekit-lsp-dev-utils generate-config-schema` to update it.") + return false + } + } + return true + } + + private static func plan() throws -> [WritePlan] { + let sourceFiles = FileManager.default.enumerator(at: sourceDir, includingPropertiesForKeys: nil)! + let typeNameResolver = TypeDeclResolver() + + for case let fileURL as URL in sourceFiles { + guard fileURL.pathExtension == "swift" else { + continue + } + let sourceText = try String(contentsOf: fileURL) + let sourceFile = Parser.parse(source: sourceText) + typeNameResolver.collect(from: sourceFile) + } + let rootTypeDecl = try typeNameResolver.lookupType(fullyQualified: ["SourceKitLSPOptions"]) + let context = OptionSchemaContext(typeNameResolver: typeNameResolver) + var schema = try context.buildSchema(from: rootTypeDecl) + + // Manually annotate the logging level enum since LogLevel type exists + // outside of the SKOptions module + schema["logging"]?["level"]?.kind = .enum( + OptionTypeSchama.Enum( + name: "LogLevel", + cases: ["debug", "info", "default", "error", "fault"].map { + OptionTypeSchama.Case(name: $0) + } + ) + ) + schema["logging"]?["privacyLevel"]?.kind = .enum( + OptionTypeSchama.Enum( + name: "PrivacyLevel", + cases: ["public", "private", "sensitive"].map { + OptionTypeSchama.Case(name: $0) + } + ) + ) + + return [ + WritePlan( + category: "JSON Schema", + path: configSchemaJSONPath, + contents: { try generateJSONSchema(from: schema, context: context) } + ), + WritePlan( + category: "Schema Documentation", + path: configSchemaDocPath, + contents: { try generateDocumentation(from: schema, context: context) } + ), + ] + } + + private static func generateJSONSchema(from schema: OptionTypeSchama, context: OptionSchemaContext) throws -> Data { + let schemaBuilder = JSONSchemaBuilder(context: context) + var jsonSchema = try schemaBuilder.build(from: schema) + jsonSchema.title = "SourceKit-LSP Configuration" + jsonSchema.comment = "DO NOT EDIT THIS FILE. This file is generated by \(#fileID)." + let encoder = JSONEncoder() + encoder.outputFormatting = [.prettyPrinted, .sortedKeys] + return try encoder.encode(jsonSchema) + } + + private static func generateDocumentation(from schema: OptionTypeSchama, context: OptionSchemaContext) throws -> Data + { + let docBuilder = OptionDocumentBuilder(context: context) + guard let data = try docBuilder.build(from: schema).data(using: .utf8) else { + throw ConfigSchemaGenError("Failed to encode documentation as UTF-8") + } + return data + } +} + +struct ConfigSchemaGenError: Error, CustomStringConvertible { + let description: String + + init(_ description: String) { + self.description = description + } +} diff --git a/SourceKitLSPDevUtils/Sources/ConfigSchemaGen/JSONSchema.swift b/SourceKitLSPDevUtils/Sources/ConfigSchemaGen/JSONSchema.swift new file mode 100644 index 000000000..ec35b5700 --- /dev/null +++ b/SourceKitLSPDevUtils/Sources/ConfigSchemaGen/JSONSchema.swift @@ -0,0 +1,140 @@ +//===----------------------------------------------------------------------===// +// +// 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 +// +//===----------------------------------------------------------------------===// + +@propertyWrapper +final class HeapBox { + var wrappedValue: T + init(wrappedValue: T) { + self.wrappedValue = wrappedValue + } +} + +/// JSON Schema representation for version draft-07 +/// https://json-schema.org/draft-07/draft-handrews-json-schema-01 +/// +/// NOTE: draft-07 is the latest version of JSON Schema that is supported by +/// most of the tools. We may need to update this schema in the future. +struct JSONSchema: Encodable { + enum CodingKeys: String, CodingKey { + case _schema = "$schema" + case id = "$id" + case comment = "$comment" + case title + case type + case description + case properties + case required + case `enum` + case items + case additionalProperties + case markdownDescription + case markdownEnumDescriptions + } + var _schema: String? + var id: String? + var comment: String? + var title: String? + var type: String? + var description: String? + var properties: [String: JSONSchema]? + var required: [String]? + var `enum`: [String]? + @HeapBox + var items: JSONSchema? + @HeapBox + var additionalProperties: JSONSchema? + + /// VSCode extension: Markdown formatted description for rich hover + /// https://github.com/microsoft/vscode-wiki/blob/main/Setting-Descriptions.md + var markdownDescription: String? + /// VSCode extension: Markdown formatted descriptions for rich hover for enum values + /// https://github.com/microsoft/vscode-wiki/blob/main/Setting-Descriptions.md + var markdownEnumDescriptions: [String]? + + func encode(to encoder: any Encoder) throws { + // Manually implement encoding to use `encodeIfPresent` for HeapBox-ed fields + var container = encoder.container(keyedBy: CodingKeys.self) + try container.encodeIfPresent(_schema, forKey: ._schema) + try container.encodeIfPresent(id, forKey: .id) + try container.encodeIfPresent(comment, forKey: .comment) + try container.encodeIfPresent(title, forKey: .title) + try container.encodeIfPresent(type, forKey: .type) + try container.encodeIfPresent(description, forKey: .description) + if let properties = properties, !properties.isEmpty { + try container.encode(properties, forKey: .properties) + } + if let required = required, !required.isEmpty { + try container.encode(required, forKey: .required) + } + try container.encodeIfPresent(`enum`, forKey: .enum) + try container.encodeIfPresent(items, forKey: .items) + try container.encodeIfPresent(additionalProperties, forKey: .additionalProperties) + try container.encodeIfPresent(markdownDescription, forKey: .markdownDescription) + if let markdownEnumDescriptions { + try container.encode(markdownEnumDescriptions, forKey: .markdownEnumDescriptions) + } + } +} + +struct JSONSchemaBuilder { + let context: OptionSchemaContext + + func build(from typeSchema: OptionTypeSchama) throws -> JSONSchema { + var schema = try buildJSONSchema(from: typeSchema) + schema._schema = "http://json-schema.org/draft-07/schema#" + return schema + } + + private func buildJSONSchema(from typeSchema: OptionTypeSchama) throws -> JSONSchema { + var schema = JSONSchema() + switch typeSchema.kind { + case .boolean: schema.type = "boolean" + case .integer: schema.type = "integer" + case .number: schema.type = "number" + case .string: schema.type = "string" + case .array(value: let value): + schema.type = "array" + schema.items = try buildJSONSchema(from: value) + case .dictionary(value: let value): + schema.type = "object" + schema.additionalProperties = try buildJSONSchema(from: value) + case .struct(let structInfo): + schema.type = "object" + var properties: [String: JSONSchema] = [:] + var required: [String] = [] + for property in structInfo.properties { + let propertyType = property.type + var propertySchema = try buildJSONSchema(from: propertyType) + propertySchema.description = property.description + // As we usually use Markdown syntax for doc comments, set `markdownDescription` + // too for better rendering in VSCode. + propertySchema.markdownDescription = property.description + properties[property.name] = propertySchema + if !propertyType.isOptional { + required.append(property.name) + } + } + schema.properties = properties + schema.required = required + case .enum(let enumInfo): + schema.type = "string" + schema.enum = enumInfo.cases.map(\.name) + // Set `markdownEnumDescriptions` for better rendering in VSCode rich hover + // Unlike `description`, `enumDescriptions` field is not a part of JSON Schema spec, + // so we only set `markdownEnumDescriptions` here. + if enumInfo.cases.contains(where: { $0.description != nil }) { + schema.markdownEnumDescriptions = enumInfo.cases.map { $0.description ?? "" } + } + } + return schema + } +} diff --git a/SourceKitLSPDevUtils/Sources/ConfigSchemaGen/OptionDocument.swift b/SourceKitLSPDevUtils/Sources/ConfigSchemaGen/OptionDocument.swift new file mode 100644 index 000000000..23271d64c --- /dev/null +++ b/SourceKitLSPDevUtils/Sources/ConfigSchemaGen/OptionDocument.swift @@ -0,0 +1,107 @@ +//===----------------------------------------------------------------------===// +// +// 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 +// +//===----------------------------------------------------------------------===// + +/// Generates a markdown document for the configuration file based on the schema. +struct OptionDocumentBuilder { + static let preamble = """ + + + # Configuration File + + `.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 + - `~/.sourcekit-lsp/config.json` + - On macOS: `~/Library/Application Support/org.swift.sourcekit-lsp/config.json` from the various `Library` folders on the system + - If the `XDG_CONFIG_HOME` environment variable is set: `$XDG_CONFIG_HOME/sourcekit-lsp/config.json` + - Initialization options passed in the initialize request + - A `.sourcekit-lsp/config.json` file in a workspace’s root + + The structure of the file is currently not guaranteed to be stable. Options may be removed or renamed. + + ## Structure + + `config.json` is a JSON file with the following structure. All keys are optional and unknown keys are ignored. + + """ + + let context: OptionSchemaContext + + /// Builds a markdown document for the configuration file based on the schema. + func build(from schema: OptionTypeSchama) throws -> String { + var doc = Self.preamble + + func appendProperty(_ property: OptionTypeSchama.Property, indentLevel: Int) throws { + let indent = String(repeating: " ", count: indentLevel) + let name = property.name + doc += "\(indent)- `\(name)" + let type = property.type + let typeDescription: String? + switch type.kind { + case .struct: + // Skip struct type as we describe its properties in the next level + typeDescription = nil + default: + typeDescription = Self.typeToDisplay(type) + } + if let typeDescription { + doc += ": \(typeDescription)`:" + } else { + doc += "`:" + } + if let description = property.description { + doc += " " + description.split(separator: "\n").joined(separator: "\n\(indent) ") + } + doc += "\n" + switch type.kind { + case .struct(let schema): + for property in schema.properties { + try appendProperty(property, indentLevel: indentLevel + 1) + } + case .enum(let schema): + for caseInfo in schema.cases { + // Add detailed description for each case if available + guard let description = caseInfo.description else { + continue + } + doc += "\(indent) - `\(caseInfo.name)`" + doc += ": " + description.split(separator: "\n").joined(separator: "\n\(indent) ") + doc += "\n" + } + default: break + } + } + guard case .struct(let schema) = schema.kind else { + throw ConfigSchemaGenError("Root schema must be a struct") + } + for property in schema.properties { + try appendProperty(property, indentLevel: 0) + } + return doc + } + + static func typeToDisplay(_ type: OptionTypeSchama, shouldWrap: Bool = false) -> String { + switch type.kind { + case .boolean: return "boolean" + case .integer: return "integer" + case .number: return "number" + case .string: return "string" + case .array(let value): + return "\(typeToDisplay(value, shouldWrap: true))[]" + case .dictionary(let value): + return "[string: \(typeToDisplay(value))]" + case .struct(let structInfo): + return structInfo.name + case .enum(let enumInfo): + let cases = enumInfo.cases.map { "\"\($0.name)\"" }.joined(separator: "|") + return shouldWrap ? "(\(cases))" : cases + } + } +} diff --git a/SourceKitLSPDevUtils/Sources/ConfigSchemaGen/OptionSchema.swift b/SourceKitLSPDevUtils/Sources/ConfigSchemaGen/OptionSchema.swift new file mode 100644 index 000000000..5ea485374 --- /dev/null +++ b/SourceKitLSPDevUtils/Sources/ConfigSchemaGen/OptionSchema.swift @@ -0,0 +1,230 @@ +//===----------------------------------------------------------------------===// +// +// 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 SwiftSyntax + +/// Intermediate type schema representation for option types derived from Swift +/// syntax nodes +struct OptionTypeSchama { + struct Property { + var name: String + var type: OptionTypeSchama + var description: String? + var defaultValue: String? + } + + struct Struct { + var name: String + /// Properties of the object, preserving the order of declaration + var properties: [Property] + } + + struct Case { + var name: String + var description: String? + } + + struct Enum { + var name: String + var cases: [Case] + } + + enum Kind { + case boolean + case integer + case number + case string + indirect case array(value: OptionTypeSchama) + indirect case dictionary(value: OptionTypeSchama) + case `struct`(Struct) + case `enum`(Enum) + } + + var kind: Kind + var isOptional: Bool + + init(kind: Kind, isOptional: Bool = false) { + self.kind = kind + self.isOptional = isOptional + } + + /// Accesses the property schema by name + subscript(_ key: String) -> OptionTypeSchama? { + get { + guard case .struct(let structInfo) = kind else { + return nil + } + return structInfo.properties.first { $0.name == key }?.type + } + set { + guard case .struct(var structInfo) = kind else { + fatalError("Cannot set property on non-object type") + } + guard let index = structInfo.properties.firstIndex(where: { $0.name == key }) else { + fatalError("Property not found: \(key)") + } + guard let newValue = newValue else { + fatalError("Cannot set property to nil") + } + structInfo.properties[index].type = newValue + kind = .struct(structInfo) + } + } +} + +/// Context for resolving option schema from Swift syntax nodes +struct OptionSchemaContext { + private let typeNameResolver: TypeDeclResolver + + init(typeNameResolver: TypeDeclResolver) { + self.typeNameResolver = typeNameResolver + } + + /// Builds a schema from a type declaration + func buildSchema(from typeDecl: TypeDeclResolver.TypeDecl) throws -> OptionTypeSchama { + switch DeclSyntax(typeDecl).as(DeclSyntaxEnum.self) { + case .structDecl(let decl): + let structInfo = try buildStructProperties(decl) + return OptionTypeSchama(kind: .struct(structInfo)) + case .enumDecl(let decl): + let enumInfo = try buildEnumCases(decl) + return OptionTypeSchama(kind: .enum(enumInfo)) + default: + throw ConfigSchemaGenError("Unsupported type declaration: \(typeDecl)") + } + } + + /// Resolves the type of a given type usage + private func resolveType(_ type: TypeSyntax) throws -> OptionTypeSchama { + switch type.as(TypeSyntaxEnum.self) { + case .optionalType(let type): + var wrapped = try resolveType(type.wrappedType) + guard !wrapped.isOptional else { + throw ConfigSchemaGenError("Nested optional type is not supported") + } + wrapped.isOptional = true + return wrapped + case .arrayType(let type): + let value = try resolveType(type.element) + return OptionTypeSchama(kind: .array(value: value)) + case .dictionaryType(let type): + guard type.key.trimmedDescription == "String" else { + throw ConfigSchemaGenError("Dictionary key type must be String: \(type.key)") + } + let value = try resolveType(type.value) + return OptionTypeSchama(kind: .dictionary(value: value)) + case .identifierType(let type): + let primitiveTypes: [String: OptionTypeSchama.Kind] = [ + "String": .string, + "Int": .integer, + "Double": .number, + "Bool": .boolean, + ] + if let primitiveType = primitiveTypes[type.trimmedDescription] { + return OptionTypeSchama(kind: primitiveType) + } else if type.name.trimmedDescription == "Set" { + guard let elementType = type.genericArgumentClause?.arguments.first?.argument else { + throw ConfigSchemaGenError("Set type must have one generic argument: \(type)") + } + return OptionTypeSchama(kind: .array(value: try resolveType(elementType))) + } else { + let type = try typeNameResolver.lookupType(for: type) + return try buildSchema(from: type) + } + default: + throw ConfigSchemaGenError("Unsupported type syntax: \(type)") + } + } + + private func buildEnumCases(_ node: EnumDeclSyntax) throws -> OptionTypeSchama.Enum { + let cases = try node.memberBlock.members.flatMap { member -> [OptionTypeSchama.Case] in + guard let caseDecl = member.decl.as(EnumCaseDeclSyntax.self) else { + return [] + } + return try caseDecl.elements.map { + guard $0.parameterClause == nil else { + throw ConfigSchemaGenError("Associated values in enum cases are not supported: \(caseDecl)") + } + let name: String + if let rawValue = $0.rawValue?.value { + if let stringLiteral = rawValue.as(StringLiteralExprSyntax.self), + let literalValue = stringLiteral.representedLiteralValue + { + name = literalValue + } else { + throw ConfigSchemaGenError( + "Only string literals without interpolation are supported as enum case raw values: \(caseDecl)" + ) + } + } else { + name = $0.name.text + } + return OptionTypeSchama.Case(name: name, description: Self.extractDocComment(caseDecl.leadingTrivia)) + } + } + let typeName = node.name.text + return .init(name: typeName, cases: cases) + } + + private func buildStructProperties(_ node: StructDeclSyntax) throws -> OptionTypeSchama.Struct { + var properties: [OptionTypeSchama.Property] = [] + for member in node.memberBlock.members { + // Skip computed properties + guard let variable = member.decl.as(VariableDeclSyntax.self), + let binding = variable.bindings.first, + let type = binding.typeAnnotation, + binding.accessorBlock == nil + else { continue } + + let name = binding.pattern.trimmed.description + let defaultValue = binding.initializer?.value.description + let description = Self.extractDocComment(variable.leadingTrivia) + let typeInfo = try resolveType(type.type) + properties.append( + .init(name: name, type: typeInfo, description: description, defaultValue: defaultValue) + ) + } + let typeName = node.name.text + return .init(name: typeName, properties: properties) + } + + private static func extractDocComment(_ trivia: Trivia) -> String? { + var docLines = trivia.flatMap { piece in + switch piece { + case .docBlockComment(let text): + // Remove `/**` and `*/` + assert(text.hasPrefix("/**") && text.hasSuffix("*/"), "Unexpected doc block comment format: \(text)") + return text.dropFirst(3).dropLast(2).split { $0.isNewline } + case .docLineComment(let text): + // Remove `///` and leading space + assert(text.hasPrefix("///"), "Unexpected doc line comment format: \(text)") + let text = text.dropFirst(3) + return [text] + default: + return [] + } + } + guard !docLines.isEmpty else { + return nil + } + // Trim leading spaces for each line and skip empty lines + docLines = docLines.compactMap { + guard !$0.isEmpty else { return nil } + var trimmed = $0 + while trimmed.first?.isWhitespace == true { + trimmed = trimmed.dropFirst() + } + return trimmed + } + return docLines.joined(separator: " ") + } +} diff --git a/SourceKitLSPDevUtils/Sources/ConfigSchemaGen/TypeDeclResolver.swift b/SourceKitLSPDevUtils/Sources/ConfigSchemaGen/TypeDeclResolver.swift new file mode 100644 index 000000000..8c8bd089e --- /dev/null +++ b/SourceKitLSPDevUtils/Sources/ConfigSchemaGen/TypeDeclResolver.swift @@ -0,0 +1,118 @@ +//===----------------------------------------------------------------------===// +// +// 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 SwiftSyntax + +/// Resolves type declarations from Swift syntax nodes +class TypeDeclResolver { + typealias TypeDecl = NamedDeclSyntax & DeclGroupSyntax & DeclSyntaxProtocol + /// A representation of a qualified name of a type declaration + /// + /// `Outer.Inner` type declaration is represented as ["Outer", "Inner"] + typealias QualifiedName = [String] + private var typeDeclByQualifiedName: [QualifiedName: TypeDecl] = [:] + + enum Error: Swift.Error { + case typeNotFound(QualifiedName) + } + + private class TypeDeclCollector: SyntaxVisitor { + let resolver: TypeDeclResolver + var scope: [TypeDecl] = [] + var rootTypeDecls: [TypeDecl] = [] + + init(resolver: TypeDeclResolver) { + self.resolver = resolver + super.init(viewMode: .all) + } + + func visitNominalDecl(_ node: TypeDecl) -> SyntaxVisitorContinueKind { + let name = node.name.text + let qualifiedName = scope.map(\.name.text) + [name] + resolver.typeDeclByQualifiedName[qualifiedName] = node + scope.append(node) + return .visitChildren + } + + func visitPostNominalDecl() { + let type = scope.removeLast() + if scope.isEmpty { + rootTypeDecls.append(type) + } + } + + override func visit(_ node: StructDeclSyntax) -> SyntaxVisitorContinueKind { + return visitNominalDecl(node) + } + override func visitPost(_ node: StructDeclSyntax) { + visitPostNominalDecl() + } + override func visit(_ node: EnumDeclSyntax) -> SyntaxVisitorContinueKind { + return visitNominalDecl(node) + } + override func visitPost(_ node: EnumDeclSyntax) { + visitPostNominalDecl() + } + } + + /// Collects type declarations from a parsed Swift source file + func collect(from schema: SourceFileSyntax) { + let collector = TypeDeclCollector(resolver: self) + collector.walk(schema) + } + + /// Builds the type name scope for a given type usage + private func buildScope(type: IdentifierTypeSyntax) -> QualifiedName { + var innerToOuter: [String] = [] + var context: SyntaxProtocol = type + while let parent = context.parent { + if let parent = parent.asProtocol(NamedDeclSyntax.self), parent.isProtocol(DeclGroupSyntax.self) { + innerToOuter.append(parent.name.text) + } + context = parent + } + return innerToOuter.reversed() + } + + /// Looks up a qualified name of a type declaration by its unqualified type usage + /// Returns the qualified name hierarchy of the type declaration + /// If the type declaration is not found, returns the unqualified name + private func tryQualify(type: IdentifierTypeSyntax) -> QualifiedName { + let name = type.name.text + let scope = buildScope(type: type) + /// Search for the type declaration from the innermost scope to the outermost scope + for i in (0...scope.count).reversed() { + let qualifiedName = Array(scope[0.. TypeDecl { + let qualifiedName = tryQualify(type: type) + guard let typeDecl = typeDeclByQualifiedName[qualifiedName] else { + throw Error.typeNotFound(qualifiedName) + } + return typeDecl + } + + /// Looks up a type declaration by its fully qualified name + func lookupType(fullyQualified: QualifiedName) throws -> TypeDecl { + guard let typeDecl = typeDeclByQualifiedName[fullyQualified] else { + throw Error.typeNotFound(fullyQualified) + } + return typeDecl + } +} diff --git a/SourceKitLSPDevUtils/Sources/SourceKitLSPDevUtils/Commands/GenerateConfigSchema.swift b/SourceKitLSPDevUtils/Sources/SourceKitLSPDevUtils/Commands/GenerateConfigSchema.swift new file mode 100644 index 000000000..cf20e7724 --- /dev/null +++ b/SourceKitLSPDevUtils/Sources/SourceKitLSPDevUtils/Commands/GenerateConfigSchema.swift @@ -0,0 +1,24 @@ +//===----------------------------------------------------------------------===// +// +// 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 ArgumentParser +import ConfigSchemaGen + +struct GenerateConfigSchema: ParsableCommand { + static let configuration = CommandConfiguration( + abstract: "Generate a JSON schema and documentation for the SourceKit-LSP configuration file" + ) + + func run() throws { + try ConfigSchemaGen.generate() + } +} diff --git a/SourceKitLSPDevUtils/Sources/SourceKitLSPDevUtils/Commands/VerifyConfigSchema.swift b/SourceKitLSPDevUtils/Sources/SourceKitLSPDevUtils/Commands/VerifyConfigSchema.swift new file mode 100644 index 000000000..40a5a8c11 --- /dev/null +++ b/SourceKitLSPDevUtils/Sources/SourceKitLSPDevUtils/Commands/VerifyConfigSchema.swift @@ -0,0 +1,29 @@ +//===----------------------------------------------------------------------===// +// +// 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 ArgumentParser +import ConfigSchemaGen +import Foundation + +struct VerifyConfigSchema: ParsableCommand { + static let configuration = CommandConfiguration( + abstract: + "Verify that the generated JSON schema and documentation for the SourceKit-LSP configuration file are up-to-date" + ) + + func run() throws { + guard try ConfigSchemaGen.verify() else { + throw ExitCode.failure + } + print("All schemas are up-to-date!") + } +} diff --git a/SourceKitLSPDevUtils/Sources/SourceKitLSPDevUtils/SourceKitLSPDevUtils.swift b/SourceKitLSPDevUtils/Sources/SourceKitLSPDevUtils/SourceKitLSPDevUtils.swift new file mode 100644 index 000000000..ddd960085 --- /dev/null +++ b/SourceKitLSPDevUtils/Sources/SourceKitLSPDevUtils/SourceKitLSPDevUtils.swift @@ -0,0 +1,25 @@ +//===----------------------------------------------------------------------===// +// +// 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 ArgumentParser + +@main +struct SourceKitLSPDevUtils: ParsableCommand { + static let configuration = CommandConfiguration( + commandName: "sourcekit-lsp-dev-utils", + abstract: "Utilities for developing SourceKit-LSP", + subcommands: [ + GenerateConfigSchema.self, + VerifyConfigSchema.self, + ] + ) +} diff --git a/Sources/LanguageServerProtocolExtensions/QueueBasedMessageHandler.swift b/Sources/LanguageServerProtocolExtensions/QueueBasedMessageHandler.swift index 28cc7d85a..4be7b9c61 100644 --- a/Sources/LanguageServerProtocolExtensions/QueueBasedMessageHandler.swift +++ b/Sources/LanguageServerProtocolExtensions/QueueBasedMessageHandler.swift @@ -10,15 +10,15 @@ // //===----------------------------------------------------------------------===// -#if compiler(>=6) import Foundation -package import LanguageServerProtocol +import LanguageServerProtocolJSONRPC import SKLogging + +#if compiler(>=6) +package import LanguageServerProtocol package import SwiftExtensions #else -import Foundation import LanguageServerProtocol -import SKLogging import SwiftExtensions #endif @@ -180,6 +180,7 @@ extension QueueBasedMessageHandler { // are currently handling. Ordering is not important here. We thus don't // need to execute it on `messageHandlingQueue`. if let notification = notification as? CancelRequestNotification { + logger.log("Received cancel request notification: \(notification.forLogging)") self.messageHandlingHelper.cancelRequest(id: notification.id) return } diff --git a/Sources/LanguageServerProtocolJSONRPC/JSONRPCConnection.swift b/Sources/LanguageServerProtocolJSONRPC/JSONRPCConnection.swift index 33ca7ee51..4e99cef04 100644 --- a/Sources/LanguageServerProtocolJSONRPC/JSONRPCConnection.swift +++ b/Sources/LanguageServerProtocolJSONRPC/JSONRPCConnection.swift @@ -61,6 +61,12 @@ public final class JSONRPCConnection: Connection { private let sendIO: DispatchIO private let messageRegistry: MessageRegistry + /// If non-nil, all input received by this `JSONRPCConnection` will be written to the file handle + let inputMirrorFile: FileHandle? + + /// If non-nil, all output created by this `JSONRPCConnection` will be written to the file handle + let outputMirrorFile: FileHandle? + enum State { case created, running, closed } @@ -120,9 +126,13 @@ public final class JSONRPCConnection: Connection { name: String, protocol messageRegistry: MessageRegistry, inFD: FileHandle, - outFD: FileHandle + outFD: FileHandle, + inputMirrorFile: FileHandle? = nil, + outputMirrorFile: FileHandle? = nil ) { self.name = name + self.inputMirrorFile = inputMirrorFile + self.outputMirrorFile = outputMirrorFile self.receiveHandler = nil #if os(Linux) || os(Android) // We receive a `SIGPIPE` if we write to a pipe that points to a crashed process. This in particular happens if the @@ -268,13 +278,10 @@ public final class JSONRPCConnection: Connection { /// Start processing `inFD` and send messages to `receiveHandler`. /// /// - parameter receiveHandler: The message handler to invoke for requests received on the `inFD`. - /// - parameter mirrorFile: If non-nil, all input received by this `JSONRPCConnection` will be written to the file - /// handle /// /// - Important: `start` must be called before sending any data over the `JSONRPCConnection`. public func start( receiveHandler: MessageHandler, - mirrorFile: FileHandle? = nil, closeHandler: @escaping @Sendable () async -> Void = {} ) { queue.sync { @@ -303,8 +310,8 @@ public final class JSONRPCConnection: Connection { return } - orLog("Writing data mirror file") { - try mirrorFile?.write(contentsOf: data) + orLog("Writing input mirror file") { + try self.inputMirrorFile?.write(contentsOf: data) } // Parse and handle any messages in `buffer + data`, leaving any remaining unparsed bytes in `buffer`. @@ -538,6 +545,9 @@ public final class JSONRPCConnection: Connection { dispatchPrecondition(condition: .onQueue(queue)) guard readyToSend() else { return } + orLog("Writing output mirror file") { + try outputMirrorFile?.write(contentsOf: dispatchData) + } sendIO.write(offset: 0, data: dispatchData, queue: sendQueue) { [weak self] done, _, errorCode in if errorCode != 0 { logger.fault("IO error sending message \(errorCode)") diff --git a/Sources/SKOptions/SourceKitLSPOptions.swift b/Sources/SKOptions/SourceKitLSPOptions.swift index 124452586..c1e4492d4 100644 --- a/Sources/SKOptions/SourceKitLSPOptions.swift +++ b/Sources/SKOptions/SourceKitLSPOptions.swift @@ -31,38 +31,42 @@ import struct TSCBasic.AbsolutePath /// See `ConfigurationFile.md` for a description of the configuration file's behavior. public struct SourceKitLSPOptions: Sendable, Codable, Equatable { public struct SwiftPMOptions: Sendable, Codable, Equatable { - /// Build 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. public var configuration: BuildConfiguration? /// 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. public var scratchPath: String? - /// Equivalent to SwiftPM's `--swift-sdks-path` option + /// Equivalent to SwiftPM's `--swift-sdks-path` option. public var swiftSDKsDirectory: String? - /// Equivalent to SwiftPM's `--swift-sdk` option + /// Equivalent to SwiftPM's `--swift-sdk` option. public var swiftSDK: String? - /// Equivalent to SwiftPM's `--triple` option + /// Equivalent to SwiftPM's `--triple` option. public var triple: String? - /// Equivalent to SwiftPM's `-Xcc` option + /// Extra arguments passed to the compiler for C files. Equivalent to SwiftPM's `-Xcc` option. public var cCompilerFlags: [String]? - /// Equivalent to SwiftPM's `-Xcxx` option + /// Extra arguments passed to the compiler for C++ files. Equivalent to SwiftPM's `-Xcxx` option. public var cxxCompilerFlags: [String]? - /// Equivalent to SwiftPM's `-Xswiftc` option + /// Extra arguments passed to the compiler for Swift files. Equivalent to SwiftPM's `-Xswiftc` option. public var swiftCompilerFlags: [String]? - /// Equivalent to SwiftPM's `-Xlinker` option + /// Extra arguments passed to the linker. Equivalent to SwiftPM's `-Xlinker` option. public var linkerFlags: [String]? - /// Equivalent to SwiftPM's `--disable-sandbox` option + /// Disables running subprocesses from SwiftPM in a sandbox. Equivalent to SwiftPM's `--disable-sandbox` option. + /// Useful when running `sourcekit-lsp` in a sandbox because nested sandboxes are not supported. public var disableSandbox: Bool? public init( @@ -122,9 +126,13 @@ public struct SourceKitLSPOptions: Sendable, Codable, Equatable { } public struct FallbackBuildSystemOptions: Sendable, Codable, Equatable { + /// Extra arguments passed to the compiler for C files. public var cCompilerFlags: [String]? + /// Extra arguments passed to the compiler for C++ files. public var cxxCompilerFlags: [String]? + /// Extra arguments passed to the compiler for Swift files. public var swiftCompilerFlags: [String]? + /// The SDK to use for fallback arguments. Default is to infer the SDK using `xcrun`. public var sdk: String? public init( @@ -153,10 +161,15 @@ public struct SourceKitLSPOptions: Sendable, Codable, Equatable { } public struct IndexOptions: Sendable, Codable, Equatable { + /// Directory in which a separate compilation stores the index store. By default, inferred from the build system. public var indexStorePath: String? + /// Directory in which the indexstore-db should be stored. By default, inferred from the build system. public var indexDatabasePath: String? + /// Path remappings for remapping index data for local use. public var indexPrefixMap: [String: String]? + /// 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. public var maxCoresPercentageToUseForBackgroundIndexing: Double? + /// Number of seconds to wait for an update index store task to finish before killing it. public var updateIndexStoreTimeout: Int? public var maxCoresPercentageToUseForBackgroundIndexingOrDefault: Double { @@ -202,6 +215,7 @@ public struct SourceKitLSPOptions: Sendable, Codable, Equatable { public var level: String? /// Whether potentially sensitive information should be redacted. + /// Default is `public`, which redacts potentially sensitive information. public var privacyLevel: String? /// Write all input received by SourceKit-LSP on stdin to a file in this directory. @@ -209,30 +223,38 @@ public struct SourceKitLSPOptions: Sendable, Codable, Equatable { /// Useful to record and replay an entire SourceKit-LSP session. public var inputMirrorDirectory: String? + /// Write all data sent from SourceKit-LSP to the client to a file in this directory. + /// + /// Useful to record the raw communication between SourceKit-LSP and the client on a low level. + public var outputMirrorDirectory: String? + public init( level: String? = nil, privacyLevel: String? = nil, - inputMirrorDirectory: String? = nil + inputMirrorDirectory: String? = nil, + outputMirrorDirectory: String? = nil ) { self.level = level self.privacyLevel = privacyLevel self.inputMirrorDirectory = inputMirrorDirectory + self.outputMirrorDirectory = outputMirrorDirectory } static func merging(base: LoggingOptions, override: LoggingOptions?) -> LoggingOptions { return LoggingOptions( level: override?.level ?? base.level, privacyLevel: override?.privacyLevel ?? base.privacyLevel, - inputMirrorDirectory: override?.inputMirrorDirectory ?? base.inputMirrorDirectory + inputMirrorDirectory: override?.inputMirrorDirectory ?? base.inputMirrorDirectory, + outputMirrorDirectory: override?.outputMirrorDirectory ?? base.outputMirrorDirectory ) } } - public enum BackgroundPreparationMode: String { - /// Build a target to prepare it + public enum BackgroundPreparationMode: String, Sendable, Codable, Equatable { + /// Build a target to prepare it. case build - /// Prepare a target without generating object files but do not do lazy type checking. + /// Prepare a target without generating object files but do not do lazy type checking and function body skipping. /// /// This uses SwiftPM's `--experimental-prepare-for-indexing-no-lazy` flag. case noLazy @@ -241,18 +263,21 @@ public struct SourceKitLSPOptions: Sendable, Codable, Equatable { case enabled } + /// Options for SwiftPM workspaces. private var swiftPM: SwiftPMOptions? public var swiftPMOrDefault: SwiftPMOptions { get { swiftPM ?? .init() } set { swiftPM = newValue } } + /// Dictionary with the following keys, defining options for workspaces with a compilation database. private var compilationDatabase: CompilationDatabaseOptions? public var compilationDatabaseOrDefault: CompilationDatabaseOptions { get { compilationDatabase ?? .init() } set { compilationDatabase = newValue } } + /// Dictionary with the following keys, defining options for files that aren't managed by any build system. private var fallbackBuildSystem: FallbackBuildSystemOptions? public var fallbackBuildSystemOrDefault: FallbackBuildSystemOptions { get { fallbackBuildSystem ?? .init() } @@ -266,22 +291,29 @@ public struct SourceKitLSPOptions: Sendable, Codable, Equatable { get { .milliseconds(buildSettingsTimeout ?? 500) } } + /// Extra command line arguments passed to `clangd` when launching it. public var clangdOptions: [String]? + /// Options related to indexing. private var index: IndexOptions? public var indexOrDefault: IndexOptions { get { index ?? .init() } set { index = newValue } } + /// Options related to logging, 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. private var logging: LoggingOptions? public var loggingOrDefault: LoggingOptions { get { logging ?? .init() } set { logging = newValue } } - /// Default workspace type (buildserver|compdb|swiftpm). Overrides workspace type selection logic. + /// Default workspace type. Overrides workspace type selection logic. public var defaultWorkspaceType: WorkspaceType? + /// Directory in which generated interfaces and macro expansions should be stored. public var generatedFilesPath: String? /// Whether background indexing is enabled. @@ -291,13 +323,11 @@ public struct SourceKitLSPOptions: Sendable, Codable, Equatable { return backgroundIndexing ?? true } - public var backgroundPreparationMode: String? + /// Determines how background indexing should prepare a target. + public var backgroundPreparationMode: BackgroundPreparationMode? public var backgroundPreparationModeOrDefault: BackgroundPreparationMode { - if let backgroundPreparationMode, let parsed = BackgroundPreparationMode(rawValue: backgroundPreparationMode) { - return parsed - } - return .enabled + return backgroundPreparationMode ?? .enabled } /// Whether sending a `textDocument/didChange` or `textDocument/didClose` notification for a document should cancel @@ -354,6 +384,7 @@ public struct SourceKitLSPOptions: Sendable, Codable, Equatable { public init( swiftPM: SwiftPMOptions? = .init(), fallbackBuildSystem: FallbackBuildSystemOptions? = .init(), + buildSettingsTimeout: Int? = nil, compilationDatabase: CompilationDatabaseOptions? = .init(), clangdOptions: [String]? = nil, index: IndexOptions? = .init(), @@ -361,7 +392,7 @@ public struct SourceKitLSPOptions: Sendable, Codable, Equatable { defaultWorkspaceType: WorkspaceType? = nil, generatedFilesPath: String? = nil, backgroundIndexing: Bool? = nil, - backgroundPreparationMode: String? = nil, + backgroundPreparationMode: BackgroundPreparationMode? = nil, cancelTextDocumentRequestsOnEditAndClose: Bool? = nil, experimentalFeatures: Set? = nil, swiftPublishDiagnosticsDebounceDuration: Double? = nil, @@ -370,6 +401,7 @@ public struct SourceKitLSPOptions: Sendable, Codable, Equatable { ) { self.swiftPM = swiftPM self.fallbackBuildSystem = fallbackBuildSystem + self.buildSettingsTimeout = buildSettingsTimeout self.compilationDatabase = compilationDatabase self.clangdOptions = clangdOptions self.index = index @@ -422,6 +454,7 @@ public struct SourceKitLSPOptions: Sendable, Codable, Equatable { base: base.fallbackBuildSystemOrDefault, override: override?.fallbackBuildSystem ), + buildSettingsTimeout: override?.buildSettingsTimeout, compilationDatabase: CompilationDatabaseOptions.merging( base: base.compilationDatabaseOrDefault, override: override?.compilationDatabase diff --git a/Sources/SemanticIndex/CheckedIndex.swift b/Sources/SemanticIndex/CheckedIndex.swift index 1ffe2df1c..e699bf17f 100644 --- a/Sources/SemanticIndex/CheckedIndex.swift +++ b/Sources/SemanticIndex/CheckedIndex.swift @@ -60,6 +60,32 @@ package final class CheckedIndex { private var checker: IndexOutOfDateChecker private let index: IndexStoreDB + /// Maps the USR of a symbol to its name and the name of all its containers, from outermost to innermost. + /// + /// It is important that we cache this because we might find a lot of symbols in the same container for eg. workspace + /// symbols (eg. consider many symbols in the same C++ namespace). If we didn't cache this value, then we would need + /// to perform a `primaryDefinitionOrDeclarationOccurrence` lookup for all of these containers, which is expensive. + /// + /// Since we don't expect `CheckedIndex` to be outlive a single request it is acceptable to cache these results + /// without having any invalidation logic (similar to how we don't invalide results cached in + /// `IndexOutOfDateChecker`). + /// + /// ### Examples + /// If we have + /// ```swift + /// struct Foo {} + /// ``` then + /// `containerNamesCache[]` will be `["Foo"]`. + /// + /// If we have + /// ```swift + /// struct Bar { + /// struct Foo {} + /// } + /// ```, then + /// `containerNamesCache[]` will be `["Bar", "Foo"]`. + private var containerNamesCache: [String: [String]] = [:] + fileprivate init(index: IndexStoreDB, checkLevel: IndexCheckLevel) { self.index = index self.checker = IndexOutOfDateChecker(checkLevel: checkLevel) @@ -183,6 +209,84 @@ package final class CheckedIndex { } return result } + + /// The names of all containers the symbol is contained in, from outermost to innermost. + /// + /// ### Examples + /// In the following, the container names of `test` are `["Foo"]`. + /// ```swift + /// struct Foo { + /// func test() {} + /// } + /// ``` + /// + /// In the following, the container names of `test` are `["Bar", "Foo"]`. + /// ```swift + /// struct Bar { + /// struct Foo { + /// func test() {} + /// } + /// } + /// ``` + package func containerNames(of symbol: SymbolOccurrence) -> [String] { + // The container name of accessors is the container of the surrounding variable. + let accessorOf = symbol.relations.filter { $0.roles.contains(.accessorOf) } + if let primaryVariable = accessorOf.sorted().first { + if accessorOf.count > 1 { + logger.fault("Expected an occurrence to an accessor of at most one symbol, not multiple") + } + if let primaryVariable = primaryDefinitionOrDeclarationOccurrence(ofUSR: primaryVariable.symbol.usr) { + return containerNames(of: primaryVariable) + } + } + + let containers = symbol.relations.filter { $0.roles.contains(.childOf) } + if containers.count > 1 { + logger.fault("Expected an occurrence to a child of at most one symbol, not multiple") + } + let container = containers.filter { + switch $0.symbol.kind { + case .module, .namespace, .enum, .struct, .class, .protocol, .extension, .union: + return true + case .unknown, .namespaceAlias, .macro, .typealias, .function, .variable, .field, .enumConstant, + .instanceMethod, .classMethod, .staticMethod, .instanceProperty, .classProperty, .staticProperty, .constructor, + .destructor, .conversionFunction, .parameter, .using, .concept, .commentTag: + return false + } + }.sorted().first + + guard var containerSymbol = container?.symbol else { + return [] + } + if let cached = containerNamesCache[containerSymbol.usr] { + return cached + } + + if containerSymbol.kind == .extension, + let extendedSymbol = self.occurrences(relatedToUSR: containerSymbol.usr, roles: .extendedBy).first?.symbol + { + containerSymbol = extendedSymbol + } + let result: [String] + + // Use `forEachSymbolOccurrence` instead of `primaryDefinitionOrDeclarationOccurrence` to get a symbol occurrence + // for the container because it can be significantly faster: Eg. when searching for a C++ namespace (such as `llvm`), + // it may be declared in many files. Finding the canonical definition means that we would need to scan through all + // of these files. But we expect all all of these declarations to have the same parent container names and we don't + // care about locations here. + var containerDefinition: SymbolOccurrence? + forEachSymbolOccurrence(byUSR: containerSymbol.usr, roles: [.definition, .declaration]) { occurrence in + containerDefinition = occurrence + return false // stop iteration + } + if let containerDefinition { + result = self.containerNames(of: containerDefinition) + [containerSymbol.name] + } else { + result = [containerSymbol.name] + } + containerNamesCache[containerSymbol.usr] = result + return result + } } /// A wrapper around `IndexStoreDB` that allows the retrieval of a `CheckedIndex` with a specified check level or the diff --git a/Sources/SourceKitLSP/Clang/ClangLanguageService.swift b/Sources/SourceKitLSP/Clang/ClangLanguageService.swift index 255bd8f39..3a713c99a 100644 --- a/Sources/SourceKitLSP/Clang/ClangLanguageService.swift +++ b/Sources/SourceKitLSP/Clang/ClangLanguageService.swift @@ -141,7 +141,7 @@ actor ClangLanguageService: LanguageService, MessageHandler { else { return nil } - return ClangBuildSettings(settings, clangPath: clangdPath) + return ClangBuildSettings(settings, clangPath: clangPath) } nonisolated func canHandle(workspace: Workspace) -> Bool { diff --git a/Sources/SourceKitLSP/SourceKitLSPServer.swift b/Sources/SourceKitLSP/SourceKitLSPServer.swift index 323d6fb12..ee95b09de 100644 --- a/Sources/SourceKitLSP/SourceKitLSPServer.swift +++ b/Sources/SourceKitLSP/SourceKitLSPServer.swift @@ -654,7 +654,9 @@ extension SourceKitLSPServer: QueueBasedMessageHandler { ) async { defer { if let request = params as? any TextDocumentRequest { - self.inProgressTextDocumentRequests[request.textDocument.uri, default: []].removeAll { $0.id == id } + textDocumentTrackingQueue.async(priority: .background) { + self.inProgressTextDocumentRequests[request.textDocument.uri, default: []].removeAll { $0.id == id } + } } } @@ -2393,48 +2395,11 @@ private let maxWorkspaceSymbolResults = 4096 package typealias Diagnostic = LanguageServerProtocol.Diagnostic fileprivate extension CheckedIndex { - func containerNames(of symbol: SymbolOccurrence) -> [String] { - // The container name of accessors is the container of the surrounding variable. - let accessorOf = symbol.relations.filter { $0.roles.contains(.accessorOf) } - if let primaryVariable = accessorOf.sorted().first { - if accessorOf.count > 1 { - logger.fault("Expected an occurrence to an accessor of at most one symbol, not multiple") - } - if let primaryVariable = primaryDefinitionOrDeclarationOccurrence(ofUSR: primaryVariable.symbol.usr) { - return containerNames(of: primaryVariable) - } - } - - let containers = symbol.relations.filter { $0.roles.contains(.childOf) } - if containers.count > 1 { - logger.fault("Expected an occurrence to a child of at most one symbol, not multiple") - } - let container = containers.filter { - switch $0.symbol.kind { - case .module, .namespace, .enum, .struct, .class, .protocol, .extension, .union: - return true - case .unknown, .namespaceAlias, .macro, .typealias, .function, .variable, .field, .enumConstant, - .instanceMethod, .classMethod, .staticMethod, .instanceProperty, .classProperty, .staticProperty, .constructor, - .destructor, .conversionFunction, .parameter, .using, .concept, .commentTag: - return false - } - }.sorted().first - - if let container { - if let containerDefinition = primaryDefinitionOrDeclarationOccurrence(ofUSR: container.symbol.usr) { - return self.containerNames(of: containerDefinition) + [container.symbol.name] - } - return [container.symbol.name] - } else { - return [] - } - } - /// Take the name of containers into account to form a fully-qualified name for the given symbol. /// This means that we will form names of nested types and type-qualify methods. func fullyQualifiedName(of symbolOccurrence: SymbolOccurrence) -> String { let symbol = symbolOccurrence.symbol - let containerNames = containerNames(of: symbolOccurrence) + let containerNames = self.containerNames(of: symbolOccurrence) guard let containerName = containerNames.last else { // No containers, so nothing to do. return symbol.name diff --git a/Sources/SourceKitLSP/Swift/SyntaxHighlightingTokenParser.swift b/Sources/SourceKitLSP/Swift/SyntaxHighlightingTokenParser.swift index f1fecf96e..c349e9f6d 100644 --- a/Sources/SourceKitLSP/Swift/SyntaxHighlightingTokenParser.swift +++ b/Sources/SourceKitLSP/Swift/SyntaxHighlightingTokenParser.swift @@ -156,6 +156,10 @@ struct SyntaxHighlightingTokenParser { values.refFunctionPostfixOperator, values.refFunctionInfixOperator: return (.operator, []) + case values.declMacro: + return (.macro, [.declaration]) + case values.refMacro: + return (.macro, []) case values.declVarStatic, values.declVarClass, values.declVarInstance: diff --git a/Sources/sourcekit-lsp/SourceKitLSP.swift b/Sources/sourcekit-lsp/SourceKitLSP.swift index 993d0e171..fc8abf12c 100644 --- a/Sources/sourcekit-lsp/SourceKitLSP.swift +++ b/Sources/sourcekit-lsp/SourceKitLSP.swift @@ -226,6 +226,22 @@ struct SourceKitLSP: AsyncParsableCommand { return options } + /// Create a new file that can be used to use as an input or output mirror file and return a file handle that can be + /// used to write to that file. + private func createMirrorFile(in directory: URL) throws -> FileHandle { + let dateFormatter = ISO8601DateFormatter() + dateFormatter.timeZone = NSTimeZone.local + let date = dateFormatter.string(from: Date()).replacingOccurrences(of: ":", with: "-") + + let inputMirrorURL = directory.appendingPathComponent("\(date).log") + + logger.log("Mirroring input to \(inputMirrorURL)") + try FileManager.default.createDirectory(at: directory, withIntermediateDirectories: true) + FileManager.default.createFile(atPath: try inputMirrorURL.filePath, contents: nil) + + return try FileHandle(forWritingTo: inputMirrorURL) + } + func run() async throws { // Dup stdout and redirect the fd to stderr so that a careless print() // will not break our connection stream. @@ -262,35 +278,33 @@ struct SourceKitLSP: AsyncParsableCommand { ) cleanOldLogFiles(logFileDirectory: logFileDirectoryURL, maxAge: 60 * 60 /* 1h */) + let inputMirror = orLog("Setting up input mirror") { + if let inputMirrorDirectory = globalConfigurationOptions.loggingOrDefault.inputMirrorDirectory { + return try createMirrorFile(in: URL(fileURLWithPath: inputMirrorDirectory)) + } + return nil + } + + let outputMirror = orLog("Setting up output mirror") { + if let outputMirrorDirectory = globalConfigurationOptions.loggingOrDefault.outputMirrorDirectory { + return try createMirrorFile(in: URL(fileURLWithPath: outputMirrorDirectory)) + } + return nil + } + let clientConnection = JSONRPCConnection( name: "client", protocol: MessageRegistry.lspProtocol, inFD: FileHandle.standardInput, - outFD: realStdoutHandle + outFD: realStdoutHandle, + inputMirrorFile: inputMirror, + outputMirrorFile: outputMirror ) // For reasons that are completely oblivious to me, `DispatchIO.write`, which is used to write LSP responses to // stdout fails with error code 5 on Windows unless we call `AbsolutePath(validating:)` on some URL first. _ = try AbsolutePath(validating: Bundle.main.bundlePath) - var inputMirror: FileHandle? = nil - if let inputMirrorDirectory = globalConfigurationOptions.loggingOrDefault.inputMirrorDirectory { - orLog("Setting up input mirror") { - let dateFormatter = ISO8601DateFormatter() - dateFormatter.timeZone = NSTimeZone.local - let date = dateFormatter.string(from: Date()).replacingOccurrences(of: ":", with: "-") - - let inputMirrorDirectory = URL(fileURLWithPath: inputMirrorDirectory) - let inputMirrorURL = inputMirrorDirectory.appendingPathComponent("\(date).log") - - logger.log("Mirroring input to \(inputMirrorURL)") - try FileManager.default.createDirectory(at: inputMirrorDirectory, withIntermediateDirectories: true) - FileManager.default.createFile(atPath: try inputMirrorURL.filePath, contents: nil) - - inputMirror = try FileHandle(forWritingTo: inputMirrorURL) - } - } - let server = SourceKitLSPServer( client: clientConnection, toolchainRegistry: ToolchainRegistry(installPath: Bundle.main.bundleURL), @@ -302,7 +316,6 @@ struct SourceKitLSP: AsyncParsableCommand { ) clientConnection.start( receiveHandler: server, - mirrorFile: inputMirror, closeHandler: { await server.prepareForExit() // Use _Exit to avoid running static destructors due to https://github.com/swiftlang/swift/issues/55112. diff --git a/Tests/SourceKitLSPTests/BackgroundIndexingTests.swift b/Tests/SourceKitLSPTests/BackgroundIndexingTests.swift index a0d3d0827..83a9261e4 100644 --- a/Tests/SourceKitLSPTests/BackgroundIndexingTests.swift +++ b/Tests/SourceKitLSPTests/BackgroundIndexingTests.swift @@ -1063,7 +1063,7 @@ final class BackgroundIndexingTests: XCTestCase { func testCrossModuleFunctionalityEvenIfLowLevelModuleHasErrors() async throws { try await SkipUnless.swiftPMSupportsExperimentalPrepareForIndexing() var options = SourceKitLSPOptions.testDefault() - options.backgroundPreparationMode = SourceKitLSPOptions.BackgroundPreparationMode.enabled.rawValue + options.backgroundPreparationMode = .enabled let project = try await SwiftPMTestProject( files: [ "LibA/LibA.swift": """ @@ -1110,7 +1110,7 @@ final class BackgroundIndexingTests: XCTestCase { func testCrossModuleFunctionalityWithPreparationNoSkipping() async throws { try await SkipUnless.swiftPMSupportsExperimentalPrepareForIndexing() var options = SourceKitLSPOptions.testDefault() - options.backgroundPreparationMode = SourceKitLSPOptions.BackgroundPreparationMode.noLazy.rawValue + options.backgroundPreparationMode = .noLazy let project = try await SwiftPMTestProject( files: [ "LibA/LibA.swift": """ @@ -1395,7 +1395,7 @@ final class BackgroundIndexingTests: XCTestCase { try SkipUnless.longTestsEnabled() var options = SourceKitLSPOptions.testDefault() - options.backgroundPreparationMode = SourceKitLSPOptions.BackgroundPreparationMode.enabled.rawValue + options.backgroundPreparationMode = .enabled options.indexOrDefault.updateIndexStoreTimeout = 1 /* second */ let dateStarted = Date() @@ -1527,7 +1527,7 @@ final class BackgroundIndexingTests: XCTestCase { ) """, options: SourceKitLSPOptions( - backgroundPreparationMode: SourceKitLSPOptions.BackgroundPreparationMode.enabled.rawValue + backgroundPreparationMode: .enabled ), enableBackgroundIndexing: true ) @@ -1591,7 +1591,7 @@ final class BackgroundIndexingTests: XCTestCase { ) """, options: SourceKitLSPOptions( - backgroundPreparationMode: SourceKitLSPOptions.BackgroundPreparationMode.enabled.rawValue + backgroundPreparationMode: .enabled ), enableBackgroundIndexing: true ) diff --git a/Tests/SourceKitLSPTests/WorkspaceSymbolsTests.swift b/Tests/SourceKitLSPTests/WorkspaceSymbolsTests.swift index 9805ddf73..4a9882a03 100644 --- a/Tests/SourceKitLSPTests/WorkspaceSymbolsTests.swift +++ b/Tests/SourceKitLSPTests/WorkspaceSymbolsTests.swift @@ -95,4 +95,32 @@ class WorkspaceSymbolsTests: XCTestCase { ] ) } + + func testContainerNameOfFunctionInExtension() async throws { + let project = try await IndexedSingleSwiftFileTestProject( + """ + struct Foo { + struct Bar {} + } + + extension Foo.Bar { + func 1️⃣barMethod() {} + } + """ + ) + let response = try await project.testClient.send(WorkspaceSymbolsRequest(query: "barMethod")) + XCTAssertEqual( + response, + [ + .symbolInformation( + SymbolInformation( + name: "barMethod()", + kind: .method, + location: Location(uri: project.fileURI, range: Range(project.positions["1️⃣"])), + containerName: "Foo.Bar" + ) + ) + ] + ) + } } diff --git a/config.schema.json b/config.schema.json new file mode 100644 index 000000000..9eabcc039 --- /dev/null +++ b/config.schema.json @@ -0,0 +1,285 @@ +{ + "$comment" : "DO NOT EDIT THIS FILE. This file is generated by ConfigSchemaGen\/ConfigSchemaGen.swift.", + "$schema" : "http:\/\/json-schema.org\/draft-07\/schema#", + "properties" : { + "backgroundIndexing" : { + "description" : "Whether background indexing is enabled.", + "markdownDescription" : "Whether background indexing is enabled.", + "type" : "boolean" + }, + "backgroundPreparationMode" : { + "description" : "Determines how background indexing should prepare a target.", + "enum" : [ + "build", + "noLazy", + "enabled" + ], + "markdownDescription" : "Determines how background indexing should prepare a target.", + "markdownEnumDescriptions" : [ + "Build a target to prepare it.", + "Prepare a target without generating object files but do not do lazy type checking and function body skipping. This uses SwiftPM's `--experimental-prepare-for-indexing-no-lazy` flag.", + "Prepare a target without generating object files." + ], + "type" : "string" + }, + "buildSettingsTimeout" : { + "description" : "Number of milliseconds to wait for build settings from the build system before using fallback build settings.", + "markdownDescription" : "Number of milliseconds to wait for build settings from the build system before using fallback build settings.", + "type" : "integer" + }, + "cancelTextDocumentRequestsOnEditAndClose" : { + "description" : "Whether sending a `textDocument\/didChange` or `textDocument\/didClose` notification for a document should cancel all pending requests for that document.", + "markdownDescription" : "Whether sending a `textDocument\/didChange` or `textDocument\/didClose` notification for a document should cancel all pending requests for that document.", + "type" : "boolean" + }, + "clangdOptions" : { + "description" : "Extra command line arguments passed to `clangd` when launching it.", + "items" : { + "type" : "string" + }, + "markdownDescription" : "Extra command line arguments passed to `clangd` when launching it.", + "type" : "array" + }, + "compilationDatabase" : { + "description" : "Dictionary with the following keys, defining options for workspaces with a compilation database.", + "markdownDescription" : "Dictionary with the following keys, defining options for workspaces with a compilation database.", + "properties" : { + "searchPaths" : { + "description" : "Additional paths to search for a compilation database, relative to a workspace root.", + "items" : { + "type" : "string" + }, + "markdownDescription" : "Additional paths to search for a compilation database, relative to a workspace root.", + "type" : "array" + } + }, + "type" : "object" + }, + "defaultWorkspaceType" : { + "description" : "Default workspace type. Overrides workspace type selection logic.", + "enum" : [ + "buildServer", + "compilationDatabase", + "swiftPM" + ], + "markdownDescription" : "Default workspace type. Overrides workspace type selection logic.", + "type" : "string" + }, + "experimentalFeatures" : { + "description" : "Experimental features that are enabled.", + "items" : { + "enum" : [ + "on-type-formatting" + ], + "type" : "string" + }, + "markdownDescription" : "Experimental features that are enabled.", + "type" : "array" + }, + "fallbackBuildSystem" : { + "description" : "Dictionary with the following keys, defining options for files that aren't managed by any build system.", + "markdownDescription" : "Dictionary with the following keys, defining options for files that aren't managed by any build system.", + "properties" : { + "cCompilerFlags" : { + "description" : "Extra arguments passed to the compiler for C files.", + "items" : { + "type" : "string" + }, + "markdownDescription" : "Extra arguments passed to the compiler for C files.", + "type" : "array" + }, + "cxxCompilerFlags" : { + "description" : "Extra arguments passed to the compiler for C++ files.", + "items" : { + "type" : "string" + }, + "markdownDescription" : "Extra arguments passed to the compiler for C++ files.", + "type" : "array" + }, + "sdk" : { + "description" : "The SDK to use for fallback arguments. Default is to infer the SDK using `xcrun`.", + "markdownDescription" : "The SDK to use for fallback arguments. Default is to infer the SDK using `xcrun`.", + "type" : "string" + }, + "swiftCompilerFlags" : { + "description" : "Extra arguments passed to the compiler for Swift files.", + "items" : { + "type" : "string" + }, + "markdownDescription" : "Extra arguments passed to the compiler for Swift files.", + "type" : "array" + } + }, + "type" : "object" + }, + "generatedFilesPath" : { + "description" : "Directory in which generated interfaces and macro expansions should be stored.", + "markdownDescription" : "Directory in which generated interfaces and macro expansions should be stored.", + "type" : "string" + }, + "index" : { + "description" : "Options related to indexing.", + "markdownDescription" : "Options related to indexing.", + "properties" : { + "indexDatabasePath" : { + "description" : "Directory in which the indexstore-db should be stored. By default, inferred from the build system.", + "markdownDescription" : "Directory in which the indexstore-db should be stored. By default, inferred from the build system.", + "type" : "string" + }, + "indexPrefixMap" : { + "additionalProperties" : { + "type" : "string" + }, + "description" : "Path remappings for remapping index data for local use.", + "markdownDescription" : "Path remappings for remapping index data for local use.", + "type" : "object" + }, + "indexStorePath" : { + "description" : "Directory in which a separate compilation stores the index store. By default, inferred from the build system.", + "markdownDescription" : "Directory in which a separate compilation stores the index store. By default, inferred from the build system.", + "type" : "string" + }, + "maxCoresPercentageToUseForBackgroundIndexing" : { + "description" : "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.", + "markdownDescription" : "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.", + "type" : "number" + }, + "updateIndexStoreTimeout" : { + "description" : "Number of seconds to wait for an update index store task to finish before killing it.", + "markdownDescription" : "Number of seconds to wait for an update index store task to finish before killing it.", + "type" : "integer" + } + }, + "type" : "object" + }, + "logging" : { + "description" : "Options related to logging, 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.", + "markdownDescription" : "Options related to logging, 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.", + "properties" : { + "inputMirrorDirectory" : { + "description" : "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.", + "markdownDescription" : "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.", + "type" : "string" + }, + "level" : { + "description" : "The level from which one onwards log messages should be written.", + "enum" : [ + "debug", + "info", + "default", + "error", + "fault" + ], + "markdownDescription" : "The level from which one onwards log messages should be written.", + "type" : "string" + }, + "outputMirrorDirectory" : { + "description" : "Write all data sent from SourceKit-LSP to the client to a file in this directory. Useful to record the raw communication between SourceKit-LSP and the client on a low level.", + "markdownDescription" : "Write all data sent from SourceKit-LSP to the client to a file in this directory. Useful to record the raw communication between SourceKit-LSP and the client on a low level.", + "type" : "string" + }, + "privacyLevel" : { + "description" : "Whether potentially sensitive information should be redacted. Default is `public`, which redacts potentially sensitive information.", + "enum" : [ + "public", + "private", + "sensitive" + ], + "markdownDescription" : "Whether potentially sensitive information should be redacted. Default is `public`, which redacts potentially sensitive information.", + "type" : "string" + } + }, + "type" : "object" + }, + "sourcekitdRequestTimeout" : { + "description" : "The maximum duration that a sourcekitd request should be allowed to execute before being declared as timed out. In general, editors should cancel requests that they are no longer interested in, but in case editors don't cancel requests, this ensures that a long-running non-cancelled request is not blocking sourcekitd and thus most semantic functionality. In particular, VS Code does not cancel the semantic tokens request, which can cause a long-running AST build that blocks sourcekitd.", + "markdownDescription" : "The maximum duration that a sourcekitd request should be allowed to execute before being declared as timed out. In general, editors should cancel requests that they are no longer interested in, but in case editors don't cancel requests, this ensures that a long-running non-cancelled request is not blocking sourcekitd and thus most semantic functionality. In particular, VS Code does not cancel the semantic tokens request, which can cause a long-running AST build that blocks sourcekitd.", + "type" : "number" + }, + "swiftPM" : { + "description" : "Options for SwiftPM workspaces.", + "markdownDescription" : "Options for SwiftPM workspaces.", + "properties" : { + "cCompilerFlags" : { + "description" : "Extra arguments passed to the compiler for C files. Equivalent to SwiftPM's `-Xcc` option.", + "items" : { + "type" : "string" + }, + "markdownDescription" : "Extra arguments passed to the compiler for C files. Equivalent to SwiftPM's `-Xcc` option.", + "type" : "array" + }, + "configuration" : { + "description" : "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.", + "enum" : [ + "debug", + "release" + ], + "markdownDescription" : "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.", + "type" : "string" + }, + "cxxCompilerFlags" : { + "description" : "Extra arguments passed to the compiler for C++ files. Equivalent to SwiftPM's `-Xcxx` option.", + "items" : { + "type" : "string" + }, + "markdownDescription" : "Extra arguments passed to the compiler for C++ files. Equivalent to SwiftPM's `-Xcxx` option.", + "type" : "array" + }, + "disableSandbox" : { + "description" : "Disables running subprocesses from SwiftPM in a sandbox. Equivalent to SwiftPM's `--disable-sandbox` option. Useful when running `sourcekit-lsp` in a sandbox because nested sandboxes are not supported.", + "markdownDescription" : "Disables running subprocesses from SwiftPM in a sandbox. Equivalent to SwiftPM's `--disable-sandbox` option. Useful when running `sourcekit-lsp` in a sandbox because nested sandboxes are not supported.", + "type" : "boolean" + }, + "linkerFlags" : { + "description" : "Extra arguments passed to the linker. Equivalent to SwiftPM's `-Xlinker` option.", + "items" : { + "type" : "string" + }, + "markdownDescription" : "Extra arguments passed to the linker. Equivalent to SwiftPM's `-Xlinker` option.", + "type" : "array" + }, + "scratchPath" : { + "description" : "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.", + "markdownDescription" : "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.", + "type" : "string" + }, + "swiftCompilerFlags" : { + "description" : "Extra arguments passed to the compiler for Swift files. Equivalent to SwiftPM's `-Xswiftc` option.", + "items" : { + "type" : "string" + }, + "markdownDescription" : "Extra arguments passed to the compiler for Swift files. Equivalent to SwiftPM's `-Xswiftc` option.", + "type" : "array" + }, + "swiftSDK" : { + "description" : "Equivalent to SwiftPM's `--swift-sdk` option.", + "markdownDescription" : "Equivalent to SwiftPM's `--swift-sdk` option.", + "type" : "string" + }, + "swiftSDKsDirectory" : { + "description" : "Equivalent to SwiftPM's `--swift-sdks-path` option.", + "markdownDescription" : "Equivalent to SwiftPM's `--swift-sdks-path` option.", + "type" : "string" + }, + "triple" : { + "description" : "Equivalent to SwiftPM's `--triple` option.", + "markdownDescription" : "Equivalent to SwiftPM's `--triple` option.", + "type" : "string" + } + }, + "type" : "object" + }, + "swiftPublishDiagnosticsDebounceDuration" : { + "description" : "The time that `SwiftLanguageService` should wait after an edit before starting to compute diagnostics and sending a `PublishDiagnosticsNotification`.", + "markdownDescription" : "The time that `SwiftLanguageService` should wait after an edit before starting to compute diagnostics and sending a `PublishDiagnosticsNotification`.", + "type" : "number" + }, + "workDoneProgressDebounceDuration" : { + "description" : "When a task is started that should be displayed to the client as a work done progress, how many milliseconds to wait before actually starting the work done progress. This prevents flickering of the work done progress in the client for short-lived index tasks which end within this duration.", + "markdownDescription" : "When a task is started that should be displayed to the client as a work done progress, how many milliseconds to wait before actually starting the work done progress. This prevents flickering of the work done progress in the client for short-lived index tasks which end within this duration.", + "type" : "number" + } + }, + "title" : "SourceKit-LSP Configuration", + "type" : "object" +} \ No newline at end of file diff --git a/sourcekit-lsp-dev-utils b/sourcekit-lsp-dev-utils new file mode 100755 index 000000000..66c3f39c0 --- /dev/null +++ b/sourcekit-lsp-dev-utils @@ -0,0 +1,3 @@ +#!/usr/bin/env sh + +swift run --package-path "$(dirname $0)/SourceKitLSPDevUtils" sourcekit-lsp-dev-utils "$@"