Skip to content

Commit d856f0c

Browse files
committed
Retrieve macro expansions for PeekDocumentsRequest through GetReferenceDocumentRequest instead of showing temporary files
1 parent 058e1a6 commit d856f0c

File tree

4 files changed

+75
-4
lines changed

4 files changed

+75
-4
lines changed

src/sourcekit-lsp/LanguageClientManager.ts

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@ import { DiagnosticsManager } from "../DiagnosticsManager";
2828
import { LSPLogger, LSPOutputChannel } from "./LSPOutputChannel";
2929
import { SwiftOutputChannel } from "../ui/SwiftOutputChannel";
3030
import { promptForDiagnostics } from "../commands/captureDiagnostics";
31+
import { activateGetReferenceDocument } from "./getReferenceDocument";
3132

3233
interface SourceKitLogMessageParams extends langclient.LogMessageParams {
3334
logName?: string;
@@ -110,6 +111,7 @@ export class LanguageClientManager {
110111
private cancellationToken?: vscode.CancellationTokenSource;
111112
private legacyInlayHints?: vscode.Disposable;
112113
private peekDocuments?: vscode.Disposable;
114+
private getReferenceDocument?: vscode.Disposable;
113115
private restartedPromise?: Promise<void>;
114116
private currentWorkspaceFolder?: vscode.Uri;
115117
private waitingOnRestartCount: number;
@@ -247,6 +249,7 @@ export class LanguageClientManager {
247249
this.cancellationToken?.dispose();
248250
this.legacyInlayHints?.dispose();
249251
this.peekDocuments?.dispose();
252+
this.getReferenceDocument?.dispose();
250253
this.subscriptions.forEach(item => item.dispose());
251254
this.languageClient?.stop();
252255
this.namedOutputChannels.forEach(channel => channel.dispose());
@@ -397,6 +400,8 @@ export class LanguageClientManager {
397400
this.legacyInlayHints = undefined;
398401
this.peekDocuments?.dispose();
399402
this.peekDocuments = undefined;
403+
this.getReferenceDocument?.dispose();
404+
this.getReferenceDocument = undefined;
400405
if (client) {
401406
this.cancellationToken?.cancel();
402407
this.cancellationToken?.dispose();
@@ -573,6 +578,7 @@ export class LanguageClientManager {
573578
initializationFailedHandler: () => false,
574579
initializationOptions: {
575580
"workspace/peekDocuments": true, // workaround for client capability to handle `PeekDocumentsRequest`
581+
"workspace/getReferenceDocument": true, // the client can handle URIs with scheme `sourcekit-lsp:`
576582
"textDocument/codeLens": {
577583
supportedCommands: {
578584
"swift.run": "swift.run",
@@ -636,6 +642,8 @@ export class LanguageClientManager {
636642
}
637643

638644
this.peekDocuments = activatePeekDocuments(client);
645+
this.getReferenceDocument = activateGetReferenceDocument(client);
646+
this.workspaceContext.subscriptions.push(this.getReferenceDocument);
639647
})
640648
.catch(reason => {
641649
this.workspaceContext.outputChannel.log(`${reason}`);
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
//===----------------------------------------------------------------------===//
2+
//
3+
// This source file is part of the VS Code Swift open source project
4+
//
5+
// Copyright (c) 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+
import * as vscode from "vscode";
16+
import * as langclient from "vscode-languageclient/node";
17+
import { GetReferenceDocumentParams, GetReferenceDocumentRequest } from "./lspExtensions";
18+
19+
export function activateGetReferenceDocument(client: langclient.LanguageClient): vscode.Disposable {
20+
const getReferenceDocument = vscode.workspace.registerTextDocumentContentProvider(
21+
"sourcekit-lsp",
22+
{
23+
provideTextDocumentContent: async (uri, token) => {
24+
const params: GetReferenceDocumentParams = {
25+
uri: uri.toString(true),
26+
};
27+
28+
const result = await client.sendRequest(GetReferenceDocumentRequest, params, token);
29+
30+
if (result) {
31+
return result.content;
32+
} else {
33+
return "Unable to retrieve reference document";
34+
}
35+
},
36+
}
37+
);
38+
39+
return getReferenceDocument;
40+
}

src/sourcekit-lsp/lspExtensions.ts

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,31 @@ export const PeekDocumentsRequest = new langclient.RequestType<
5656
unknown
5757
>("workspace/peekDocuments");
5858

59+
// Get Reference Document
60+
export interface GetReferenceDocumentParams {
61+
/**
62+
* The `DocumentUri` of the custom scheme url for which content is required
63+
*/
64+
uri: langclient.DocumentUri;
65+
}
66+
67+
/**
68+
* Response containing `content` of `GetReferenceDocumentRequest`
69+
*/
70+
export interface GetReferenceDocumentResult {
71+
content: string;
72+
}
73+
74+
/**
75+
* Request from the client to the server asking for contents of a URI having a custom scheme
76+
* For example: "sourcekit-lsp:"
77+
*/
78+
export const GetReferenceDocumentRequest = new langclient.RequestType<
79+
GetReferenceDocumentParams,
80+
GetReferenceDocumentResult,
81+
unknown
82+
>("workspace/getReferenceDocument");
83+
5984
// Inlay Hints (pre Swift 5.6)
6085
export interface LegacyInlayHintsParams {
6186
/**

src/sourcekit-lsp/peekDocuments.ts

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -21,11 +21,9 @@ export function activatePeekDocuments(client: langclient.LanguageClient): vscode
2121
PeekDocumentsRequest.method,
2222
async (params: PeekDocumentsParams) => {
2323
const locations = params.locations.map(uri => {
24+
const url = new URL(uri);
2425
const location = new vscode.Location(
25-
vscode.Uri.from({
26-
scheme: "file",
27-
path: new URL(uri).pathname,
28-
}),
26+
vscode.Uri.parse(url.href, true),
2927
new vscode.Position(0, 0)
3028
);
3129

0 commit comments

Comments
 (0)