Skip to content

Commit 75c5edc

Browse files
Rename DocC SourceKit-LSP request to textDocument/doccDocumentation (#1318)
1 parent 6f426e1 commit 75c5edc

File tree

5 files changed

+103
-134
lines changed

5 files changed

+103
-134
lines changed

src/WorkspaceContext.ts

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@ import { SwiftBuildStatus } from "./ui/SwiftBuildStatus";
3434
import { SwiftToolchain } from "./toolchain/toolchain";
3535
import { DiagnosticsManager } from "./DiagnosticsManager";
3636
import { DocumentationManager } from "./documentation/DocumentationManager";
37+
import { DocCDocumentationRequest, ReIndexProjectRequest } from "./sourcekit-lsp/extensions";
3738

3839
/**
3940
* Context for whole workspace. Holds array of contexts for each workspace folder
@@ -239,9 +240,9 @@ export class WorkspaceContext implements vscode.Disposable {
239240
return;
240241
}
241242
contextKeys.supportsReindexing =
242-
experimentalCaps["workspace/triggerReindex"] !== undefined;
243+
experimentalCaps[ReIndexProjectRequest.method] !== undefined;
243244
contextKeys.supportsDocumentationLivePreview =
244-
experimentalCaps["textDocument/convertDocumentation"] !== undefined;
245+
experimentalCaps[DocCDocumentationRequest.method] !== undefined;
245246
});
246247

247248
setSnippetContextKey(this);

src/documentation/DocumentationPreviewEditor.ts

Lines changed: 44 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,8 @@ import * as fs from "fs/promises";
1717
import * as path from "path";
1818
import { RenderNode, WebviewContent, WebviewMessage } from "./webview/WebviewMessage";
1919
import { WorkspaceContext } from "../WorkspaceContext";
20-
import { ConvertDocumentationRequest } from "../sourcekit-lsp/extensions/ConvertDocumentationRequest";
20+
import { DocCDocumentationRequest, DocCDocumentationResponse } from "../sourcekit-lsp/extensions";
21+
import { LSPErrorCodes, ResponseError } from "vscode-languageclient";
2122

2223
export enum PreviewEditorConstant {
2324
VIEW_TYPE = "swift.previewDocumentationEditor",
@@ -185,33 +186,56 @@ export class DocumentationPreviewEditor implements vscode.Disposable {
185186
return;
186187
}
187188

188-
const response = await this.context.languageClientManager.useLanguageClient(
189-
async client => {
190-
return await client.sendRequest(ConvertDocumentationRequest.type, {
191-
textDocument: {
192-
uri: document.uri.toString(),
193-
},
194-
position: textEditor.selection.start,
195-
});
189+
try {
190+
const response = await this.context.languageClientManager.useLanguageClient(
191+
async (client): Promise<DocCDocumentationResponse> => {
192+
return await client.sendRequest(DocCDocumentationRequest.type, {
193+
textDocument: {
194+
uri: document.uri.toString(),
195+
},
196+
position: textEditor.selection.start,
197+
});
198+
}
199+
);
200+
this.postMessage({
201+
type: "update-content",
202+
content: {
203+
type: "render-node",
204+
renderNode: this.parseRenderNode(response.renderNode),
205+
},
206+
});
207+
} catch (error) {
208+
// Update the preview editor to reflect what error occurred
209+
let livePreviewErrorMessage = "An internal error occurred";
210+
const baseLogErrorMessage = `SourceKit-LSP request "${DocCDocumentationRequest.method}" failed: `;
211+
if (error instanceof ResponseError) {
212+
if (error.code === LSPErrorCodes.RequestCancelled) {
213+
// We can safely ignore cancellations
214+
return undefined;
215+
}
216+
switch (error.code) {
217+
case LSPErrorCodes.RequestFailed:
218+
// RequestFailed response errors can be shown to the user
219+
livePreviewErrorMessage = error.message;
220+
break;
221+
default:
222+
// We should log additional info for other response errors
223+
this.context.outputChannel.log(
224+
baseLogErrorMessage + JSON.stringify(error.toJson(), undefined, 2)
225+
);
226+
break;
227+
}
228+
} else {
229+
this.context.outputChannel.log(baseLogErrorMessage + `${error}`);
196230
}
197-
);
198-
if (response.type === "error") {
199231
this.postMessage({
200232
type: "update-content",
201233
content: {
202234
type: "error",
203-
errorMessage: response.error.message,
235+
errorMessage: livePreviewErrorMessage,
204236
},
205237
});
206-
return;
207238
}
208-
this.postMessage({
209-
type: "update-content",
210-
content: {
211-
type: "render-node",
212-
renderNode: this.parseRenderNode(response.renderNode),
213-
},
214-
});
215239
}
216240

217241
private parseRenderNode(content: string): RenderNode {

src/sourcekit-lsp/extensions/ConvertDocumentationRequest.ts

Lines changed: 0 additions & 112 deletions
This file was deleted.
Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
//===----------------------------------------------------------------------===//
2+
//
3+
// This source file is part of the VS Code Swift open source project
4+
//
5+
// Copyright (c) 2021-2024 the VS Code Swift project authors
6+
// Licensed under Apache License v2.0
7+
//
8+
// See LICENSE.txt for license information
9+
// See CONTRIBUTORS.txt for the list of VS Code Swift project authors
10+
//
11+
// SPDX-License-Identifier: Apache-2.0
12+
//
13+
//===----------------------------------------------------------------------===//
14+
15+
// We use namespaces to store request information just like vscode-languageclient
16+
/* eslint-disable @typescript-eslint/no-namespace */
17+
18+
import {
19+
TextDocumentIdentifier,
20+
Position,
21+
MessageDirection,
22+
RequestType,
23+
} from "vscode-languageclient";
24+
25+
/** Parameters used to make a {@link DocCDocumentationRequest}. */
26+
export interface DocCDocumentationParams {
27+
/**
28+
* The document to render documentation for.
29+
*/
30+
textDocument: TextDocumentIdentifier;
31+
32+
/**
33+
* The document location at which to lookup symbol information.
34+
*
35+
* This parameter is only used in Swift files to determine which symbol to render.
36+
* The position is ignored for markdown and tutorial documents.
37+
*/
38+
position: Position;
39+
}
40+
41+
/**
42+
* The response from a {@link DocCDocumentationRequest} containing a single RenderNode
43+
* that can be displayed in an editor via `swiftlang/swift-docc-render`
44+
*/
45+
export interface DocCDocumentationResponse {
46+
renderNode: string;
47+
}
48+
49+
export namespace DocCDocumentationRequest {
50+
export const method = "textDocument/doccDocumentation" as const;
51+
export const messageDirection: MessageDirection = MessageDirection.clientToServer;
52+
export const type = new RequestType<DocCDocumentationParams, DocCDocumentationResponse, never>(
53+
method
54+
);
55+
}

src/sourcekit-lsp/extensions/index.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414

1515
// Definitions for non-standard requests used by sourcekit-lsp
1616

17+
export * from "./DocCDocumentationRequest";
1718
export * from "./GetReferenceDocumentRequest";
1819
export * from "./GetTestsRequest";
1920
export * from "./LegacyInlayHintRequest";

0 commit comments

Comments
 (0)