Skip to content

Commit 12135d0

Browse files
author
Wang Lun
committed
fixed group of module can't navi, interface name strategy
1 parent f47168a commit 12135d0

File tree

5 files changed

+86
-10
lines changed

5 files changed

+86
-10
lines changed

Sources/SourceKitLSP/GeneratedInterfaceDocumentURLData.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,7 @@ package struct GeneratedInterfaceDocumentURLData: Hashable, ReferenceURLData {
6767
self.moduleName = moduleName
6868
self.groupName = groupName
6969
self.sourcekitdDocumentName = sourcekitdDocumentName
70-
self.buildSettingsFrom = primaryFile
70+
self.buildSettingsFrom = primaryFile.buildSettingsFile
7171
}
7272

7373
init(queryItems: [URLQueryItem]) throws {

Sources/SourceKitLSP/ReferenceDocumentURL.swift

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -170,8 +170,4 @@ extension DocumentURI {
170170

171171
package struct ReferenceDocumentURLError: Error, CustomStringConvertible {
172172
package var description: String
173-
174-
init(description: String) {
175-
self.description = description
176-
}
177173
}

Sources/SourceKitLSP/SourceKitLSPServer.swift

Lines changed: 21 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1857,10 +1857,24 @@ extension SourceKitLSPServer {
18571857
languageService: LanguageService
18581858
) async throws -> [Location] {
18591859
// If this symbol is a module then generate a textual interface
1860-
if symbol.kind == .module, let name = symbol.name {
1860+
if symbol.kind == .module {
1861+
// For module symbols, prefer using systemModule information if available
1862+
let moduleName: String
1863+
let groupName: String?
1864+
1865+
if let systemModule = symbol.systemModule {
1866+
moduleName = systemModule.moduleName
1867+
groupName = systemModule.groupName
1868+
} else if let name = symbol.name {
1869+
moduleName = name
1870+
groupName = nil
1871+
} else {
1872+
return []
1873+
}
1874+
18611875
let interfaceLocation = try await self.definitionInInterface(
1862-
moduleName: name,
1863-
groupName: nil,
1876+
moduleName: moduleName,
1877+
groupName: groupName,
18641878
symbolUSR: nil,
18651879
originatorUri: uri,
18661880
languageService: languageService
@@ -2058,9 +2072,12 @@ extension SourceKitLSPServer {
20582072
originatorUri: DocumentURI,
20592073
languageService: LanguageService
20602074
) async throws -> Location {
2075+
// Let openGeneratedInterface handle all the logic, including checking if we're already in the right interface
2076+
let documentForBuildSettings = originatorUri.buildSettingsFile
2077+
20612078
guard
20622079
let interfaceDetails = try await languageService.openGeneratedInterface(
2063-
document: originatorUri,
2080+
document: documentForBuildSettings,
20642081
moduleName: moduleName,
20652082
groupName: groupName,
20662083
symbolUSR: symbolUSR

Sources/SwiftLanguageService/OpenInterface.swift

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,10 +22,16 @@ extension SwiftLanguageService {
2222
groupName: String?,
2323
symbolUSR symbol: String?
2424
) async throws -> GeneratedInterfaceDetails? {
25+
// Include build settings context to distinguish different versions/configurations
26+
let buildSettingsFileHash = "\(abs(document.buildSettingsFile.stringValue.hashValue))"
27+
let sourcekitdDocumentName = [moduleName, groupName, buildSettingsFileHash].compactMap(\.self).joined(
28+
separator: "."
29+
)
30+
2531
let urlData = GeneratedInterfaceDocumentURLData(
2632
moduleName: moduleName,
2733
groupName: groupName,
28-
sourcekitdDocumentName: "\(moduleName)-\(UUID())",
34+
sourcekitdDocumentName: sourcekitdDocumentName,
2935
primaryFile: document
3036
)
3137
let position: Position? =

Tests/SourceKitLSPTests/SwiftInterfaceTests.swift

Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -319,6 +319,63 @@ final class SwiftInterfaceTests: XCTestCase {
319319
)
320320
XCTAssertEqual(diagnostics.fullReport?.items, [])
321321
}
322+
323+
func testFoundationImportNavigation() async throws {
324+
let testClient = try await TestSourceKitLSPClient(
325+
capabilities: ClientCapabilities(experimental: [
326+
GetReferenceDocumentRequest.method: .dictionary(["supported": .bool(true)])
327+
])
328+
)
329+
let uri = DocumentURI(for: .swift)
330+
331+
let positions = testClient.openDocument(
332+
"""
333+
import 1️⃣Foundation
334+
""",
335+
uri: uri,
336+
language: .swift
337+
)
338+
339+
// Test navigation to Foundation module
340+
let foundationDefinition = try await testClient.send(
341+
DefinitionRequest(textDocument: TextDocumentIdentifier(uri), position: positions["1️⃣"])
342+
)
343+
let foundationLocation = try XCTUnwrap(foundationDefinition?.locations?.only)
344+
XCTAssertTrue(foundationLocation.uri.scheme == "sourcekit-lsp")
345+
XCTAssertTrue(foundationLocation.uri.pseudoPath.contains("Foundation.swiftinterface"))
346+
}
347+
348+
func testFoundationSubmoduleNavigation() async throws {
349+
let testClient = try await TestSourceKitLSPClient(
350+
capabilities: ClientCapabilities(experimental: [
351+
GetReferenceDocumentRequest.method: .dictionary(["supported": .bool(true)])
352+
])
353+
)
354+
let uri = DocumentURI(for: .swift)
355+
356+
let positions = testClient.openDocument(
357+
"""
358+
import 1️⃣Foundation.2️⃣NSAffineTransform
359+
""",
360+
uri: uri
361+
)
362+
363+
let foundationDefinition = try await testClient.send(
364+
DefinitionRequest(textDocument: TextDocumentIdentifier(uri), position: positions["1️⃣"])
365+
)
366+
let foundationLocation = try XCTUnwrap(foundationDefinition?.locations?.only)
367+
XCTAssertTrue(foundationLocation.uri.pseudoPath.contains("Foundation.swiftinterface"))
368+
XCTAssertTrue(foundationLocation.uri.scheme == "sourcekit-lsp")
369+
370+
// Test navigation to NSAffineTransform
371+
let transformDefinition = try await testClient.send(
372+
DefinitionRequest(textDocument: TextDocumentIdentifier(uri), position: positions["2️⃣"])
373+
)
374+
let transformLocation = try XCTUnwrap(transformDefinition?.locations?.only)
375+
// Verify we can identify this as a swiftinterface file
376+
XCTAssertTrue(transformLocation.uri.pseudoPath.contains("Foundation.NSAffineTransform.swiftinterface"))
377+
XCTAssertTrue(transformLocation.uri.scheme == "sourcekit-lsp")
378+
}
322379
}
323380

324381
private func assertSystemSwiftInterface(

0 commit comments

Comments
 (0)