|
| 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 { RequestType } from 'vscode-languageclient'; |
| 8 | +import { RazorLanguageServerClient } from '../razorLanguageServerClient'; |
| 9 | +import { RazorDocumentManager } from '../document/razorDocumentManager'; |
| 10 | +import { RazorLanguageServiceClient } from '../razorLanguageServiceClient'; |
| 11 | +import { RazorLanguageFeatureBase } from '../razorLanguageFeatureBase'; |
| 12 | +import { RazorDocumentSynchronizer } from '../document/razorDocumentSynchronizer'; |
| 13 | +import { RazorLogger } from '../razorLogger'; |
| 14 | +import { SerializableFormatNewFileParams } from './serializableFormatNewFileParams'; |
| 15 | +import { roslynFormatNewFileCommand } from '../../../lsptoolshost/razorCommands'; |
| 16 | + |
| 17 | +export class RazorFormatNewFileHandler extends RazorLanguageFeatureBase { |
| 18 | + private static readonly razorFormatNewFileCommand = 'razor/formatNewFile'; |
| 19 | + private formatNewFileRequestType: RequestType<SerializableFormatNewFileParams, string | undefined, any> = |
| 20 | + new RequestType(RazorFormatNewFileHandler.razorFormatNewFileCommand); |
| 21 | + |
| 22 | + constructor( |
| 23 | + documentSynchronizer: RazorDocumentSynchronizer, |
| 24 | + protected readonly serverClient: RazorLanguageServerClient, |
| 25 | + protected readonly serviceClient: RazorLanguageServiceClient, |
| 26 | + protected readonly documentManager: RazorDocumentManager, |
| 27 | + protected readonly logger: RazorLogger |
| 28 | + ) { |
| 29 | + super(documentSynchronizer, documentManager, serviceClient, logger); |
| 30 | + } |
| 31 | + |
| 32 | + public async register() { |
| 33 | + await this.serverClient.onRequestWithParams<SerializableFormatNewFileParams, string | undefined, any>( |
| 34 | + this.formatNewFileRequestType, |
| 35 | + async (request: SerializableFormatNewFileParams, token: vscode.CancellationToken) => |
| 36 | + this.getFormatNewFile(request, token) |
| 37 | + ); |
| 38 | + } |
| 39 | + |
| 40 | + private async getFormatNewFile( |
| 41 | + request: SerializableFormatNewFileParams, |
| 42 | + _: vscode.CancellationToken |
| 43 | + ): Promise<string | undefined> { |
| 44 | + if (!this.documentManager.roslynActivated) { |
| 45 | + return undefined; |
| 46 | + } |
| 47 | + |
| 48 | + const response: string | undefined = await vscode.commands.executeCommand(roslynFormatNewFileCommand, request); |
| 49 | + |
| 50 | + return response; |
| 51 | + } |
| 52 | +} |
0 commit comments