-
Notifications
You must be signed in to change notification settings - Fork 319
[DocC Live Preview] Cache on-disk snapshots opened in sourcekitd #2226
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,134 @@ | ||
//===----------------------------------------------------------------------===// | ||
// | ||
// This source file is part of the Swift.org open source project | ||
// | ||
// Copyright (c) 2025 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 BuildSystemIntegration | ||
import Foundation | ||
import IndexStoreDB | ||
import LanguageServerProtocol | ||
import SKLogging | ||
import SKUtilities | ||
import SwiftExtensions | ||
|
||
/// A cache of symbol graphs and their associated snapshots opened in sourcekitd. Any opened documents will be | ||
/// closed when the cache is de-initialized. | ||
/// | ||
/// Used by `textDocument/doccDocumentation` requests to retrieve symbol graphs for files that are not currently | ||
/// open in the editor. This allows for retrieving multiple symbol graphs from the same file without having | ||
/// to re-open and parse the syntax tree every time. | ||
actor SymbolGraphCache: Sendable { | ||
private weak var sourceKitLSPServer: SourceKitLSPServer? | ||
private var openSnapshots: [DocumentURI: (snapshot: DocumentSnapshot, patchedCompileCommand: SwiftCompileCommand?)] | ||
|
||
init(sourceKitLSPServer: SourceKitLSPServer) { | ||
self.sourceKitLSPServer = sourceKitLSPServer | ||
self.openSnapshots = [:] | ||
} | ||
|
||
/// Open a unique dummy document in sourcekitd that has the contents of the file on disk for uri, but an arbitrary | ||
/// URI which doesn't exist on disk. Return the symbol graph from sourcekitd. | ||
/// | ||
/// The document will be retained until ``DocCSymbolGraphCache`` is de-initialized. This will avoid parsing the same | ||
/// document multiple times if more than one symbol needs to be looked up. | ||
/// | ||
/// - Parameter symbolLocation: The location of a symbol to find the symbol graph for. | ||
/// - Returns: The symbol graph for this location, if any. | ||
func fetchSymbolGraph(at symbolLocation: SymbolLocation) async throws -> String? { | ||
let swiftLanguageService = try await swiftLanguageService(for: symbolLocation.documentUri) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I think we’re never re-using an open document, are we? I don’t even think that we ever register open documents. |
||
let (snapshot, patchedCompileCommand) = try await swiftLanguageService.openSnapshotFromDiskOpenedInSourcekitd( | ||
uri: symbolLocation.documentUri, | ||
fallbackSettingsAfterTimeout: false | ||
) | ||
return try await swiftLanguageService.cursorInfo( | ||
snapshot, | ||
compileCommand: patchedCompileCommand, | ||
Range(snapshot.position(of: symbolLocation)), | ||
includeSymbolGraph: true | ||
).symbolGraph | ||
} | ||
|
||
private func swiftLanguageService(for uri: DocumentURI) async throws -> SwiftLanguageService { | ||
guard let sourceKitLSPServer else { | ||
throw ResponseError.internalError("SourceKit-LSP is shutting down") | ||
} | ||
guard let workspace = await sourceKitLSPServer.workspaceForDocument(uri: uri), | ||
let languageService = await sourceKitLSPServer.languageService(for: uri, .swift, in: workspace), | ||
let swiftLanguageService = languageService as? SwiftLanguageService | ||
else { | ||
throw ResponseError.internalError("Unable to find SwiftLanguageService for \(uri)") | ||
} | ||
return swiftLanguageService | ||
} | ||
|
||
deinit { | ||
guard let sourceKitLSPServer else { | ||
return | ||
} | ||
|
||
let documentsToClose = openSnapshots.values | ||
Task { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Launching a task here means that the documents could be closed in parallel while another request is handled, which would be very hard to debug if this ends up creating some kind of data races in sourcekitd. I think I’d prefer a pattern where you have something like the following and then explicitly close the documents at end of the cache’s lifetime. func withSymbolGraphCache<T>(_ body: (SymbolGraphCache) async throws -> T) async throws -> T I recently wrote the following for another project, which might be helpful for such a /// Run `body` and always ensure that `cleanup` gets run, independently of whether `body` threw an error or returned a
/// value.
package func run<T>(
_ body: () async throws -> T,
cleanup: () async -> ()
) async throws -> T {
do {
let result = try await body()
await cleanup()
return result
} catch {
await cleanup()
throw error
}
} |
||
for (snapshot, _) in documentsToClose { | ||
guard let workspace = await sourceKitLSPServer.workspaceForDocument(uri: snapshot.uri), | ||
let languageService = await sourceKitLSPServer.languageService(for: snapshot.uri, .swift, in: workspace), | ||
let swiftLanguageService = languageService as? SwiftLanguageService | ||
else { | ||
logger.log("Unable to find SwiftLanguageService to close helper document \(snapshot.uri.forLogging)") | ||
return | ||
} | ||
await swiftLanguageService.closeSnapshotFromDiskOpenedInSourcekitd(snapshot: snapshot) | ||
} | ||
} | ||
} | ||
} | ||
|
||
fileprivate extension SwiftLanguageService { | ||
func openSnapshotFromDiskOpenedInSourcekitd( | ||
uri: DocumentURI, | ||
fallbackSettingsAfterTimeout: Bool, | ||
) async throws -> (snapshot: DocumentSnapshot, patchedCompileCommand: SwiftCompileCommand?) { | ||
guard let fileURL = uri.fileURL else { | ||
throw ResponseError.unknown("Cannot create snapshot with on-disk contents for non-file URI \(uri.forLogging)") | ||
} | ||
let snapshot = DocumentSnapshot( | ||
uri: try DocumentURI(filePath: "\(UUID().uuidString)/\(fileURL.filePath)", isDirectory: false), | ||
language: .swift, | ||
version: 0, | ||
lineTable: LineTable(try String(contentsOf: fileURL, encoding: .utf8)) | ||
) | ||
let patchedCompileCommand: SwiftCompileCommand? = | ||
if let buildSettings = await self.buildSettings( | ||
for: uri, | ||
fallbackAfterTimeout: fallbackSettingsAfterTimeout | ||
) { | ||
SwiftCompileCommand(buildSettings.patching(newFile: snapshot.uri, originalFile: uri)) | ||
} else { | ||
nil | ||
} | ||
|
||
_ = try await send( | ||
sourcekitdRequest: \.editorOpen, | ||
self.openDocumentSourcekitdRequest(snapshot: snapshot, compileCommand: patchedCompileCommand), | ||
snapshot: snapshot | ||
) | ||
|
||
return (snapshot, patchedCompileCommand) | ||
} | ||
|
||
func closeSnapshotFromDiskOpenedInSourcekitd(snapshot: DocumentSnapshot) async { | ||
await orLog("Close helper document '\(snapshot.uri)'") { | ||
_ = try await send( | ||
sourcekitdRequest: \.editorClose, | ||
self.closeDocumentSourcekitdRequest(uri: snapshot.uri), | ||
snapshot: snapshot | ||
) | ||
} | ||
} | ||
} |
This file was deleted.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Would something like
OnDiskDocumentManager
be a more appropriate name because this type neither really caches values (it manages document lifetimes), nor does it really cache symbol graphs but it keeps track of sourcekitd documents.