|
| 1 | +//===----------------------------------------------------------------------===// |
| 2 | +// |
| 3 | +// This source file is part of the Swift.org open source project |
| 4 | +// |
| 5 | +// Copyright (c) 2014 - 2022 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 LanguageServerProtocol |
| 14 | +import SKLogging |
| 15 | +import SKUtilities |
| 16 | +import SourceKitD |
| 17 | +import SwiftExtensions |
| 18 | + |
| 19 | +/// When information about a generated interface is requested, this opens the generated interface in sourcekitd and |
| 20 | +/// caches the generated interface contents. |
| 21 | +/// |
| 22 | +/// It keeps the generated interface open in sourcekitd until the corresponding reference document is closed in the |
| 23 | +/// editor. Additionally, it also keeps a few recently requested interfaces cached. This way we don't need to recompute |
| 24 | +/// the generated interface contents between the initial generated interface request to find a USR's position in the |
| 25 | +/// interface until the editor actually opens the reference document. |
| 26 | +actor GeneratedInterfaceManager { |
| 27 | + private struct OpenGeneratedInterfaceDocumentDetails { |
| 28 | + let url: GeneratedInterfaceDocumentURLData |
| 29 | + |
| 30 | + /// The contents of the generated interface. |
| 31 | + let snapshot: DocumentSnapshot |
| 32 | + |
| 33 | + /// The number of `GeneratedInterfaceManager` that are actively working with the sourcekitd document. If this value |
| 34 | + /// is 0, the generated interface may be closed in sourcekitd. |
| 35 | + /// |
| 36 | + /// Usually, this value is 1, while the reference document for this generated interface is open in the editor. |
| 37 | + var refCount: Int |
| 38 | + } |
| 39 | + |
| 40 | + private weak var swiftLanguageService: SwiftLanguageService? |
| 41 | + |
| 42 | + /// The number of generated interface documents that are not in editor but should still be cached. |
| 43 | + private let cacheSize = 2 |
| 44 | + |
| 45 | + /// Details about the generated interfaces that are currently open in sourcekitd. |
| 46 | + /// |
| 47 | + /// Conceptually, this is a dictionary with `url` being the key. To prevent excessive memory usage we only keep |
| 48 | + /// `cacheSize` entries with a ref count of 0 in the array. Older entries are at the end of the list, newer entries |
| 49 | + /// at the front. |
| 50 | + private var openInterfaces: [OpenGeneratedInterfaceDocumentDetails] = [] |
| 51 | + |
| 52 | + init(swiftLanguageService: SwiftLanguageService) { |
| 53 | + self.swiftLanguageService = swiftLanguageService |
| 54 | + } |
| 55 | + |
| 56 | + /// If there are more than `cacheSize` entries in `openInterfaces` that have a ref count of 0, close the oldest ones. |
| 57 | + private func purgeCache() { |
| 58 | + var documentsToClose: [String] = [] |
| 59 | + while openInterfaces.count(where: { $0.refCount == 0 }) > cacheSize, |
| 60 | + let indexToPurge = openInterfaces.lastIndex(where: { $0.refCount == 0 }) |
| 61 | + { |
| 62 | + documentsToClose.append(openInterfaces[indexToPurge].url.sourcekitdDocumentName) |
| 63 | + openInterfaces.remove(at: indexToPurge) |
| 64 | + } |
| 65 | + if !documentsToClose.isEmpty, let swiftLanguageService { |
| 66 | + Task { |
| 67 | + let sourcekitd = swiftLanguageService.sourcekitd |
| 68 | + for documentToClose in documentsToClose { |
| 69 | + await orLog("Closing generated interface") { |
| 70 | + _ = try await swiftLanguageService.sendSourcekitdRequest( |
| 71 | + sourcekitd.dictionary([ |
| 72 | + sourcekitd.keys.request: sourcekitd.requests.editorClose, |
| 73 | + sourcekitd.keys.name: documentToClose, |
| 74 | + sourcekitd.keys.cancelBuilds: 0, |
| 75 | + ]), |
| 76 | + fileContents: nil |
| 77 | + ) |
| 78 | + } |
| 79 | + } |
| 80 | + } |
| 81 | + } |
| 82 | + } |
| 83 | + |
| 84 | + /// If we don't have the generated interface for the given `document` open in sourcekitd, open it, otherwise return |
| 85 | + /// its details from the cache. |
| 86 | + /// |
| 87 | + /// If `incrementingRefCount` is `true`, then the document manager will keep the generated interface open in |
| 88 | + /// sourcekitd, independent of the cache size. If `incrementingRefCount` is `true`, then `decrementRefCount` must be |
| 89 | + /// called to allow the document to be closed again. |
| 90 | + private func details( |
| 91 | + for document: GeneratedInterfaceDocumentURLData, |
| 92 | + incrementingRefCount: Bool |
| 93 | + ) async throws -> OpenGeneratedInterfaceDocumentDetails { |
| 94 | + func loadFromCache() -> OpenGeneratedInterfaceDocumentDetails? { |
| 95 | + guard let cachedIndex = openInterfaces.firstIndex(where: { $0.url == document }) else { |
| 96 | + return nil |
| 97 | + } |
| 98 | + if incrementingRefCount { |
| 99 | + openInterfaces[cachedIndex].refCount += 1 |
| 100 | + } |
| 101 | + return openInterfaces[cachedIndex] |
| 102 | + |
| 103 | + } |
| 104 | + if let cached = loadFromCache() { |
| 105 | + return cached |
| 106 | + } |
| 107 | + |
| 108 | + guard let swiftLanguageService else { |
| 109 | + // `SwiftLanguageService` has been destructed. We are tearing down the language server. Nothing left to do. |
| 110 | + throw ResponseError.unknown("Connection to the editor closed") |
| 111 | + } |
| 112 | + |
| 113 | + let sourcekitd = swiftLanguageService.sourcekitd |
| 114 | + |
| 115 | + let keys = sourcekitd.keys |
| 116 | + let skreq = sourcekitd.dictionary([ |
| 117 | + keys.request: sourcekitd.requests.editorOpenInterface, |
| 118 | + keys.moduleName: document.moduleName, |
| 119 | + keys.groupName: document.groupName, |
| 120 | + keys.name: document.sourcekitdDocumentName, |
| 121 | + keys.synthesizedExtension: 1, |
| 122 | + keys.compilerArgs: await swiftLanguageService.buildSettings(for: try document.uri, fallbackAfterTimeout: false)? |
| 123 | + .compilerArgs as [SKDRequestValue]?, |
| 124 | + ]) |
| 125 | + |
| 126 | + let dict = try await swiftLanguageService.sendSourcekitdRequest(skreq, fileContents: nil) |
| 127 | + |
| 128 | + guard let contents: String = dict[keys.sourceText] else { |
| 129 | + throw ResponseError.unknown("sourcekitd response is missing sourceText") |
| 130 | + } |
| 131 | + |
| 132 | + if let cached = loadFromCache() { |
| 133 | + // Another request raced us to create the generated interface. Discard what we computed here and return the cached |
| 134 | + // value. |
| 135 | + await orLog("Closing generated interface created during race") { |
| 136 | + _ = try await swiftLanguageService.sendSourcekitdRequest( |
| 137 | + sourcekitd.dictionary([ |
| 138 | + keys.request: sourcekitd.requests.editorClose, |
| 139 | + keys.name: document.sourcekitdDocumentName, |
| 140 | + keys.cancelBuilds: 0, |
| 141 | + ]), |
| 142 | + fileContents: nil |
| 143 | + ) |
| 144 | + } |
| 145 | + return cached |
| 146 | + } |
| 147 | + |
| 148 | + let details = OpenGeneratedInterfaceDocumentDetails( |
| 149 | + url: document, |
| 150 | + snapshot: DocumentSnapshot( |
| 151 | + uri: try document.uri, |
| 152 | + language: .swift, |
| 153 | + version: 0, |
| 154 | + lineTable: LineTable(contents) |
| 155 | + ), |
| 156 | + refCount: incrementingRefCount ? 1 : 0 |
| 157 | + ) |
| 158 | + openInterfaces.insert(details, at: 0) |
| 159 | + purgeCache() |
| 160 | + return details |
| 161 | + } |
| 162 | + |
| 163 | + private func decrementRefCount(for document: GeneratedInterfaceDocumentURLData) { |
| 164 | + guard let cachedIndex = openInterfaces.firstIndex(where: { $0.url == document }) else { |
| 165 | + logger.fault( |
| 166 | + "Generated interface document for \(document.moduleName) is not open anymore. Unbalanced retain and releases?" |
| 167 | + ) |
| 168 | + return |
| 169 | + } |
| 170 | + if openInterfaces[cachedIndex].refCount == 0 { |
| 171 | + logger.fault( |
| 172 | + "Generated interface document for \(document.moduleName) is already 0. Unbalanced retain and releases?" |
| 173 | + ) |
| 174 | + return |
| 175 | + } |
| 176 | + openInterfaces[cachedIndex].refCount -= 1 |
| 177 | + purgeCache() |
| 178 | + } |
| 179 | + |
| 180 | + func position(ofUsr usr: String, in document: GeneratedInterfaceDocumentURLData) async throws -> Position { |
| 181 | + guard let swiftLanguageService else { |
| 182 | + // `SwiftLanguageService` has been destructed. We are tearing down the language server. Nothing left to do. |
| 183 | + throw ResponseError.unknown("Connection to the editor closed") |
| 184 | + } |
| 185 | + |
| 186 | + let details = try await details(for: document, incrementingRefCount: true) |
| 187 | + defer { |
| 188 | + decrementRefCount(for: document) |
| 189 | + } |
| 190 | + |
| 191 | + let sourcekitd = swiftLanguageService.sourcekitd |
| 192 | + let keys = sourcekitd.keys |
| 193 | + let skreq = sourcekitd.dictionary([ |
| 194 | + keys.request: sourcekitd.requests.editorFindUSR, |
| 195 | + keys.sourceFile: document.sourcekitdDocumentName, |
| 196 | + keys.usr: usr, |
| 197 | + ]) |
| 198 | + |
| 199 | + let dict = try await swiftLanguageService.sendSourcekitdRequest(skreq, fileContents: details.snapshot.text) |
| 200 | + guard let offset: Int = dict[keys.offset] else { |
| 201 | + throw ResponseError.unknown("Missing key 'offset'") |
| 202 | + } |
| 203 | + return details.snapshot.positionOf(utf8Offset: offset) |
| 204 | + } |
| 205 | + |
| 206 | + func snapshot(of document: GeneratedInterfaceDocumentURLData) async throws -> DocumentSnapshot { |
| 207 | + return try await details(for: document, incrementingRefCount: false).snapshot |
| 208 | + } |
| 209 | + |
| 210 | + func open(document: GeneratedInterfaceDocumentURLData) async throws { |
| 211 | + _ = try await details(for: document, incrementingRefCount: true) |
| 212 | + } |
| 213 | + |
| 214 | + func close(document: GeneratedInterfaceDocumentURLData) async { |
| 215 | + decrementRefCount(for: document) |
| 216 | + } |
| 217 | + |
| 218 | + func reopen(interfacesWithBuildSettingsFrom buildSettingsFile: DocumentURI) async { |
| 219 | + for openInterface in openInterfaces { |
| 220 | + guard openInterface.url.buildSettingsFrom == buildSettingsFile else { |
| 221 | + continue |
| 222 | + } |
| 223 | + await orLog("Reopening generated interface") { |
| 224 | + // `MessageHandlingDependencyTracker` ensures that we don't handle a request for the generated interface while |
| 225 | + // it is being re-opened because `documentUpdate` and `documentRequest` use the `buildSettingsFile` to determine |
| 226 | + // their dependencies. |
| 227 | + await close(document: openInterface.url) |
| 228 | + openInterfaces.removeAll(where: { $0.url == openInterface.url }) |
| 229 | + try await open(document: openInterface.url) |
| 230 | + } |
| 231 | + } |
| 232 | + } |
| 233 | +} |
0 commit comments