|
| 1 | +/* -------------------------------------------------------------------------------------------- |
| 2 | + * Copyright (c) Microsoft Corporation. All Rights Reserved. |
| 3 | + * See 'LICENSE' in the project root for license information. |
| 4 | + * ------------------------------------------------------------------------------------------ */ |
| 5 | +import * as vscode from 'vscode'; |
| 6 | +import { DefaultClient, openFileVersions } from '../client'; |
| 7 | +import { Position, RequestType } from 'vscode-languageclient'; |
| 8 | + |
| 9 | +interface GetInlayHintsParams { |
| 10 | + uri: string; |
| 11 | +} |
| 12 | + |
| 13 | +enum InlayHintKind { |
| 14 | + Type = 0, |
| 15 | + Parameter = 1, |
| 16 | +} |
| 17 | + |
| 18 | +interface CppInlayHint { |
| 19 | + position: Position; |
| 20 | + label: string; |
| 21 | + inlayHintKind: InlayHintKind; |
| 22 | + isValueRef: boolean; |
| 23 | + // hasParamName: boolean; |
| 24 | +} |
| 25 | + |
| 26 | +interface GetInlayHintsResult { |
| 27 | + fileVersion: number; |
| 28 | + canceled: boolean; |
| 29 | + inlayHints: CppInlayHint[]; |
| 30 | +} |
| 31 | + |
| 32 | +type InlayHintsCacheEntry = { |
| 33 | + FileVersion: number; |
| 34 | + TypeHints: vscode.InlayHint[]; |
| 35 | + ParameterHints: CppInlayHint[]; |
| 36 | +}; |
| 37 | + |
| 38 | +const GetInlayHintsRequest: RequestType<GetInlayHintsParams, GetInlayHintsResult, void, void> = |
| 39 | + new RequestType<GetInlayHintsParams, GetInlayHintsResult, void, void>('cpptools/getInlayHints'); |
| 40 | + |
| 41 | +export class InlayHintsProvider implements vscode.InlayHintsProvider { |
| 42 | + private client: DefaultClient; |
| 43 | + public onDidChangeInlayHintsEvent = new vscode.EventEmitter<void>(); |
| 44 | + public onDidChangeInlayHints?: vscode.Event<void>; |
| 45 | + private cache: Map<string, InlayHintsCacheEntry> = new Map<string, InlayHintsCacheEntry>(); |
| 46 | + |
| 47 | + constructor(client: DefaultClient) { |
| 48 | + this.client = client; |
| 49 | + this.onDidChangeInlayHints = this.onDidChangeInlayHintsEvent.event; |
| 50 | + } |
| 51 | + |
| 52 | + public async provideInlayHints(document: vscode.TextDocument, range: vscode.Range, |
| 53 | + token: vscode.CancellationToken): Promise<vscode.InlayHint[] | undefined> { |
| 54 | + await this.client.awaitUntilLanguageClientReady(); |
| 55 | + const uriString: string = document.uri.toString(); |
| 56 | + |
| 57 | + // Get results from cache if available. |
| 58 | + const cacheEntry: InlayHintsCacheEntry | undefined = this.cache.get(uriString); |
| 59 | + if (cacheEntry?.FileVersion === document.version) { |
| 60 | + return this.buildVSCodeHints(cacheEntry); |
| 61 | + } |
| 62 | + |
| 63 | + // Get new results from the language server |
| 64 | + const params: GetInlayHintsParams = { uri: uriString }; |
| 65 | + const inlayHintsResult: GetInlayHintsResult = await this.client.languageClient.sendRequest(GetInlayHintsRequest, params, token); |
| 66 | + if (!inlayHintsResult.canceled) { |
| 67 | + if (inlayHintsResult.fileVersion === openFileVersions.get(uriString)) { |
| 68 | + const cacheEntry: InlayHintsCacheEntry = this.createCacheEntry(inlayHintsResult); |
| 69 | + this.cache.set(uriString, cacheEntry); |
| 70 | + return this.buildVSCodeHints(cacheEntry); |
| 71 | + } else { |
| 72 | + // Force another request because file versions do not match. |
| 73 | + this.onDidChangeInlayHintsEvent.fire(); |
| 74 | + } |
| 75 | + } |
| 76 | + return undefined; |
| 77 | + } |
| 78 | + |
| 79 | + public invalidateFile(uri: string): void { |
| 80 | + this.cache.delete(uri); |
| 81 | + this.onDidChangeInlayHintsEvent.fire(); |
| 82 | + } |
| 83 | + |
| 84 | + private buildVSCodeHints(cacheEntry: InlayHintsCacheEntry): vscode.InlayHint[] { |
| 85 | + let result: vscode.InlayHint[] = []; |
| 86 | + const showTypeHintsEnabled: boolean = true; // TODO: get value from settings. |
| 87 | + const showParamHintsEnabled: boolean = true; // TODO: get value from settings. |
| 88 | + if (showTypeHintsEnabled) { |
| 89 | + result = result.concat(cacheEntry?.TypeHints); |
| 90 | + } |
| 91 | + if (showParamHintsEnabled) { |
| 92 | + const resolvedParameterHints: vscode.InlayHint[] = this.resolveParameterHints(cacheEntry.ParameterHints); |
| 93 | + result = result.concat(resolvedParameterHints); |
| 94 | + } |
| 95 | + return result; |
| 96 | + } |
| 97 | + |
| 98 | + private resolveParameterHints(hints: CppInlayHint[]): vscode.InlayHint[] { |
| 99 | + const resolvedHints: vscode.InlayHint[] = []; |
| 100 | + const showRefEnabled: boolean = true; // TODO: get from settings |
| 101 | + const hideParamNameEnabled: boolean = false; // TODO: get from settings |
| 102 | + for (const h of hints) { |
| 103 | + // Build parameter label based on settings. |
| 104 | + // TODO: remove label if param includes parameter name or in comments. |
| 105 | + const paramName: string = (hideParamNameEnabled /* && h.hasParamName*/) ? "" : h.label; |
| 106 | + let refString: string = ""; |
| 107 | + if (showRefEnabled && h.isValueRef) { |
| 108 | + refString = (paramName.length > 0) ? "& " : "&"; |
| 109 | + } |
| 110 | + const colonString: string = (paramName.length > 0 || refString.length > 0) ? ":" : ""; |
| 111 | + const label: string = refString + paramName + colonString; |
| 112 | + |
| 113 | + const inlayHint: vscode.InlayHint = new vscode.InlayHint( |
| 114 | + new vscode.Position(h.position.line, h.position.character), |
| 115 | + label, |
| 116 | + vscode.InlayHintKind.Parameter); |
| 117 | + inlayHint.paddingRight = true; |
| 118 | + resolvedHints.push(inlayHint); |
| 119 | + }; |
| 120 | + return resolvedHints; |
| 121 | + } |
| 122 | + |
| 123 | + private createCacheEntry(inlayHintsResults: GetInlayHintsResult): InlayHintsCacheEntry { |
| 124 | + const typeHints: vscode.InlayHint[] = []; |
| 125 | + for (const h of inlayHintsResults.inlayHints) { |
| 126 | + if (h.inlayHintKind === InlayHintKind.Type) { |
| 127 | + const inlayHint: vscode.InlayHint = new vscode.InlayHint( |
| 128 | + new vscode.Position(h.position.line, h.position.character), |
| 129 | + h.label, |
| 130 | + vscode.InlayHintKind.Type); |
| 131 | + inlayHint.paddingRight = true; |
| 132 | + typeHints.push(inlayHint); |
| 133 | + } |
| 134 | + } |
| 135 | + const paramHints: CppInlayHint[] = inlayHintsResults.inlayHints.filter(h => h.inlayHintKind === InlayHintKind.Parameter); |
| 136 | + const cacheEntry: InlayHintsCacheEntry = { |
| 137 | + FileVersion: inlayHintsResults.fileVersion, |
| 138 | + TypeHints: typeHints, |
| 139 | + ParameterHints: paramHints |
| 140 | + }; |
| 141 | + return cacheEntry; |
| 142 | + } |
| 143 | +} |
0 commit comments