|
| 1 | +//===----------------------------------------------------------------------===// |
| 2 | +// |
| 3 | +// This source file is part of the Swift.org open source project |
| 4 | +// |
| 5 | +// Copyright (c) 2025 Apple Inc. and the Swift project authors |
| 6 | +// Licensed under Apache License v2.0 with Runtime Library Exception |
| 7 | +// |
| 8 | +// See https://swift.org/LICENSE.txt for license information |
| 9 | +// See https://swift.org/CONTRIBUTORS.txt for the list of Swift project authors |
| 10 | +// |
| 11 | +//===----------------------------------------------------------------------===// |
| 12 | + |
| 13 | +import BuildServerIntegration |
| 14 | +import Foundation |
| 15 | +import IndexStoreDB |
| 16 | +import LanguageServerProtocol |
| 17 | +import SKLogging |
| 18 | +import SKUtilities |
| 19 | +import SwiftExtensions |
| 20 | + |
| 21 | +/// A cache of symbol graphs and their associated snapshots opened in sourcekitd. Any opened documents will be |
| 22 | +/// closed when the cache is de-initialized. |
| 23 | +/// |
| 24 | +/// Used by `textDocument/doccDocumentation` requests to retrieve symbol graphs for files that are not currently |
| 25 | +/// open in the editor. This allows for retrieving multiple symbol graphs from the same file without having |
| 26 | +/// to re-open and parse the syntax tree every time. |
| 27 | +actor SymbolGraphCache: Sendable { |
| 28 | + private weak var sourceKitLSPServer: SourceKitLSPServer? |
| 29 | + private var openSnapshots: [DocumentURI: (snapshot: DocumentSnapshot, patchedCompileCommand: SwiftCompileCommand?)] |
| 30 | + |
| 31 | + init(sourceKitLSPServer: SourceKitLSPServer) { |
| 32 | + self.sourceKitLSPServer = sourceKitLSPServer |
| 33 | + self.openSnapshots = [:] |
| 34 | + } |
| 35 | + |
| 36 | + /// Open a unique dummy document in sourcekitd that has the contents of the file on disk for uri, but an arbitrary |
| 37 | + /// URI which doesn't exist on disk. Return the symbol graph from sourcekitd. |
| 38 | + /// |
| 39 | + /// The document will be retained until ``DocCSymbolGraphCache`` is de-initialized. This will avoid parsing the same |
| 40 | + /// document multiple times if more than one symbol needs to be looked up. |
| 41 | + /// |
| 42 | + /// - Parameter symbolLocation: The location of a symbol to find the symbol graph for. |
| 43 | + /// - Returns: The symbol graph for this location, if any. |
| 44 | + func fetchSymbolGraph(at symbolLocation: SymbolLocation) async throws -> String? { |
| 45 | + let swiftLanguageService = try await swiftLanguageService(for: symbolLocation.documentUri) |
| 46 | + let (snapshot, patchedCompileCommand) = try await swiftLanguageService.openSnapshotFromDiskOpenedInSourcekitd( |
| 47 | + uri: symbolLocation.documentUri, |
| 48 | + fallbackSettingsAfterTimeout: false |
| 49 | + ) |
| 50 | + return try await swiftLanguageService.cursorInfo( |
| 51 | + snapshot, |
| 52 | + compileCommand: patchedCompileCommand, |
| 53 | + Range(snapshot.position(of: symbolLocation)), |
| 54 | + includeSymbolGraph: true |
| 55 | + ).symbolGraph |
| 56 | + } |
| 57 | + |
| 58 | + private func swiftLanguageService(for uri: DocumentURI) async throws -> SwiftLanguageService { |
| 59 | + guard let sourceKitLSPServer else { |
| 60 | + throw ResponseError.internalError("SourceKit-LSP is shutting down") |
| 61 | + } |
| 62 | + guard let workspace = await sourceKitLSPServer.workspaceForDocument(uri: uri), |
| 63 | + let languageService = await sourceKitLSPServer.languageService(for: uri, .swift, in: workspace), |
| 64 | + let swiftLanguageService = languageService as? SwiftLanguageService |
| 65 | + else { |
| 66 | + throw ResponseError.internalError("Unable to find SwiftLanguageService for \(uri)") |
| 67 | + } |
| 68 | + return swiftLanguageService |
| 69 | + } |
| 70 | + |
| 71 | + deinit { |
| 72 | + guard let sourceKitLSPServer else { |
| 73 | + return |
| 74 | + } |
| 75 | + |
| 76 | + let documentsToClose = openSnapshots.values |
| 77 | + Task { |
| 78 | + for (snapshot, _) in documentsToClose { |
| 79 | + guard let workspace = await sourceKitLSPServer.workspaceForDocument(uri: snapshot.uri), |
| 80 | + let languageService = await sourceKitLSPServer.languageService(for: snapshot.uri, .swift, in: workspace), |
| 81 | + let swiftLanguageService = languageService as? SwiftLanguageService |
| 82 | + else { |
| 83 | + logger.log("Unable to find SwiftLanguageService to close helper document \(snapshot.uri.forLogging)") |
| 84 | + return |
| 85 | + } |
| 86 | + await swiftLanguageService.closeSnapshotFromDiskOpenedInSourcekitd(snapshot: snapshot) |
| 87 | + } |
| 88 | + } |
| 89 | + } |
| 90 | +} |
| 91 | + |
| 92 | +fileprivate extension SwiftLanguageService { |
| 93 | + func openSnapshotFromDiskOpenedInSourcekitd( |
| 94 | + uri: DocumentURI, |
| 95 | + fallbackSettingsAfterTimeout: Bool, |
| 96 | + ) async throws -> (snapshot: DocumentSnapshot, patchedCompileCommand: SwiftCompileCommand?) { |
| 97 | + guard let fileURL = uri.fileURL else { |
| 98 | + throw ResponseError.unknown("Cannot create snapshot with on-disk contents for non-file URI \(uri.forLogging)") |
| 99 | + } |
| 100 | + let snapshot = DocumentSnapshot( |
| 101 | + uri: try DocumentURI(filePath: "\(UUID().uuidString)/\(fileURL.filePath)", isDirectory: false), |
| 102 | + language: .swift, |
| 103 | + version: 0, |
| 104 | + lineTable: LineTable(try String(contentsOf: fileURL, encoding: .utf8)) |
| 105 | + ) |
| 106 | + let patchedCompileCommand: SwiftCompileCommand? = |
| 107 | + if let buildSettings = await self.buildSettings( |
| 108 | + for: uri, |
| 109 | + fallbackAfterTimeout: fallbackSettingsAfterTimeout |
| 110 | + ) { |
| 111 | + SwiftCompileCommand(buildSettings.patching(newFile: snapshot.uri, originalFile: uri)) |
| 112 | + } else { |
| 113 | + nil |
| 114 | + } |
| 115 | + |
| 116 | + _ = try await send( |
| 117 | + sourcekitdRequest: \.editorOpen, |
| 118 | + self.openDocumentSourcekitdRequest(snapshot: snapshot, compileCommand: patchedCompileCommand), |
| 119 | + snapshot: snapshot |
| 120 | + ) |
| 121 | + |
| 122 | + return (snapshot, patchedCompileCommand) |
| 123 | + } |
| 124 | + |
| 125 | + func closeSnapshotFromDiskOpenedInSourcekitd(snapshot: DocumentSnapshot) async { |
| 126 | + await orLog("Close helper document '\(snapshot.uri)'") { |
| 127 | + _ = try await send( |
| 128 | + sourcekitdRequest: \.editorClose, |
| 129 | + self.closeDocumentSourcekitdRequest(uri: snapshot.uri), |
| 130 | + snapshot: snapshot |
| 131 | + ) |
| 132 | + } |
| 133 | + } |
| 134 | +} |
0 commit comments