Skip to content

Commit 6675d27

Browse files
address review comments
1 parent f6bec67 commit 6675d27

File tree

6 files changed

+67
-49
lines changed

6 files changed

+67
-49
lines changed

Contributor Documentation/LSP Extensions.md

Lines changed: 23 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -171,27 +171,39 @@ interface SKCompletionOptions {
171171
172172
## `textDocument/doccDocumentation`
173173
174-
New request that returns a RenderNode for a symbol at a given location that can then be
175-
rendered in an editor by `swiftlang/swift-docc-render`.
174+
New request that generates documentation for a symbol at a given cursor location.
176175
177-
This request uses Swift DocC's convert service to convert documentation for a Swift symbol
178-
or Markup/Tutorial file. It responds with a string containing a JSON encoded `RenderNode`
179-
or an error if the documentation could not be converted. This error message can be displayed
180-
to the user in the live preview editor.
176+
Primarily designed to support live preview of Swift documentation in editors.
181177
182-
At the moment this request is only available on macOS and Linux. If SourceKit-LSP supports
183-
this request it will add `textDocument/doccDocumentation` to its experimental server
184-
capabilities.
178+
This request looks up the nearest documentable symbol (if any) at a given cursor location within
179+
a text document and returns a `DoccDocumentationResponse`. The response contains a string
180+
representing single JSON encoded DocC RenderNode. This RenderNode can then be rendered in an
181+
editor via https://github.com/swiftlang/swift-docc-render.
182+
183+
The position may be ommitted for documentation within DocC markdown and tutorial files as they
184+
represent a single documentation page. It is only required for generating documentation within
185+
Swift files as they usually contain multiple documentable symbols.
186+
187+
Documentation can fail to be generated for a number of reasons. The most common of which being
188+
that no documentable symbol could be found. In such cases the request will fail with a request
189+
failed LSP error code (-32803) that contains a human-readable error message. This error message can
190+
be displayed within the live preview editor to indicate that something has gone wrong.
191+
192+
At the moment this request is only available on macOS and Linux. SourceKit-LSP will advertise
193+
`textDocument/doccDocumentation` in its experimental server capabilities if it supports it.
194+
195+
- params: `DoccDocumentationParams`
196+
- result: `DoccDocumentationResponse`
185197
186198
```ts
187199
export interface DoccDocumentationParams {
188200
/**
189-
* The document to render documentation for.
201+
* The document to generate documentation for.
190202
*/
191203
textDocument: TextDocumentIdentifier;
192204

193205
/**
194-
* The document location at which to lookup symbol information.
206+
* The cursor position within the document. (optional)
195207
*
196208
* This parameter is only used in Swift files to determine which symbol to render.
197209
* The position is ignored for markdown and tutorial documents.

Sources/LanguageServerProtocol/Requests/DoccDocumentationRequest.swift

Lines changed: 22 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -10,17 +10,32 @@
1010
//
1111
//===----------------------------------------------------------------------===//
1212

13-
/// Request for generating documentation for the symbol at a given location **(LSP Extension)**.
13+
/// Request that generates documentation for a symbol at a given cursor location **(LSP Extension)**.
1414
///
15-
/// This request looks up the symbol (if any) at a given text document location and returns a
16-
/// ``DoccDocumentationResponse`` for that location. This request is primarily designed for editors
17-
/// to support live preview of Swift documentation.
15+
/// Primarily designed to support live preview of Swift documentation in editors.
16+
///
17+
/// This request looks up the nearest documentable symbol (if any) at a given cursor location within
18+
/// a text document and returns a `DoccDocumentationResponse`. The response contains a string
19+
/// representing single JSON encoded DocC RenderNode. This RenderNode can then be rendered in an
20+
/// editor via https://github.com/swiftlang/swift-docc-render.
21+
///
22+
/// The position may be ommitted for documentation within DocC markdown and tutorial files as they
23+
/// represent a single documentation page. It is only required for generating documentation within
24+
/// Swift files as they usually contain multiple documentable symbols.
25+
///
26+
/// Documentation can fail to be generated for a number of reasons. The most common of which being
27+
/// that no documentable symbol could be found. In such cases the request will fail with a request
28+
/// failed LSP error code (-32803) that contains a human-readable error message. This error message can
29+
/// be displayed within the live preview editor to indicate that something has gone wrong.
30+
///
31+
/// At the moment this request is only available on macOS and Linux. SourceKit-LSP will advertise
32+
/// `textDocument/doccDocumentation` in its experimental server capabilities if it supports it.
1833
///
1934
/// - Parameters:
20-
/// - textDocument: The document to render documentation for.
21-
/// - position: The document location at which to lookup symbol information. (optional)
35+
/// - textDocument: The document to generate documentation for.
36+
/// - position: The cursor position within the document. (optional)
2237
///
23-
/// - Returns: A ``DoccDocumentationResponse`` for the given location, which may contain an error
38+
/// - Returns: A `DoccDocumentationResponse` for the given location, which may contain an error
2439
/// message if documentation could not be converted. This error message can be displayed to the user
2540
/// in the live preview editor.
2641
///

Sources/SKTestSupport/TestSourceKitLSPClient.swift

Lines changed: 4 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -237,17 +237,14 @@ package final class TestSourceKitLSPClient: MessageHandler, Sendable {
237237

238238
// MARK: - Sending messages
239239

240-
package func sendWithRawResponse<R: RequestType>(_ request: R) async -> Result<R.Response, ResponseError> {
241-
await withCheckedContinuation { continuation in
240+
/// Send the request to `server` and return the request result.
241+
package func send<R: RequestType>(_ request: R) async throws(ResponseError) -> R.Response {
242+
let response = await withCheckedContinuation { continuation in
242243
self.send(request) { result in
243244
continuation.resume(returning: result)
244245
}
245246
}
246-
}
247-
248-
/// Send the request to `server` and return the request result.
249-
package func send<R: RequestType>(_ request: R) async throws(ResponseError) -> R.Response {
250-
return try await sendWithRawResponse(request).get()
247+
return try response.get()
251248
}
252249

253250
/// Variant of `send` above that allows the response to be discarded if it is a `VoidResponse`.

Sources/SourceKitLSP/Documentation/DocCServer.swift

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -82,7 +82,7 @@ package struct DocCServer {
8282
messageType: ConvertService.convertMessageType,
8383
messageIdentifier: convertRequestIdentifier,
8484
request: request
85-
);
85+
)
8686
guard let responsePayload = response.payload else {
8787
throw .unexpectedlyNilPayload(response.type.rawValue)
8888
}
@@ -136,11 +136,12 @@ package struct DocCServer {
136136
}
137137
// Send the request to the server and decode the response
138138
server.process(encodedMessage) { response in
139-
let decodeMessageResult: Result<DocumentationServer.Message, DocCServerError> = Result {
140-
try JSONDecoder().decode(DocumentationServer.Message.self, from: response)
139+
do {
140+
let decodedMessage = try JSONDecoder().decode(DocumentationServer.Message.self, from: response)
141+
continuation.resume(returning: .success(decodedMessage))
142+
} catch {
143+
continuation.resume(returning: .failure(.decodingFailure(error)))
141144
}
142-
.flatMapError { .failure(.decodingFailure($0)) }
143-
continuation.resume(returning: decodeMessageResult)
144145
}
145146
}
146147
return try result.get()

Sources/SourceKitLSP/Documentation/DocumentationManager.swift

Lines changed: 5 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@ package final actor DocumentationManager {
4545
let snapshot = try sourceKitLSPServer.documentManager.latestSnapshot(documentURI)
4646
let targetId = await workspace.buildSystemManager.canonicalTarget(for: documentURI)
4747
var moduleName: String? = nil
48-
if let targetId = targetId {
48+
if let targetId {
4949
moduleName = await workspace.buildSystemManager.moduleName(for: documentURI, in: targetId)
5050
}
5151

@@ -70,7 +70,7 @@ package final actor DocumentationManager {
7070
at: snapshot.absolutePosition(of: position)
7171
)
7272
else {
73-
throw ResponseError.requestFailed(.noDocumentation)
73+
throw ResponseError.requestFailed(convertError: .noDocumentation)
7474
}
7575
// Retrieve the symbol graph as well as information about the symbol
7676
let symbolPosition = await swiftLanguageService.adjustPositionToStartOfIdentifier(
@@ -96,7 +96,7 @@ package final actor DocumentationManager {
9696
symbolGraphs.append(rawSymbolGraph)
9797
overridingDocumentationComments[symbolUSR] = nearestDocumentableSymbol.documentationComments
9898
default:
99-
throw ResponseError.requestFailed(.noDocumentation)
99+
throw ResponseError.requestFailed(convertError: .noDocumentation)
100100
}
101101
// Send the convert request to SwiftDocC and wait for the response
102102
let convertResponse = try await doccServer.convert(
@@ -124,25 +124,19 @@ package final actor DocumentationManager {
124124
}
125125

126126
package enum ConvertDocumentationError {
127-
case indexNotAvailable
128127
case noDocumentation
129-
case symbolNotFound(String)
130128

131129
public var message: String {
132130
switch self {
133-
case .indexNotAvailable:
134-
return "The index is not availble to complete the request"
135131
case .noDocumentation:
136132
return "No documentation could be rendered for the position in this document"
137-
case .symbolNotFound(let symbolName):
138-
return "Could not find symbol \(symbolName) in the project"
139133
}
140134
}
141135
}
142136

143137
fileprivate extension ResponseError {
144-
static func requestFailed(_ error: ConvertDocumentationError) -> ResponseError {
145-
return ResponseError(code: .requestFailed, message: error.message)
138+
static func requestFailed(convertError: ConvertDocumentationError) -> ResponseError {
139+
return ResponseError.requestFailed(convertError.message)
146140
}
147141
}
148142

Tests/SourceKitLSPTests/DoccDocumentationTests.swift

Lines changed: 7 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -507,14 +507,13 @@ fileprivate func renderDocumentation(
507507
XCTFail("No expected response was given for marker \(marker)", file: file, line: line)
508508
return
509509
}
510-
let actualResponse = await testClient.sendWithRawResponse(
511-
DoccDocumentationRequest(
512-
textDocument: TextDocumentIdentifier(uri),
513-
position: positions[marker]
510+
do {
511+
let response = try await testClient.send(
512+
DoccDocumentationRequest(
513+
textDocument: TextDocumentIdentifier(uri),
514+
position: positions[marker]
515+
)
514516
)
515-
)
516-
switch actualResponse {
517-
case .success(let response):
518517
let renderNodeString = response.renderNode
519518
guard let renderNodeData = renderNodeString.data(using: .utf8),
520519
let renderNode = try? JSONDecoder().decode(RenderNode.self, from: renderNodeData)
@@ -555,7 +554,7 @@ fileprivate func renderDocumentation(
555554
line: line
556555
)
557556
}
558-
case .failure(let error):
557+
} catch {
559558
switch expectedResponse {
560559
case .renderNode:
561560
XCTFail(

0 commit comments

Comments
 (0)