|
| 1 | +import * as vscode from "vscode"; |
| 2 | +import { |
| 3 | + Hover, |
| 4 | + HoverRequest, |
| 5 | + LanguageClient, |
| 6 | + TextDocumentPositionParams, |
| 7 | +} from "vscode-languageclient/node"; |
| 8 | + |
| 9 | +interface HoverResult extends Hover { |
| 10 | + canIncreaseVerbosity?: boolean; |
| 11 | +} |
| 12 | + |
| 13 | +interface HoverParamsWithVerbosity extends TextDocumentPositionParams { |
| 14 | + verbosityLevel?: number; |
| 15 | +} |
| 16 | + |
| 17 | +class VerboseHoverProvider implements vscode.HoverProvider { |
| 18 | + private lastHoverAndLevel: [vscode.Hover, number] | undefined; |
| 19 | + |
| 20 | + constructor(private readonly client: LanguageClient) {} |
| 21 | + |
| 22 | + async provideHover( |
| 23 | + document: vscode.TextDocument, |
| 24 | + position: vscode.Position, |
| 25 | + token: vscode.CancellationToken, |
| 26 | + context?: vscode.HoverContext, |
| 27 | + ): Promise<vscode.VerboseHover | vscode.Hover | undefined> { |
| 28 | + // HoverContext and VerboseHover are proposed API; guard against missing or unexpected properties. |
| 29 | + const verbosityDelta = typeof context?.verbosityDelta === "number" ? context.verbosityDelta : undefined; |
| 30 | + const previousHover = context?.previousHover instanceof vscode.Hover ? context.previousHover : undefined; |
| 31 | + const verbosityLevel = verbosityDelta !== undefined ? Math.max(0, this.getPreviousLevel(previousHover) + verbosityDelta) : undefined; |
| 32 | + |
| 33 | + const params: HoverParamsWithVerbosity = { |
| 34 | + ...this.client.code2ProtocolConverter.asTextDocumentPositionParams(document, position), |
| 35 | + verbosityLevel, |
| 36 | + }; |
| 37 | + |
| 38 | + let response: HoverResult | null; |
| 39 | + try { |
| 40 | + response = await this.client.sendRequest(HoverRequest.type, params, token); |
| 41 | + } |
| 42 | + catch (error) { |
| 43 | + return this.client.handleFailedRequest(HoverRequest.type, token, error, null) ?? undefined; |
| 44 | + } |
| 45 | + |
| 46 | + if (!response || token.isCancellationRequested) { |
| 47 | + return undefined; |
| 48 | + } |
| 49 | + |
| 50 | + const hover = this.client.protocol2CodeConverter.asHover(response); |
| 51 | + // VerboseHover is proposed API; guard against missing or changed constructor. |
| 52 | + try { |
| 53 | + const verboseHover = new vscode.VerboseHover( |
| 54 | + hover.contents, |
| 55 | + hover.range, |
| 56 | + response.canIncreaseVerbosity, |
| 57 | + (verbosityLevel ?? 0) > 0, |
| 58 | + ); |
| 59 | + |
| 60 | + this.lastHoverAndLevel = [verboseHover, verbosityLevel ?? 0]; |
| 61 | + return verboseHover; |
| 62 | + } |
| 63 | + catch { |
| 64 | + return hover; |
| 65 | + } |
| 66 | + } |
| 67 | + |
| 68 | + private getPreviousLevel(previousHover: vscode.Hover | undefined): number { |
| 69 | + if (previousHover && this.lastHoverAndLevel && this.lastHoverAndLevel[0] === previousHover) { |
| 70 | + return this.lastHoverAndLevel[1]; |
| 71 | + } |
| 72 | + return 0; |
| 73 | + } |
| 74 | +} |
| 75 | + |
| 76 | +export function registerHoverFeature( |
| 77 | + selector: vscode.DocumentSelector, |
| 78 | + client: LanguageClient, |
| 79 | +): vscode.Disposable { |
| 80 | + return vscode.languages.registerHoverProvider(selector, new VerboseHoverProvider(client)); |
| 81 | +} |
0 commit comments