|
5 | 5 |
|
6 | 6 | import { RoslynLanguageServer } from '../server/roslynLanguageServer';
|
7 | 7 | import * as vscode from 'vscode';
|
8 |
| -import { LogMessageParams, NotificationType } from 'vscode-languageclient'; |
| 8 | +import { LogMessageParams, NotificationType, RequestType } from 'vscode-languageclient'; |
9 | 9 | import { RazorLogger } from '../../razor/src/razorLogger';
|
| 10 | +import { HtmlUpdateParameters } from './htmlUpdateParameters'; |
| 11 | +import { UriConverter } from '../utils/uriConverter'; |
| 12 | +import { PlatformInformation } from '../../shared/platform'; |
| 13 | +import { HtmlDocumentManager } from './htmlDocumentManager'; |
| 14 | +import { razorOptions } from '../../shared/options'; |
10 | 15 |
|
11 | 16 | export function registerRazorEndpoints(
|
12 | 17 | context: vscode.ExtensionContext,
|
13 | 18 | languageServer: RoslynLanguageServer,
|
14 |
| - razorLogger: RazorLogger |
| 19 | + razorLogger: RazorLogger, |
| 20 | + platformInfo: PlatformInformation |
15 | 21 | ) {
|
16 | 22 | const logNotificationType = new NotificationType<LogMessageParams>('razor/log');
|
17 | 23 | languageServer.registerOnNotificationWithParams(logNotificationType, (params) =>
|
18 | 24 | razorLogger.log(params.message, params.type)
|
19 | 25 | );
|
| 26 | + |
| 27 | + if (!razorOptions.cohostingEnabled) { |
| 28 | + return; |
| 29 | + } |
| 30 | + |
| 31 | + const documentManager = new HtmlDocumentManager(platformInfo, razorLogger); |
| 32 | + context.subscriptions.push(documentManager.register()); |
| 33 | + |
| 34 | + registerRequestHandler<HtmlUpdateParameters, void>('razor/updateHtml', async (params) => { |
| 35 | + const uri = UriConverter.deserialize(params.textDocument.uri); |
| 36 | + await documentManager.updateDocumentText(uri, params.text); |
| 37 | + }); |
| 38 | + |
| 39 | + // Helper method that registers a request handler, and logs errors to the Razor logger. |
| 40 | + function registerRequestHandler<Params, Result>(method: string, invocation: (params: Params) => Promise<Result>) { |
| 41 | + const requestType = new RequestType<Params, Result, Error>(method); |
| 42 | + languageServer.registerOnRequest(requestType, async (params) => { |
| 43 | + try { |
| 44 | + return await invocation(params); |
| 45 | + } catch (error) { |
| 46 | + razorLogger.logError(`Error: ${error}`, error); |
| 47 | + return undefined; |
| 48 | + } |
| 49 | + }); |
| 50 | + } |
20 | 51 | }
|
0 commit comments