|
| 1 | +/*--------------------------------------------------------------------------------------------- |
| 2 | + * Copyright (c) Microsoft Corporation. All rights reserved. |
| 3 | + * Licensed under the MIT License. See License.txt in the project root for license information. |
| 4 | + *--------------------------------------------------------------------------------------------*/ |
| 5 | + |
| 6 | +import * as vscode from 'vscode'; |
| 7 | +import { InlayHint, InlayHintParams, RequestType, TextDocumentIdentifier } from 'vscode-languageclient'; |
| 8 | +import { RazorDocumentManager } from '../document/razorDocumentManager'; |
| 9 | +import { RazorLanguageServerClient } from '../razorLanguageServerClient'; |
| 10 | +import { RazorLogger } from '../razorLogger'; |
| 11 | +import { SerializableInlayHintParams } from './serializableInlayHintParams'; |
| 12 | +import { provideInlayHintsCommand } from '../../../lsptoolshost/razorCommands'; |
| 13 | +import { UriConverter } from '../../../lsptoolshost/uriConverter'; |
| 14 | +import { RazorDocumentSynchronizer } from '../document/razorDocumentSynchronizer'; |
| 15 | + |
| 16 | +export class InlayHintHandler { |
| 17 | + private static readonly provideInlayHint = 'razor/inlayHint'; |
| 18 | + private InlayHintRequestType: RequestType<SerializableInlayHintParams, InlayHint[], any> = new RequestType( |
| 19 | + InlayHintHandler.provideInlayHint |
| 20 | + ); |
| 21 | + private emptyInlayHintResponse = new Array<InlayHint>(); |
| 22 | + |
| 23 | + constructor( |
| 24 | + private readonly serverClient: RazorLanguageServerClient, |
| 25 | + private readonly documentManager: RazorDocumentManager, |
| 26 | + private readonly documentSynchronizer: RazorDocumentSynchronizer, |
| 27 | + private readonly logger: RazorLogger |
| 28 | + ) {} |
| 29 | + |
| 30 | + public async register() { |
| 31 | + await this.serverClient.onRequestWithParams<SerializableInlayHintParams, InlayHint[], any>( |
| 32 | + this.InlayHintRequestType, |
| 33 | + async (request, token) => this.provideInlayHints(request, token) |
| 34 | + ); |
| 35 | + } |
| 36 | + |
| 37 | + private async provideInlayHints(inlayHintParams: SerializableInlayHintParams, token: vscode.CancellationToken) { |
| 38 | + try { |
| 39 | + const razorDocumentUri = vscode.Uri.parse(inlayHintParams.identifier.textDocumentIdentifier.uri, true); |
| 40 | + const razorDocument = await this.documentManager.getDocument(razorDocumentUri); |
| 41 | + if (razorDocument === undefined) { |
| 42 | + return this.emptyInlayHintResponse; |
| 43 | + } |
| 44 | + |
| 45 | + if (!this.documentManager.roslynActivated) { |
| 46 | + // Unlike most other handlers, inlay hints works by directly sending an LSP request to Roslyn, so if Roslyn isn't |
| 47 | + // activated we need to catch that here. |
| 48 | + return this.emptyInlayHintResponse; |
| 49 | + } |
| 50 | + |
| 51 | + const textDocument = await vscode.workspace.openTextDocument(razorDocumentUri); |
| 52 | + const synchronized = await this.documentSynchronizer.trySynchronizeProjectedDocument( |
| 53 | + textDocument, |
| 54 | + razorDocument.csharpDocument, |
| 55 | + inlayHintParams.identifier.version, |
| 56 | + token |
| 57 | + ); |
| 58 | + if (!synchronized) { |
| 59 | + return this.emptyInlayHintResponse; |
| 60 | + } |
| 61 | + |
| 62 | + const virtualCSharpUri = UriConverter.serialize(razorDocument.csharpDocument.uri); |
| 63 | + |
| 64 | + const roslynInlayHintParams = <InlayHintParams>{ |
| 65 | + textDocument: TextDocumentIdentifier.create(virtualCSharpUri), |
| 66 | + range: inlayHintParams.projectedRange, |
| 67 | + }; |
| 68 | + const csharpInlayHints = await vscode.commands.executeCommand<InlayHint[]>( |
| 69 | + provideInlayHintsCommand, |
| 70 | + roslynInlayHintParams |
| 71 | + ); |
| 72 | + |
| 73 | + return csharpInlayHints; |
| 74 | + } catch (error) { |
| 75 | + this.logger.logWarning(`${InlayHintHandler.provideInlayHint} failed with ${error}`); |
| 76 | + } |
| 77 | + |
| 78 | + return this.emptyInlayHintResponse; |
| 79 | + } |
| 80 | +} |
0 commit comments