Skip to content

Commit 4007fd7

Browse files
committed
Handle custom PeekDocumentsRequest to show macro expansions in a peeked editor
1 parent 862f8d5 commit 4007fd7

File tree

2 files changed

+73
-0
lines changed

2 files changed

+73
-0
lines changed

src/sourcekit-lsp/LanguageClientManager.ts

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ import { DiagnosticsManager } from "../DiagnosticsManager";
2727
import { LSPLogger, LSPOutputChannel } from "./LSPOutputChannel";
2828
import { SwiftOutputChannel } from "../ui/SwiftOutputChannel";
2929
import { promptForDiagnostics } from "../commands/captureDiagnostics";
30+
import { PeekDocumentsParams, PeekDocumentsRequest } from "./lspExtensions";
3031

3132
interface SourceKitLogMessageParams extends langclient.LogMessageParams {
3233
logName?: string;
@@ -559,6 +560,9 @@ export class LanguageClientManager {
559560
})(),
560561
},
561562
errorHandler: new SourceKitLSPErrorHandler(5),
563+
initializationOptions: {
564+
"workspace/peekDocuments": true, // workaround for client capability to handle `PeekDocumentsRequest`
565+
},
562566
};
563567

564568
return new langclient.LanguageClient(
@@ -616,6 +620,36 @@ export class LanguageClientManager {
616620
this.languageClient = client;
617621
this.cancellationToken = new vscode.CancellationTokenSource();
618622

623+
this.languageClient.onRequest(
624+
PeekDocumentsRequest.method,
625+
async (params: PeekDocumentsParams) => {
626+
const locations = params.locations.map(uri => {
627+
const location = new vscode.Location(
628+
vscode.Uri.from({
629+
scheme: "file",
630+
path: new URL(uri).pathname,
631+
}),
632+
new vscode.Position(0, 0)
633+
);
634+
635+
return location;
636+
});
637+
638+
await vscode.commands.executeCommand(
639+
"editor.action.peekLocations",
640+
vscode.Uri.from({
641+
scheme: "file",
642+
path: new URL(params.uri).pathname,
643+
}),
644+
new vscode.Position(params.position.line, params.position.character),
645+
locations,
646+
"peek"
647+
);
648+
649+
return { success: true };
650+
}
651+
);
652+
619653
return this.clientReadyPromise;
620654
}
621655

src/sourcekit-lsp/lspExtensions.ts

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,9 +14,48 @@
1414

1515
import * as ls from "vscode-languageserver-protocol";
1616
import * as langclient from "vscode-languageclient/node";
17+
import * as vscode from "vscode";
1718

1819
// Definitions for non-standard requests used by sourcekit-lsp
1920

21+
// Peek Documents
22+
export interface PeekDocumentsParams {
23+
/**
24+
* The `DocumentUri` of the text document in which to show the "peeked" editor
25+
*/
26+
uri: langclient.DocumentUri;
27+
28+
/**
29+
* The `Position` in the given text document in which to show the "peeked editor"
30+
*/
31+
position: vscode.Position;
32+
33+
/**
34+
* An array `DocumentUri` of the documents to appear inside the "peeked" editor
35+
*/
36+
locations: langclient.DocumentUri[];
37+
}
38+
39+
/**
40+
* Response to indicate the `success` of the `PeekDocumentsRequest`
41+
*/
42+
export interface PeekDocumentsResult {
43+
success: boolean;
44+
}
45+
46+
/**
47+
* Request from the server to the client to show the given documents in a "peeked" editor.
48+
*
49+
* This request is handled by the client to show the given documents in a "peeked" editor (i.e. inline with / inside the editor canvas).
50+
*
51+
* It requires the experimental client capability `"workspace/peekDocuments"` to use.
52+
*/
53+
export const PeekDocumentsRequest = new langclient.RequestType<
54+
PeekDocumentsParams,
55+
PeekDocumentsResult,
56+
unknown
57+
>("workspace/peekDocuments");
58+
2059
// Inlay Hints (pre Swift 5.6)
2160
export interface LegacyInlayHintsParams {
2261
/**

0 commit comments

Comments
 (0)