Skip to content

Commit 053f286

Browse files
committed
Handle custom PeekMacroRequest to show macro expansions in a peeked editor
1 parent d7f92c6 commit 053f286

File tree

2 files changed

+53
-0
lines changed

2 files changed

+53
-0
lines changed

src/sourcekit-lsp/LanguageClientManager.ts

Lines changed: 32 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 { PeekMacroParams, PeekMacroRequest } from "./lspExtensions";
3031

3132
interface SourceKitLogMessageParams extends langclient.LogMessageParams {
3233
logName?: string;
@@ -616,6 +617,37 @@ export class LanguageClientManager {
616617
this.languageClient = client;
617618
this.cancellationToken = new vscode.CancellationTokenSource();
618619

620+
this.languageClient.onRequest(PeekMacroRequest.method, async (params: PeekMacroParams) => {
621+
const locations = params.macroExpansion.expansionURIs.map(uri => {
622+
const location = new vscode.Location(
623+
vscode.Uri.from({
624+
scheme: "file",
625+
path: new URL(uri).pathname,
626+
}),
627+
new vscode.Position(0, 0)
628+
);
629+
630+
return location;
631+
});
632+
633+
console.log(params.peekLocation);
634+
635+
const peekPosition = new vscode.Position(
636+
params.peekLocation.line,
637+
params.peekLocation.character
638+
);
639+
640+
await vscode.commands.executeCommand(
641+
"editor.action.peekLocations",
642+
vscode.window.activeTextEditor?.document.uri,
643+
peekPosition,
644+
locations,
645+
"peek"
646+
);
647+
648+
return { success: true };
649+
});
650+
619651
return this.clientReadyPromise;
620652
}
621653

src/sourcekit-lsp/lspExtensions.ts

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

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

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

21+
export interface PeekMacroParams {
22+
macroExpansion: MacroExpansion;
23+
peekLocation: Position;
24+
}
25+
26+
interface MacroExpansion {
27+
expansionURIs: Uri[];
28+
}
29+
30+
export interface PeekMacroResult {
31+
success: boolean;
32+
failureReason?: string;
33+
}
34+
35+
export const PeekMacroRequest = new langclient.RequestType<
36+
PeekMacroParams,
37+
PeekMacroResult,
38+
unknown
39+
>("sourcekit-lsp/peekMacro");
40+
2041
// Inlay Hints (pre Swift 5.6)
2142
export interface LegacyInlayHintsParams {
2243
/**

0 commit comments

Comments
 (0)