|
| 1 | +import { |
| 2 | + DisposableCollection, MonacoLanguageClient |
| 3 | +} from 'monaco-languageclient' |
| 4 | +import { StaticFeature, FeatureState } from 'vscode-languageclient/lib/common/api' |
| 5 | +import { InlayHint, InlayHintParams, InlayHintRefreshRequest, InlayHintRequest, WorkspaceEdit } from 'vscode-languageserver-protocol' |
| 6 | +import * as vscode from 'vscode' |
| 7 | + |
| 8 | +async function asInlayHints (values: InlayHint[] | undefined | null, client: MonacoLanguageClient): Promise<vscode.InlayHint[] | undefined> { |
| 9 | + if (!Array.isArray(values)) { |
| 10 | + return undefined |
| 11 | + } |
| 12 | + return values.map(lsHint => asInlayHint(lsHint, client)) |
| 13 | +} |
| 14 | + |
| 15 | +function asInlayHint (value: InlayHint, client: MonacoLanguageClient): vscode.InlayHint { |
| 16 | + const label = value.label as string |
| 17 | + const result = new vscode.InlayHint(client.protocol2CodeConverter.asPosition(value.position), label) |
| 18 | + result.paddingRight = true |
| 19 | + result.kind = vscode.InlayHintKind.Parameter |
| 20 | + return result |
| 21 | +} |
| 22 | + |
| 23 | +/** |
| 24 | + * Comes from https://github.com/redhat-developer/vscode-java/blob/9b6046eecc65fd47507f309a3ccc9add45c6d3be/src/inlayHintsProvider.ts#L5 |
| 25 | + */ |
| 26 | +class JavaInlayHintsProvider implements vscode.InlayHintsProvider { |
| 27 | + private onDidChange = new vscode.EventEmitter<void>() |
| 28 | + public onDidChangeInlayHints = this.onDidChange.event |
| 29 | + |
| 30 | + constructor (private client: MonacoLanguageClient) { |
| 31 | + this.client.onRequest(InlayHintRefreshRequest.type, async () => { |
| 32 | + this.onDidChange.fire() |
| 33 | + }) |
| 34 | + } |
| 35 | + |
| 36 | + public async provideInlayHints (document: vscode.TextDocument, range: vscode.Range, token: vscode.CancellationToken): Promise<vscode.InlayHint[] | undefined> { |
| 37 | + const requestParams: InlayHintParams = { |
| 38 | + textDocument: this.client.code2ProtocolConverter.asTextDocumentIdentifier(document), |
| 39 | + range: this.client.code2ProtocolConverter.asRange(range) |
| 40 | + } |
| 41 | + try { |
| 42 | + const values = await this.client.sendRequest(InlayHintRequest.type, requestParams, token) |
| 43 | + if (token.isCancellationRequested) { |
| 44 | + return [] |
| 45 | + } |
| 46 | + return asInlayHints(values, this.client) |
| 47 | + } catch (error) { |
| 48 | + return this.client.handleFailedRequest(InlayHintRequest.type, token, error, []) |
| 49 | + } |
| 50 | + } |
| 51 | +} |
| 52 | + |
| 53 | +export class JavaExtensionFeature implements StaticFeature { |
| 54 | + private disposables: DisposableCollection |
| 55 | + constructor (private languageClient: MonacoLanguageClient) { |
| 56 | + this.disposables = new DisposableCollection() |
| 57 | + } |
| 58 | + |
| 59 | + fillClientCapabilities (): void {} |
| 60 | + |
| 61 | + initialize (): void { |
| 62 | + // Comes from https://github.com/redhat-developer/vscode-java/blob/9b6046eecc65fd47507f309a3ccc9add45c6d3be/src/standardLanguageClient.ts#L321 |
| 63 | + this.disposables.push(vscode.commands.registerCommand('java.apply.workspaceEdit', async (obj: WorkspaceEdit) => { |
| 64 | + const edit = await this.languageClient.protocol2CodeConverter.asWorkspaceEdit(obj) |
| 65 | + return vscode.workspace.applyEdit(edit) |
| 66 | + })) |
| 67 | + |
| 68 | + this.disposables.push(vscode.languages.registerInlayHintsProvider(this.languageClient.clientOptions.documentSelector!, new JavaInlayHintsProvider(this.languageClient))) |
| 69 | + } |
| 70 | + |
| 71 | + getState (): FeatureState { |
| 72 | + return { |
| 73 | + kind: 'static' |
| 74 | + } |
| 75 | + } |
| 76 | + |
| 77 | + dispose (): void { |
| 78 | + this.disposables.dispose() |
| 79 | + } |
| 80 | +} |
0 commit comments