Skip to content

Commit 6657623

Browse files
committed
Register openInterface with registerToolchainTextDocumentRequest
and change `InterfaceDetails` to be optional.
1 parent d4d0d19 commit 6657623

File tree

3 files changed

+20
-30
lines changed

3 files changed

+20
-30
lines changed

Sources/LanguageServerProtocol/Requests/OpenInterfaceRequest.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@
1414
/// **(LSP Extension)**
1515
public struct OpenInterfaceRequest: TextDocumentRequest, Hashable {
1616
public static let method: String = "textDocument/openInterface"
17-
public typealias Response = InterfaceDetails
17+
public typealias Response = InterfaceDetails?
1818

1919
/// The document whose compiler arguments should be used to generate the interface.
2020
public var textDocument: TextDocumentIdentifier

Sources/SourceKitLSP/SourceKitServer.swift

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -184,6 +184,7 @@ public final class SourceKitServer: LanguageServer {
184184
registerToolchainTextDocumentRequest(SourceKitServer.completion,
185185
CompletionList(isIncomplete: false, items: []))
186186
registerToolchainTextDocumentRequest(SourceKitServer.hover, nil)
187+
registerToolchainTextDocumentRequest(SourceKitServer.openInterface, nil)
187188
registerToolchainTextDocumentRequest(SourceKitServer.declaration, .locations([]))
188189
registerToolchainTextDocumentRequest(SourceKitServer.definition, .locations([]))
189190
registerToolchainTextDocumentRequest(SourceKitServer.references, [])
@@ -985,6 +986,14 @@ extension SourceKitServer {
985986
) {
986987
languageService.hover(req)
987988
}
989+
990+
func openInterface(
991+
_ req: Request<OpenInterfaceRequest>,
992+
workspace: Workspace,
993+
languageService: ToolchainLanguageServer
994+
) {
995+
languageService.openInterface(req)
996+
}
988997

989998
/// Find all symbols in the workspace that include a string in their name.
990999
/// - returns: An array of SymbolOccurrences that match the string.
@@ -1279,9 +1288,11 @@ extension SourceKitServer {
12791288
let request = Request(openInterface, id: req.id, clientID: ObjectIdentifier(self),
12801289
cancellation: req.cancellationToken, reply: { (result: Result<OpenInterfaceRequest.Response, ResponseError>) in
12811290
switch result {
1282-
case .success(let interfaceDetails):
1291+
case .success(let interfaceDetails?):
12831292
let loc = Location(uri: interfaceDetails.uri, range: Range(Position(line: 0, utf16index: 0)))
12841293
req.reply(.locations([loc]))
1294+
case .success(nil):
1295+
req.reply(.failure(.unknown("Could not generate Swift Interface for \(name)")))
12851296
case .failure(let error):
12861297
req.reply(.failure(error))
12871298
}

Tests/SourceKitLSPTests/SwiftInterfaceTests.swift

Lines changed: 7 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -87,41 +87,20 @@ final class SwiftInterfaceTests: XCTestCase {
8787
try ws.buildAndIndex()
8888
let importedModule = ws.testLoc("lib:import")
8989
try ws.openDocument(importedModule.url, language: .swift)
90-
let workspace = try XCTUnwrap(ws.testServer.server?.queue.sync {
91-
try XCTUnwrap(ws.testServer.server?.workspaceForDocument(uri: importedModule.docUri))
92-
})
93-
let swiftLangServer = try XCTUnwrap(ws.testServer.server?._languageService(for: importedModule.docUri, .swift, in: workspace))
94-
let expectation = expectation(description: "open interface request")
9590
let openInterface = OpenInterfaceRequest(textDocument: importedModule.docIdentifier, name: "lib")
96-
let request = Request(openInterface, id: .number(1), clientID: ObjectIdentifier(swiftLangServer),
97-
cancellation: CancellationToken(), reply: { (result: Result<OpenInterfaceRequest.Response, ResponseError>) in
98-
do {
99-
let interfaceDetails = try result.get()
100-
XCTAssertTrue(interfaceDetails.uri.pseudoPath.hasSuffix("/lib.swiftinterface"))
101-
let fileContents = try XCTUnwrap(interfaceDetails.uri.fileURL.flatMap({ try String(contentsOf: $0, encoding: .utf8) }))
102-
XCTAssertTrue(fileContents.contains("""
91+
let interfaceDetails = try XCTUnwrap(ws.sk.sendSync(openInterface))
92+
XCTAssertTrue(interfaceDetails.uri.pseudoPath.hasSuffix("/lib.swiftinterface"))
93+
let fileContents = try XCTUnwrap(interfaceDetails.uri.fileURL.flatMap({ try String(contentsOf: $0, encoding: .utf8) }))
94+
XCTAssertTrue(fileContents.contains("""
10395
public struct Lib {
104-
96+
10597
public func foo()
106-
98+
10799
public init()
108100
}
109101
"""))
110-
} catch {
111-
XCTFail(error.localizedDescription)
112-
}
113-
expectation.fulfill()
114-
})
115-
116-
// Send an arbitrary request through the front door first or SourceKitServer won't be properly initialized.
117-
_ = try ws.sk.sendSync(HoverRequest(
118-
textDocument: importedModule.docIdentifier,
119-
position: importedModule.position))
120-
swiftLangServer.openInterface(request)
121-
122-
waitForExpectations(timeout: 15)
123102
}
124-
103+
125104
func testSwiftInterfaceAcrossModules() throws {
126105
guard let ws = try staticSourceKitSwiftPMWorkspace(name: "SwiftPMPackage") else { return }
127106
try ws.buildAndIndex()

0 commit comments

Comments
 (0)