From 0bba771a2a5cb8d16ed659a89db4ab13f319b352 Mon Sep 17 00:00:00 2001 From: Alex Hoppen Date: Mon, 14 Jul 2025 17:05:45 +0200 Subject: [PATCH] Accept `Location` for `workspace/peekDocument` This allows the peek window to scroll to a position within the peeked document. --- src/sourcekit-lsp/LanguageClientConfiguration.ts | 1 + .../extensions/PeekDocumentsRequest.ts | 6 +++--- src/sourcekit-lsp/peekDocuments.ts | 15 ++++++++++----- 3 files changed, 14 insertions(+), 8 deletions(-) diff --git a/src/sourcekit-lsp/LanguageClientConfiguration.ts b/src/sourcekit-lsp/LanguageClientConfiguration.ts index 6a36cfe09..104ed99bf 100644 --- a/src/sourcekit-lsp/LanguageClientConfiguration.ts +++ b/src/sourcekit-lsp/LanguageClientConfiguration.ts @@ -45,6 +45,7 @@ function initializationOptions(swiftVersion: Version): any { options = { "workspace/peekDocuments": { supported: true, // workaround for client capability to handle `PeekDocumentsRequest` + peekLocation: true, // allow SourceKit-LSP to send `Location` instead of `DocumentUri` for the locations to peek. }, "workspace/getReferenceDocument": { supported: true, // the client can handle URIs with scheme `sourcekit-lsp:` diff --git a/src/sourcekit-lsp/extensions/PeekDocumentsRequest.ts b/src/sourcekit-lsp/extensions/PeekDocumentsRequest.ts index ea1302b40..ca38a5136 100644 --- a/src/sourcekit-lsp/extensions/PeekDocumentsRequest.ts +++ b/src/sourcekit-lsp/extensions/PeekDocumentsRequest.ts @@ -15,7 +15,7 @@ // We use namespaces to store request information just like vscode-languageclient /* eslint-disable @typescript-eslint/no-namespace */ -import { DocumentUri, Position, MessageDirection, RequestType } from "vscode-languageclient"; +import { DocumentUri, Position, Location, MessageDirection, RequestType } from "vscode-languageclient"; /** Parameters used to make a {@link PeekDocumentsRequest}. */ export interface PeekDocumentsParams { @@ -30,9 +30,9 @@ export interface PeekDocumentsParams { position: Position; /** - * An array `DocumentUri` of the documents to appear inside the "peeked" editor + * An array `DocumentUri` or `Location` of the documents to appear inside the "peeked" editor */ - locations: DocumentUri[]; + locations: DocumentUri[] | Location[]; } /** Response to indicate the `success` of the {@link PeekDocumentsRequest}. */ diff --git a/src/sourcekit-lsp/peekDocuments.ts b/src/sourcekit-lsp/peekDocuments.ts index bfbfb44fa..27881d431 100644 --- a/src/sourcekit-lsp/peekDocuments.ts +++ b/src/sourcekit-lsp/peekDocuments.ts @@ -89,11 +89,16 @@ export function activatePeekDocuments(client: langclient.LanguageClient): vscode ); const peekLocations = params.locations.map( - location => - new vscode.Location( - client.protocol2CodeConverter.asUri(location), - new vscode.Position(0, 0) - ) + location => { + if (typeof location === "string") { // DocumentUri is a typedef of `string`, so effectively we are checking for DocumentUri here + return new vscode.Location( + client.protocol2CodeConverter.asUri(location), + new vscode.Position(0, 0) + ) + } else { + return client.protocol2CodeConverter.asLocation(location) + } + } ); await openPeekedEditorIn(peekURI, peekPosition, peekLocations);