Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ package struct GeneratedInterfaceDocumentURLData: Hashable, ReferenceURLData {
self.moduleName = moduleName
self.groupName = groupName
self.sourcekitdDocumentName = sourcekitdDocumentName
self.buildSettingsFrom = primaryFile
self.buildSettingsFrom = primaryFile.buildSettingsFile
}

init(queryItems: [URLQueryItem]) throws {
Expand Down
4 changes: 0 additions & 4 deletions Sources/SourceKitLSP/ReferenceDocumentURL.swift
Original file line number Diff line number Diff line change
Expand Up @@ -170,8 +170,4 @@ extension DocumentURI {

package struct ReferenceDocumentURLError: Error, CustomStringConvertible {
package var description: String

init(description: String) {
self.description = description
}
}
25 changes: 21 additions & 4 deletions Sources/SourceKitLSP/SourceKitLSPServer.swift
Original file line number Diff line number Diff line change
Expand Up @@ -1857,10 +1857,24 @@ extension SourceKitLSPServer {
languageService: LanguageService
) async throws -> [Location] {
// If this symbol is a module then generate a textual interface
if symbol.kind == .module, let name = symbol.name {
if symbol.kind == .module {
// For module symbols, prefer using systemModule information if available
let moduleName: String
let groupName: String?

if let systemModule = symbol.systemModule {
moduleName = systemModule.moduleName
groupName = systemModule.groupName
} else if let name = symbol.name {
moduleName = name
groupName = nil
} else {
return []
}

let interfaceLocation = try await self.definitionInInterface(
moduleName: name,
groupName: nil,
moduleName: moduleName,
groupName: groupName,
symbolUSR: nil,
originatorUri: uri,
languageService: languageService
Expand Down Expand Up @@ -2058,9 +2072,12 @@ extension SourceKitLSPServer {
originatorUri: DocumentURI,
languageService: LanguageService
) async throws -> Location {
// Let openGeneratedInterface handle all the logic, including checking if we're already in the right interface
let documentForBuildSettings = originatorUri.buildSettingsFile

guard
let interfaceDetails = try await languageService.openGeneratedInterface(
document: originatorUri,
document: documentForBuildSettings,
moduleName: moduleName,
groupName: groupName,
symbolUSR: symbolUSR
Expand Down
15 changes: 13 additions & 2 deletions Sources/SwiftLanguageService/OpenInterface.swift
Original file line number Diff line number Diff line change
Expand Up @@ -22,10 +22,15 @@ extension SwiftLanguageService {
groupName: String?,
symbolUSR symbol: String?
) async throws -> GeneratedInterfaceDetails? {
// Include build settings context to distinguish different versions/configurations
let buildSettingsFileHash = "\(abs(document.buildSettingsFile.stringValue.hashValue))"
let sourcekitdDocumentName = [moduleName, groupName, buildSettingsFileHash].compactMap(\.self)
.joined(separator: ".")

let urlData = GeneratedInterfaceDocumentURLData(
moduleName: moduleName,
groupName: groupName,
sourcekitdDocumentName: "\(moduleName)-\(UUID())",
sourcekitdDocumentName: sourcekitdDocumentName,
primaryFile: document
)
let position: Position? =
Expand All @@ -40,7 +45,13 @@ extension SwiftLanguageService {
if self.capabilityRegistry.clientHasExperimentalCapability(GetReferenceDocumentRequest.method) {
return GeneratedInterfaceDetails(uri: try urlData.uri, position: position)
}
let interfaceFilePath = self.generatedInterfacesPath.appendingPathComponent(urlData.displayName)
let interfaceFilePath = self.generatedInterfacesPath
.appendingPathComponent(buildSettingsFileHash)
.appendingPathComponent(urlData.displayName)
try FileManager.default.createDirectory(
at: interfaceFilePath.deletingLastPathComponent(),
withIntermediateDirectories: true
)
try await generatedInterfaceManager.snapshot(of: urlData).text.write(
to: interfaceFilePath,
atomically: true,
Expand Down
59 changes: 59 additions & 0 deletions Tests/SourceKitLSPTests/SwiftInterfaceTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -319,6 +319,65 @@ final class SwiftInterfaceTests: XCTestCase {
)
XCTAssertEqual(diagnostics.fullReport?.items, [])
}

func testFoundationImportNavigation() async throws {
let testClient = try await TestSourceKitLSPClient(
capabilities: ClientCapabilities(experimental: [
GetReferenceDocumentRequest.method: .dictionary(["supported": .bool(true)])
])
)
let uri = DocumentURI(for: .swift)

let positions = testClient.openDocument(
"""
import 1️⃣Foundation
""",
uri: uri,
language: .swift
)

// Test navigation to Foundation module
let foundationDefinition = try await testClient.send(
DefinitionRequest(textDocument: TextDocumentIdentifier(uri), position: positions["1️⃣"])
)
let foundationLocation = try XCTUnwrap(foundationDefinition?.locations?.only)
XCTAssertEqual(foundationLocation.uri.scheme, "sourcekit-lsp")
assertContains(foundationLocation.uri.pseudoPath, "Foundation.swiftinterface")
}

func testFoundationSubmoduleNavigation() async throws {
try SkipUnless.platformIsDarwin("Non-Darwin platforms don't have Foundation submodules")

let testClient = try await TestSourceKitLSPClient(
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks like Foundation only has submodules on Darwin platforms (macOS, iOS, etc.). Let’s skip this test on Linux and Windows by adding the following at the start of the test.

try SkipUnless.platformIsDarwin("Non-Darwin platforms don't have Foundation submodules")

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for very detailed code every time 😄

capabilities: ClientCapabilities(experimental: [
GetReferenceDocumentRequest.method: .dictionary(["supported": .bool(true)])
])
)
let uri = DocumentURI(for: .swift)

let positions = testClient.openDocument(
"""
import 1️⃣Foundation.2️⃣NSAffineTransform
""",
uri: uri
)

let foundationDefinition = try await testClient.send(
DefinitionRequest(textDocument: TextDocumentIdentifier(uri), position: positions["1️⃣"])
)
let foundationLocation = try XCTUnwrap(foundationDefinition?.locations?.only)
XCTAssertEqual(foundationLocation.uri.scheme, "sourcekit-lsp")
assertContains(foundationLocation.uri.pseudoPath, "Foundation.swiftinterface")

// Test navigation to NSAffineTransform
let transformDefinition = try await testClient.send(
DefinitionRequest(textDocument: TextDocumentIdentifier(uri), position: positions["2️⃣"])
)
let transformLocation = try XCTUnwrap(transformDefinition?.locations?.only)
// Verify we can identify this as a swiftinterface file
XCTAssertEqual(transformLocation.uri.scheme, "sourcekit-lsp")
assertContains(transformLocation.uri.pseudoPath, "Foundation.NSAffineTransform.swiftinterface")
}
}

private func assertSystemSwiftInterface(
Expand Down