1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403
|
//===----------------------------------------------------------------------===//
//
// 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 LSPLogging
import LanguageServerProtocol
import SKSupport
import struct TSCBasic.AbsolutePath
/// Options that can be used to modify SourceKit-LSP's behavior.
///
/// See `ConfigurationFile.md` for a description of the configuration file's behavior.
public struct SourceKitLSPOptions: Sendable, Codable {
public struct SwiftPMOptions: Sendable, Codable {
/// Build configuration (debug|release).
///
/// Equivalent to SwiftPM's `--configuration` option.
public var configuration: BuildConfiguration?
/// Build artifacts directory path. If nil, the build system may choose a default value.
///
/// Equivalent to SwiftPM's `--scratch-path` option.
public var scratchPath: String?
/// Equivalent to SwiftPM's `--swift-sdks-path` option
public var swiftSDKsDirectory: String?
/// Equivalent to SwiftPM's `--swift-sdk` option
public var swiftSDK: String?
/// Equivalent to SwiftPM's `--triple` option
public var triple: String?
/// Equivalent to SwiftPM's `-Xcc` option
public var cCompilerFlags: [String]?
/// Equivalent to SwiftPM's `-Xcxx` option
public var cxxCompilerFlags: [String]?
/// Equivalent to SwiftPM's `-Xswiftc` option
public var swiftCompilerFlags: [String]?
/// Equivalent to SwiftPM's `-Xlinker` option
public var linkerFlags: [String]?
public init(
configuration: BuildConfiguration? = nil,
scratchPath: String? = nil,
swiftSDKsDirectory: String? = nil,
swiftSDK: String? = nil,
triple: String? = nil,
cCompilerFlags: [String]? = nil,
cxxCompilerFlags: [String]? = nil,
swiftCompilerFlags: [String]? = nil,
linkerFlags: [String]? = nil
) {
self.configuration = configuration
self.scratchPath = scratchPath
self.swiftSDKsDirectory = swiftSDKsDirectory
self.swiftSDK = swiftSDK
self.triple = triple
self.cCompilerFlags = cCompilerFlags
self.cxxCompilerFlags = cxxCompilerFlags
self.swiftCompilerFlags = swiftCompilerFlags
self.linkerFlags = linkerFlags
}
static func merging(base: SwiftPMOptions, override: SwiftPMOptions?) -> SwiftPMOptions {
return SwiftPMOptions(
configuration: override?.configuration ?? base.configuration,
scratchPath: override?.scratchPath ?? base.scratchPath,
swiftSDKsDirectory: override?.swiftSDKsDirectory ?? base.swiftSDKsDirectory,
swiftSDK: override?.swiftSDK ?? base.swiftSDK,
triple: override?.triple ?? base.triple,
cCompilerFlags: override?.cCompilerFlags ?? base.cCompilerFlags,
cxxCompilerFlags: override?.cxxCompilerFlags ?? base.cxxCompilerFlags,
swiftCompilerFlags: override?.swiftCompilerFlags ?? base.swiftCompilerFlags,
linkerFlags: override?.linkerFlags ?? base.linkerFlags
)
}
}
public struct CompilationDatabaseOptions: Sendable, Codable {
/// Additional paths to search for a compilation database, relative to a workspace root.
public var searchPaths: [String]?
public init(searchPaths: [String]? = nil) {
self.searchPaths = searchPaths
}
static func merging(
base: CompilationDatabaseOptions,
override: CompilationDatabaseOptions?
) -> CompilationDatabaseOptions {
return CompilationDatabaseOptions(searchPaths: override?.searchPaths ?? base.searchPaths)
}
}
public struct FallbackBuildSystemOptions: Sendable, Codable {
public var cCompilerFlags: [String]?
public var cxxCompilerFlags: [String]?
public var swiftCompilerFlags: [String]?
public init(
cCompilerFlags: [String]? = nil,
cxxCompilerFlags: [String]? = nil,
swiftCompilerFlags: [String]? = nil
) {
self.cCompilerFlags = cCompilerFlags
self.cxxCompilerFlags = cxxCompilerFlags
self.swiftCompilerFlags = swiftCompilerFlags
}
static func merging(
base: FallbackBuildSystemOptions,
override: FallbackBuildSystemOptions?
) -> FallbackBuildSystemOptions {
return FallbackBuildSystemOptions(
cCompilerFlags: override?.cCompilerFlags ?? base.cCompilerFlags,
cxxCompilerFlags: override?.cxxCompilerFlags ?? base.cxxCompilerFlags,
swiftCompilerFlags: override?.swiftCompilerFlags ?? base.swiftCompilerFlags
)
}
}
public struct IndexOptions: Sendable, Codable {
public var indexStorePath: String?
public var indexDatabasePath: String?
public var indexPrefixMap: [String: String]?
public var maxCoresPercentageToUseForBackgroundIndexing: Double?
public var updateIndexStoreTimeout: Int?
public var maxCoresPercentageToUseForBackgroundIndexingOrDefault: Double {
return maxCoresPercentageToUseForBackgroundIndexing ?? 1
}
public var updateIndexStoreTimeoutOrDefault: Duration {
if let updateIndexStoreTimeout {
.seconds(updateIndexStoreTimeout)
} else {
.seconds(120)
}
}
public init(
indexStorePath: String? = nil,
indexDatabasePath: String? = nil,
indexPrefixMap: [String: String]? = nil,
maxCoresPercentageToUseForBackgroundIndexing: Double? = nil,
updateIndexStoreTimeout: Int? = nil
) {
self.indexStorePath = indexStorePath
self.indexDatabasePath = indexDatabasePath
self.indexPrefixMap = indexPrefixMap
self.maxCoresPercentageToUseForBackgroundIndexing = maxCoresPercentageToUseForBackgroundIndexing
self.updateIndexStoreTimeout = updateIndexStoreTimeout
}
static func merging(base: IndexOptions, override: IndexOptions?) -> IndexOptions {
return IndexOptions(
indexStorePath: override?.indexStorePath ?? base.indexStorePath,
indexDatabasePath: override?.indexDatabasePath ?? base.indexDatabasePath,
indexPrefixMap: override?.indexPrefixMap ?? base.indexPrefixMap,
maxCoresPercentageToUseForBackgroundIndexing: override?.maxCoresPercentageToUseForBackgroundIndexing
?? base.maxCoresPercentageToUseForBackgroundIndexing,
updateIndexStoreTimeout: override?.updateIndexStoreTimeout ?? base.updateIndexStoreTimeout
)
}
}
public enum BackgroundPreparationMode: String {
/// Build a target to prepare it
case build
/// Prepare a target without generating object files but do not do lazy type checking.
///
/// This uses SwiftPM's `--experimental-prepare-for-indexing-no-lazy` flag.
case noLazy
/// Prepare a target without generating object files.
case enabled
}
public var swiftPM: SwiftPMOptions
public var compilationDatabase: CompilationDatabaseOptions
public var fallbackBuildSystem: FallbackBuildSystemOptions
public var clangdOptions: [String]?
public var index: IndexOptions
/// Default workspace type (buildserver|compdb|swiftpm). Overrides workspace type selection logic.
public var defaultWorkspaceType: WorkspaceType?
public var generatedFilesPath: String?
/// Whether background indexing is enabled.
public var backgroundIndexing: Bool?
public var backgroundIndexingOrDefault: Bool {
return backgroundIndexing ?? false
}
public var backgroundPreparationMode: String?
public var backgroundPreparationModeOrDefault: BackgroundPreparationMode {
if let backgroundPreparationMode, let parsed = BackgroundPreparationMode(rawValue: backgroundPreparationMode) {
return parsed
}
return .build
}
/// Experimental features that are enabled.
public var experimentalFeatures: Set<ExperimentalFeature>? = nil
/// The time that `SwiftLanguageService` should wait after an edit before starting to compute diagnostics and
/// sending a `PublishDiagnosticsNotification`.
///
/// This is mostly intended for testing purposes so we don't need to wait the debouncing time to get a diagnostics
/// notification when running unit tests.
public var swiftPublishDiagnosticsDebounceDuration: Double? = nil
public var swiftPublishDiagnosticsDebounceDurationOrDefault: Duration {
if let swiftPublishDiagnosticsDebounceDuration {
return .seconds(swiftPublishDiagnosticsDebounceDuration)
}
return .seconds(2)
}
/// 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.
public var workDoneProgressDebounceDuration: Double? = nil
public var workDoneProgressDebounceDurationOrDefault: Duration {
if let workDoneProgressDebounceDuration {
return .seconds(workDoneProgressDebounceDuration)
}
return .seconds(1)
}
/// 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.
public var sourcekitdRequestTimeout: Double? = nil
public var sourcekitdRequestTimeoutOrDefault: Duration {
if let sourcekitdRequestTimeout {
return .seconds(sourcekitdRequestTimeout)
}
return .seconds(120)
}
public init(
swiftPM: SwiftPMOptions = .init(),
fallbackBuildSystem: FallbackBuildSystemOptions = .init(),
compilationDatabase: CompilationDatabaseOptions = .init(),
clangdOptions: [String]? = nil,
index: IndexOptions = .init(),
defaultWorkspaceType: WorkspaceType? = nil,
generatedFilesPath: String? = nil,
backgroundIndexing: Bool? = nil,
backgroundPreparationMode: String? = nil,
experimentalFeatures: Set<ExperimentalFeature>? = nil,
swiftPublishDiagnosticsDebounceDuration: Double? = nil,
workDoneProgressDebounceDuration: Double? = nil,
sourcekitdRequestTimeout: Double? = nil
) {
self.swiftPM = swiftPM
self.fallbackBuildSystem = fallbackBuildSystem
self.compilationDatabase = compilationDatabase
self.clangdOptions = clangdOptions
self.index = index
self.generatedFilesPath = generatedFilesPath
self.defaultWorkspaceType = defaultWorkspaceType
self.backgroundIndexing = backgroundIndexing
self.backgroundPreparationMode = backgroundPreparationMode
self.experimentalFeatures = experimentalFeatures
self.swiftPublishDiagnosticsDebounceDuration = swiftPublishDiagnosticsDebounceDuration
self.workDoneProgressDebounceDuration = workDoneProgressDebounceDuration
self.sourcekitdRequestTimeout = sourcekitdRequestTimeout
}
public init?(fromLSPAny lspAny: LSPAny?) throws {
guard let lspAny else {
return nil
}
let jsonEncoded = try JSONEncoder().encode(lspAny)
self = try JSONDecoder().decode(Self.self, from: jsonEncoded)
}
public var asLSPAny: LSPAny {
get throws {
let jsonEncoded = try JSONEncoder().encode(self)
return try JSONDecoder().decode(LSPAny.self, from: jsonEncoded)
}
}
public init?(path: URL?) {
guard let path, let contents = try? String(contentsOf: path, encoding: .utf8) else {
return nil
}
guard
let decoded = orLog(
"Parsing config.json",
{ try JSONDecoder().decode(Self.self, from: contents) }
)
else {
return nil
}
self = decoded
}
public static func merging(base: SourceKitLSPOptions, override: SourceKitLSPOptions?) -> SourceKitLSPOptions {
return SourceKitLSPOptions(
swiftPM: SwiftPMOptions.merging(base: base.swiftPM, override: override?.swiftPM),
fallbackBuildSystem: FallbackBuildSystemOptions.merging(
base: base.fallbackBuildSystem,
override: override?.fallbackBuildSystem
),
compilationDatabase: CompilationDatabaseOptions.merging(
base: base.compilationDatabase,
override: override?.compilationDatabase
),
clangdOptions: override?.clangdOptions ?? base.clangdOptions,
index: IndexOptions.merging(base: base.index, override: override?.index),
defaultWorkspaceType: override?.defaultWorkspaceType ?? base.defaultWorkspaceType,
generatedFilesPath: override?.generatedFilesPath ?? base.generatedFilesPath,
backgroundIndexing: override?.backgroundIndexing ?? base.backgroundIndexing,
backgroundPreparationMode: override?.backgroundPreparationMode ?? base.backgroundPreparationMode,
experimentalFeatures: override?.experimentalFeatures ?? base.experimentalFeatures,
swiftPublishDiagnosticsDebounceDuration: override?.swiftPublishDiagnosticsDebounceDuration
?? base.swiftPublishDiagnosticsDebounceDuration,
workDoneProgressDebounceDuration: override?.workDoneProgressDebounceDuration
?? base.workDoneProgressDebounceDuration,
sourcekitdRequestTimeout: override?.sourcekitdRequestTimeout ?? base.sourcekitdRequestTimeout
)
}
public var generatedFilesAbsolutePath: AbsolutePath {
if let absolutePath = AbsolutePath(validatingOrNil: generatedFilesPath) {
return absolutePath
}
return defaultDirectoryForGeneratedFiles
}
public func hasExperimentalFeature(_ feature: ExperimentalFeature) -> Bool {
guard let experimentalFeatures else {
return false
}
return experimentalFeatures.contains(feature)
}
public init(from decoder: any Decoder) throws {
let container = try decoder.container(keyedBy: CodingKeys.self)
self.swiftPM = try container.decodeIfPresent(SwiftPMOptions.self, forKey: CodingKeys.swiftPM) ?? .init()
self.compilationDatabase =
try container.decodeIfPresent(CompilationDatabaseOptions.self, forKey: CodingKeys.compilationDatabase) ?? .init()
self.fallbackBuildSystem =
try container.decodeIfPresent(FallbackBuildSystemOptions.self, forKey: CodingKeys.fallbackBuildSystem) ?? .init()
self.clangdOptions = try container.decodeIfPresent([String].self, forKey: CodingKeys.clangdOptions)
self.index = try container.decodeIfPresent(IndexOptions.self, forKey: CodingKeys.index) ?? .init()
self.defaultWorkspaceType = try container.decodeIfPresent(
WorkspaceType.self,
forKey: CodingKeys.defaultWorkspaceType
)
self.generatedFilesPath = try container.decodeIfPresent(String.self, forKey: CodingKeys.generatedFilesPath)
self.backgroundIndexing = try container.decodeIfPresent(Bool.self, forKey: CodingKeys.backgroundIndexing)
self.backgroundPreparationMode = try container.decodeIfPresent(
String.self,
forKey: CodingKeys.backgroundPreparationMode
)
self.experimentalFeatures = try container.decodeIfPresent(
Set<ExperimentalFeature>.self,
forKey: CodingKeys.experimentalFeatures
)
self.swiftPublishDiagnosticsDebounceDuration = try container.decodeIfPresent(
Double.self,
forKey: CodingKeys.swiftPublishDiagnosticsDebounceDuration
)
self.workDoneProgressDebounceDuration = try container.decodeIfPresent(
Double.self,
forKey: CodingKeys.workDoneProgressDebounceDuration
)
self.sourcekitdRequestTimeout = try container.decodeIfPresent(
Double.self,
forKey: CodingKeys.sourcekitdRequestTimeout
)
}
}
|