Skip to content

Commit 3e6416b

Browse files
author
Andrew Hall
committed
Move code to be more uniform
1 parent 2eeb1f3 commit 3e6416b

File tree

4 files changed

+79
-65
lines changed

4 files changed

+79
-65
lines changed

src/lsptoolshost/activate.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -85,7 +85,7 @@ export async function activateRoslynLanguageServer(
8585
registerNestedCodeActionCommands(context, languageServer, _channel);
8686
registerCodeActionFixAllCommands(context, languageServer, _channel);
8787

88-
registerRazorCommands(context, languageServer);
88+
registerRazorCommands(context, languageServer, razorLogger);
8989
registerRazorEndpoints(context, languageServer, razorLogger, platformInfo);
9090

9191
registerUnitTestingCommands(context, languageServer);

src/lsptoolshost/razor/razorCommands.ts

Lines changed: 30 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,10 @@ import {
3131
import SerializableSimplifyMethodParams from '../../razor/src/simplify/serializableSimplifyMethodParams';
3232
import { TextEdit } from 'vscode-html-languageservice';
3333
import { SerializableFormatNewFileParams } from '../../razor/src/formatNewFile/serializableFormatNewFileParams';
34+
import { RazorLogger } from '../../razor/src/razorLogger';
35+
import { RazorDynamicFileChangedParams } from '../../razor/src/dynamicFile/dynamicFileUpdatedParams';
36+
import { DynamicFileInfoHandler } from '../../razor/src/dynamicFile/dynamicFileInfoHandler';
37+
import { razorOptions } from '../../shared/options';
3438

3539
// These are commands that are invoked by the Razor extension, and are used to send LSP requests to the Roslyn LSP server
3640
export const roslynDidChangeCommand = 'roslyn.changeRazorCSharp';
@@ -47,7 +51,11 @@ export const razorInitializeCommand = 'razor.initialize';
4751
export const provideInlayHintsCommand = 'roslyn.provideInlayHints';
4852
export const resolveInlayHintCommand = 'roslyn.resolveInlayHint';
4953

50-
export function registerRazorCommands(context: vscode.ExtensionContext, languageServer: RoslynLanguageServer) {
54+
export function registerRazorCommands(
55+
context: vscode.ExtensionContext,
56+
languageServer: RoslynLanguageServer,
57+
razorLogger: RazorLogger
58+
) {
5159
// Razor will call into us (via command) for generated file didChange/didClose notifications. We'll then forward these
5260
// notifications along to Roslyn. didOpen notifications are handled separately via the vscode.openTextDocument method.
5361
context.subscriptions.push(
@@ -138,4 +146,25 @@ export function registerRazorCommands(context: vscode.ExtensionContext, language
138146
await languageServer.sendNotification('razor/initialize', { pipeName: pipeName });
139147
})
140148
);
149+
150+
if (!razorOptions.cohostingEnabled) {
151+
// Dynamic files are only handled when cohosting is disabled.
152+
context.subscriptions.push(
153+
vscode.commands.registerCommand(
154+
DynamicFileInfoHandler.dynamicFileUpdatedCommand,
155+
async (notification: RazorDynamicFileChangedParams) => {
156+
if (languageServer.isRunning()) {
157+
await languageServer.sendNotification<RazorDynamicFileChangedParams>(
158+
'razor/dynamicFileInfoChanged',
159+
notification
160+
);
161+
} else {
162+
razorLogger.logVerbose(
163+
'Tried to send razor/dynamicFileInfoChanged while server is not running'
164+
);
165+
}
166+
}
167+
)
168+
);
169+
}
141170
}

src/lsptoolshost/razor/razorEndpoints.ts

Lines changed: 48 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,9 @@ import { DocumentColorHandler } from '../../razor/src/documentColor/documentColo
2424
import { razorOptions } from '../../shared/options';
2525
import { ColorPresentationHandler } from '../../razor/src/colorPresentation/colorPresentationHandler';
2626
import { ColorPresentation } from 'vscode-html-languageservice';
27+
import { DynamicFileInfoHandler } from '../../razor/src/dynamicFile/dynamicFileInfoHandler';
28+
import { ProvideDynamicFileParams } from '../../razor/src/dynamicFile/provideDynamicFileParams';
29+
import { ProvideDynamicFileResponse } from '../../razor/src/dynamicFile/provideDynamicFileResponse';
2730

2831
export function registerRazorEndpoints(
2932
context: vscode.ExtensionContext,
@@ -36,34 +39,59 @@ export function registerRazorEndpoints(
3639
razorLogger.log(params.message, params.type)
3740
);
3841

39-
if (!razorOptions.cohostingEnabled) {
40-
return;
42+
if (razorOptions.cohostingEnabled) {
43+
registerCohostingEndpoints();
44+
} else {
45+
registerNonCohostingEndpoints();
4146
}
4247

43-
const documentManager = new HtmlDocumentManager(platformInfo, razorLogger);
44-
context.subscriptions.push(documentManager.register());
48+
return;
4549

46-
registerRequestHandler<HtmlUpdateParameters, void>('razor/updateHtml', async (params) => {
47-
const uri = UriConverter.deserialize(params.textDocument.uri);
48-
await documentManager.updateDocumentText(uri, params.text);
49-
});
50+
//
51+
// Local Functions
52+
//
53+
function registerCohostingEndpoints() {
54+
const documentManager = new HtmlDocumentManager(platformInfo, razorLogger);
55+
context.subscriptions.push(documentManager.register());
5056

51-
registerRequestHandler<DocumentColorParams, ColorInformation[]>(DocumentColorRequest.method, async (params) => {
52-
const uri = UriConverter.deserialize(params.textDocument.uri);
53-
const document = await documentManager.getDocument(uri);
54-
55-
return await DocumentColorHandler.doDocumentColorRequest(document.uri);
56-
});
57+
registerRequestHandler<HtmlUpdateParameters, void>('razor/updateHtml', async (params) => {
58+
const uri = UriConverter.deserialize(params.textDocument.uri);
59+
await documentManager.updateDocumentText(uri, params.text);
60+
});
5761

58-
registerRequestHandler<ColorPresentationParams, ColorPresentation[]>(
59-
ColorPresentationRequest.method,
60-
async (params) => {
62+
registerRequestHandler<DocumentColorParams, ColorInformation[]>(DocumentColorRequest.method, async (params) => {
6163
const uri = UriConverter.deserialize(params.textDocument.uri);
6264
const document = await documentManager.getDocument(uri);
6365

64-
return await ColorPresentationHandler.doColorPresentationRequest(document.uri, params);
65-
}
66-
);
66+
return await DocumentColorHandler.doDocumentColorRequest(document.uri);
67+
});
68+
69+
registerRequestHandler<ColorPresentationParams, ColorPresentation[]>(
70+
ColorPresentationRequest.method,
71+
async (params) => {
72+
const uri = UriConverter.deserialize(params.textDocument.uri);
73+
const document = await documentManager.getDocument(uri);
74+
75+
return await ColorPresentationHandler.doColorPresentationRequest(document.uri, params);
76+
}
77+
);
78+
}
79+
80+
function registerNonCohostingEndpoints() {
81+
// When the Roslyn language server sends a request for Razor dynamic file info, we forward that request along to Razor via
82+
// a command.
83+
registerRequestHandler<ProvideDynamicFileParams, ProvideDynamicFileResponse>(
84+
'razor/provideDynamicFileInfo',
85+
async (params) =>
86+
vscode.commands.executeCommand(DynamicFileInfoHandler.provideDynamicFileInfoCommand, params)
87+
);
88+
89+
registerRequestHandler<ProvideDynamicFileParams, ProvideDynamicFileResponse>(
90+
'razor/removeDynamicFileInfo',
91+
async (params) =>
92+
vscode.commands.executeCommand(DynamicFileInfoHandler.provideDynamicFileInfoCommand, params)
93+
);
94+
}
6795

6896
// Helper method that registers a request handler, and logs errors to the Razor logger.
6997
function registerRequestHandler<Params, Result>(method: string, invocation: (params: Params) => Promise<Result>) {

src/lsptoolshost/server/roslynLanguageServer.ts

Lines changed: 0 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,6 @@ import {
3232
} from 'vscode-languageclient/node';
3333
import { PlatformInformation } from '../../shared/platform';
3434
import { readConfigurations } from '../options/configurationMiddleware';
35-
import { DynamicFileInfoHandler } from '../../razor/src/dynamicFile/dynamicFileInfoHandler';
3635
import * as RoslynProtocol from './roslynProtocol';
3736
import { CSharpDevKitExports } from '../../csharpDevKitExports';
3837
import { SolutionSnapshotId } from '../solutionSnapshot/ISolutionSnapshotProvider';
@@ -60,16 +59,13 @@ import { BuildDiagnosticsService } from '../diagnostics/buildDiagnosticsService'
6059
import { getComponentPaths } from '../extensions/builtInComponents';
6160
import { OnAutoInsertFeature } from '../autoInsert/onAutoInsertFeature';
6261
import { ProjectContextService } from '../projectContext/projectContextService';
63-
import { ProvideDynamicFileResponse } from '../../razor/src/dynamicFile/provideDynamicFileResponse';
64-
import { ProvideDynamicFileParams } from '../../razor/src/dynamicFile/provideDynamicFileParams';
6562
import {
6663
ActionOption,
6764
CommandOption,
6865
showErrorMessage,
6966
showInformationMessage,
7067
} from '../../shared/observers/utils/showMessage';
7168
import { TelemetryEventNames } from '../../shared/telemetryEventNames';
72-
import { RazorDynamicFileChangedParams } from '../../razor/src/dynamicFile/dynamicFileUpdatedParams';
7369
import { getProfilingEnvVars } from '../profiling/profiling';
7470
import { isString } from '../utils/isString';
7571
import { getServerPath } from '../activate';
@@ -84,10 +80,6 @@ import {
8480
let _wasActivatedWithCSharpDevkit: boolean | undefined;
8581

8682
export class RoslynLanguageServer {
87-
// These are notifications we will get from the LSP server and will forward to the Razor extension.
88-
private static readonly provideRazorDynamicFileInfoMethodName: string = 'razor/provideDynamicFileInfo';
89-
private static readonly removeRazorDynamicFileInfoMethodName: string = 'razor/removeDynamicFileInfo';
90-
9183
/**
9284
* The encoding to use when writing to and from the stream.
9385
*/
@@ -142,10 +134,6 @@ export class RoslynLanguageServer {
142134
this.registerDocumentOpenForDiagnostics();
143135

144136
this._projectContextService = new ProjectContextService(this, this._languageServerEvents);
145-
146-
// Register Razor dynamic file info handling
147-
this.registerDynamicFileInfo();
148-
149137
this.registerDebuggerAttach();
150138

151139
registerShowToastNotification(this._languageClient);
@@ -815,37 +803,6 @@ export class RoslynLanguageServer {
815803
};
816804
}
817805

818-
private ProvideDyanmicFileInfoType: RequestType<ProvideDynamicFileParams, ProvideDynamicFileResponse, any> =
819-
new RequestType(RoslynLanguageServer.provideRazorDynamicFileInfoMethodName);
820-
821-
private registerDynamicFileInfo() {
822-
// When the Roslyn language server sends a request for Razor dynamic file info, we forward that request along to Razor via
823-
// a command.
824-
this._languageClient.onRequest<ProvideDynamicFileParams, ProvideDynamicFileResponse, any>(
825-
this.ProvideDyanmicFileInfoType,
826-
async (request) =>
827-
vscode.commands.executeCommand(DynamicFileInfoHandler.provideDynamicFileInfoCommand, request)
828-
);
829-
this._languageClient.onNotification(
830-
RoslynLanguageServer.removeRazorDynamicFileInfoMethodName,
831-
async (notification) =>
832-
vscode.commands.executeCommand(DynamicFileInfoHandler.removeDynamicFileInfoCommand, notification)
833-
);
834-
vscode.commands.registerCommand(
835-
DynamicFileInfoHandler.dynamicFileUpdatedCommand,
836-
async (notification: RazorDynamicFileChangedParams) => {
837-
if (this.isRunning()) {
838-
await this.sendNotification<RazorDynamicFileChangedParams>(
839-
'razor/dynamicFileInfoChanged',
840-
notification
841-
);
842-
} else {
843-
this._channel.warn('Tried to send razor/dynamicFileInfoChanged while server is not running');
844-
}
845-
}
846-
);
847-
}
848-
849806
// eslint-disable-next-line @typescript-eslint/promise-function-async
850807
private WaitForAttachCompleteAsync(attachRequestId: string): Promise<boolean> {
851808
return new Promise<boolean>((resolve) => {

0 commit comments

Comments
 (0)