Skip to content

Commit dd7de19

Browse files
author
Wang Lun
committed
fixed second swiftinterface page won't response
* fixed second swiftinterface page won't response * fixed tap the same name of swiftinterface, it would open another swiftinterface with same content
1 parent 4058ac6 commit dd7de19

File tree

4 files changed

+52
-6
lines changed

4 files changed

+52
-6
lines changed

Sources/SourceKitLSP/SourceKitLSPServer.swift

Lines changed: 28 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2065,9 +2065,36 @@ extension SourceKitLSPServer {
20652065
originatorUri: DocumentURI,
20662066
languageService: LanguageService
20672067
) async throws -> Location {
2068+
// Check if we're already in the target interface with the same module/group/symbol
2069+
if let referenceDoc = try? ReferenceDocumentURL(from: originatorUri),
2070+
case .generatedInterface(let interfaceData) = referenceDoc,
2071+
interfaceData.moduleName == moduleName && interfaceData.groupName == groupName {
2072+
2073+
// If we have a specific symbol USR, try to find its position in the current interface
2074+
if let symbolUSR = symbolUSR,
2075+
let swiftLanguageService = languageService as? SwiftLanguageService {
2076+
do {
2077+
let position = try await swiftLanguageService.generatedInterfaceManager.position(
2078+
ofUsr: symbolUSR,
2079+
in: interfaceData
2080+
)
2081+
return Location(uri: originatorUri, range: Range(position))
2082+
} catch {
2083+
// If we can't find the symbol, just return the top of the current interface
2084+
return Location(uri: originatorUri, range: Range(Position(line: 0, utf16index: 0)))
2085+
}
2086+
} else {
2087+
// No specific symbol, just return the current interface location
2088+
return Location(uri: originatorUri, range: Range(Position(line: 0, utf16index: 0)))
2089+
}
2090+
}
2091+
2092+
// If the originator URI is already a generated interface, use its primary file for build settings
2093+
let documentForBuildSettings = originatorUri.primaryFile ?? originatorUri
2094+
20682095
guard
20692096
let interfaceDetails = try await languageService.openGeneratedInterface(
2070-
document: originatorUri,
2097+
document: documentForBuildSettings,
20712098
moduleName: moduleName,
20722099
groupName: groupName,
20732100
symbolUSR: symbolUSR

Sources/SourceKitLSP/Swift/GeneratedInterfaceManager.swift

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -92,7 +92,13 @@ actor GeneratedInterfaceManager {
9292
incrementingRefCount: Bool
9393
) async throws -> OpenGeneratedInterfaceDocumentDetails {
9494
func loadFromCache() -> OpenGeneratedInterfaceDocumentDetails? {
95-
guard let cachedIndex = openInterfaces.firstIndex(where: { $0.url == document }) else {
95+
// Cache by module name and group name, not the full document data
96+
// This allows reuse across different buildSettingsFrom URIs
97+
guard let cachedIndex = openInterfaces.firstIndex(where: {
98+
$0.url.moduleName == document.moduleName &&
99+
$0.url.groupName == document.groupName &&
100+
$0.url.sourcekitdDocumentName == document.sourcekitdDocumentName
101+
}) else {
96102
return nil
97103
}
98104
if incrementingRefCount {
@@ -160,7 +166,11 @@ actor GeneratedInterfaceManager {
160166
}
161167

162168
private func decrementRefCount(for document: GeneratedInterfaceDocumentURLData) {
163-
guard let cachedIndex = openInterfaces.firstIndex(where: { $0.url == document }) else {
169+
guard let cachedIndex = openInterfaces.firstIndex(where: {
170+
$0.url.moduleName == document.moduleName &&
171+
$0.url.groupName == document.groupName &&
172+
$0.url.sourcekitdDocumentName == document.sourcekitdDocumentName
173+
}) else {
164174
logger.fault(
165175
"Generated interface document for \(document.moduleName) is not open anymore. Unbalanced retain and releases?"
166176
)

Sources/SourceKitLSP/Swift/OpenInterface.swift

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,10 +21,18 @@ extension SwiftLanguageService {
2121
groupName: String?,
2222
symbolUSR symbol: String?
2323
) async throws -> GeneratedInterfaceDetails? {
24+
// Generate a deterministic document name based on module name and group name
25+
// This ensures we reuse the same interface for the same module/group combination
26+
let documentName = if let groupName {
27+
"\(moduleName)-\(groupName.replacingOccurrences(of: "/", with: "-"))"
28+
} else {
29+
moduleName
30+
}
31+
2432
let urlData = GeneratedInterfaceDocumentURLData(
2533
moduleName: moduleName,
2634
groupName: groupName,
27-
sourcekitdDocumentName: "\(moduleName)-\(UUID())",
35+
sourcekitdDocumentName: documentName,
2836
primaryFile: document
2937
)
3038
let position: Position? =

Sources/SourceKitLSP/Workspace.swift

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -432,11 +432,12 @@ package final class Workspace: Sendable, BuildServerManagerDelegate {
432432
/// document is returned.
433433
func setDocumentService(for uri: DocumentURI, _ newLanguageService: any LanguageService) -> LanguageService {
434434
return documentService.withLock { service in
435-
if let languageService = service[uri] {
435+
let key = uri.buildSettingsFile
436+
if let languageService = service[key] {
436437
return languageService
437438
}
438439

439-
service[uri] = newLanguageService
440+
service[key] = newLanguageService
440441
return newLanguageService
441442
}
442443
}

0 commit comments

Comments
 (0)