Skip to content

Commit 3d13794

Browse files
author
Andrew Hall
committed
Working but needs server fix
1 parent 246106b commit 3d13794

12 files changed

+85
-72
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, razorLogger);
88+
registerRazorCommands(context, languageServer);
8989
registerRazorEndpoints(context, languageServer, razorLogger, platformInfo);
9090

9191
registerUnitTestingCommands(context, languageServer);

src/lsptoolshost/razor/razorCommands.ts

Lines changed: 1 addition & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -31,10 +31,6 @@ 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';
3834

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

54-
export function registerRazorCommands(
55-
context: vscode.ExtensionContext,
56-
languageServer: RoslynLanguageServer,
57-
razorLogger: RazorLogger
58-
) {
50+
export function registerRazorCommands(context: vscode.ExtensionContext, languageServer: RoslynLanguageServer) {
5951
// Razor will call into us (via command) for generated file didChange/didClose notifications. We'll then forward these
6052
// notifications along to Roslyn. didOpen notifications are handled separately via the vscode.openTextDocument method.
6153
context.subscriptions.push(
@@ -146,25 +138,4 @@ export function registerRazorCommands(
146138
await languageServer.sendNotification('razor/initialize', { pipeName: pipeName });
147139
})
148140
);
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-
}
170141
}

src/lsptoolshost/razor/razorEndpoints.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -30,8 +30,8 @@ import { ProvideDynamicFileResponse } from '../../razor/src/dynamicFile/provideD
3030
import { RazorMapSpansParams } from '../../razor/src/mapping/razorMapSpansParams';
3131
import { RazorMapSpansResponse } from '../../razor/src/mapping/razorMapSpansResponse';
3232
import { MappingHandler } from '../../razor/src/mapping/mappingHandler';
33-
import { RazorMapTextChangesParams } from '../../razor/src/mapping/RazorMapTextChangesParams';
34-
import { RazorMapTextChangesResponse } from '../../razor/src/mapping/RazorMapTextChangesResponse';
33+
import { RazorMapTextChangesParams } from '../../razor/src/mapping/razorMapTextChangesParams';
34+
import { RazorMapTextChangesResponse } from '../../razor/src/mapping/razorMapTextChangesResponse';
3535

3636
export function registerRazorEndpoints(
3737
context: vscode.ExtensionContext,

src/lsptoolshost/server/roslynLanguageServer.ts

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@ 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';
3536
import * as RoslynProtocol from './roslynProtocol';
3637
import { CSharpDevKitExports } from '../../csharpDevKitExports';
3738
import { SolutionSnapshotId } from '../solutionSnapshot/ISolutionSnapshotProvider';
@@ -59,13 +60,16 @@ import { BuildDiagnosticsService } from '../diagnostics/buildDiagnosticsService'
5960
import { getComponentPaths } from '../extensions/builtInComponents';
6061
import { OnAutoInsertFeature } from '../autoInsert/onAutoInsertFeature';
6162
import { ProjectContextService } from '../projectContext/projectContextService';
63+
import { ProvideDynamicFileResponse } from '../../razor/src/dynamicFile/provideDynamicFileResponse';
64+
import { ProvideDynamicFileParams } from '../../razor/src/dynamicFile/provideDynamicFileParams';
6265
import {
6366
ActionOption,
6467
CommandOption,
6568
showErrorMessage,
6669
showInformationMessage,
6770
} from '../../shared/observers/utils/showMessage';
6871
import { TelemetryEventNames } from '../../shared/telemetryEventNames';
72+
import { RazorDynamicFileChangedParams } from '../../razor/src/dynamicFile/dynamicFileUpdatedParams';
6973
import { getProfilingEnvVars } from '../profiling/profiling';
7074
import { isString } from '../utils/isString';
7175
import { getServerPath } from '../activate';
@@ -80,6 +84,10 @@ import {
8084
let _wasActivatedWithCSharpDevkit: boolean | undefined;
8185

8286
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+
8391
/**
8492
* The encoding to use when writing to and from the stream.
8593
*/
@@ -134,6 +142,10 @@ export class RoslynLanguageServer {
134142
this.registerDocumentOpenForDiagnostics();
135143

136144
this._projectContextService = new ProjectContextService(this, this._languageServerEvents);
145+
146+
// Register Razor dynamic file info handling
147+
this.registerDynamicFileInfo();
148+
137149
this.registerDebuggerAttach();
138150

139151
registerShowToastNotification(this._languageClient);
@@ -803,6 +815,37 @@ export class RoslynLanguageServer {
803815
};
804816
}
805817

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+
806849
// eslint-disable-next-line @typescript-eslint/promise-function-async
807850
private WaitForAttachCompleteAsync(attachRequestId: string): Promise<boolean> {
808851
return new Promise<boolean>((resolve) => {

src/razor/src/document/razorDocumentManager.ts

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -57,12 +57,14 @@ export class RazorDocumentManager implements IRazorDocumentManager {
5757
}
5858

5959
public async getDocumentForCSharpUri(csharpUri: vscode.Uri): Promise<IRazorDocument | undefined> {
60+
const csharpPath = csharpUri.fsPath ?? csharpUri.path;
61+
6062
return this.documents.find((document) => {
6163
if (this.platformInfo.isLinux()) {
62-
return document.csharpDocument.path === csharpUri.path;
64+
return document.csharpDocument.path === csharpPath;
6365
}
6466

65-
return document.csharpDocument.path.localeCompare(csharpUri.path, undefined, { sensitivity: 'base' }) === 0;
67+
return document.csharpDocument.path.localeCompare(csharpPath, undefined, { sensitivity: 'base' }) === 0;
6668
});
6769
}
6870

src/razor/src/dynamicFile/dynamicFileInfoHandler.ts

Lines changed: 16 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ import { RazorDynamicFileChangedParams } from './dynamicFileUpdatedParams';
1616
import { TextDocumentIdentifier } from 'vscode-languageserver-protocol';
1717
import { razorTextChange } from './razorTextChange';
1818
import { razorTextSpan } from './razorTextSpan';
19+
import { razorOptions } from '../../../shared/options';
1920

2021
// Handles Razor generated doc communication between the Roslyn workspace and Razor.
2122
// didChange behavior for Razor generated docs is handled in the RazorDocumentManager.
@@ -39,18 +40,21 @@ export class DynamicFileInfoHandler {
3940
await this.removeDynamicFileInfo(request);
4041
}
4142
);
42-
this.documentManager.onChange(async (e) => {
43-
// Ignore any updates without text changes. This is important for perf since sending an update to roslyn does
44-
// a round trip for producing nothing new and causes a lot of churn in solution updates.
45-
if (e.kind == RazorDocumentChangeKind.csharpChanged && !e.document.isOpen && e.changes.length > 0) {
46-
const uriString = UriConverter.serialize(e.document.uri);
47-
const identifier = TextDocumentIdentifier.create(uriString);
48-
await vscode.commands.executeCommand(
49-
DynamicFileInfoHandler.dynamicFileUpdatedCommand,
50-
new RazorDynamicFileChangedParams(identifier)
51-
);
52-
}
53-
});
43+
44+
if (!razorOptions.cohostingEnabled) {
45+
this.documentManager.onChange(async (e) => {
46+
// Ignore any updates without text changes. This is important for perf since sending an update to roslyn does
47+
// a round trip for producing nothing new and causes a lot of churn in solution updates.
48+
if (e.kind == RazorDocumentChangeKind.csharpChanged && !e.document.isOpen && e.changes.length > 0) {
49+
const uriString = UriConverter.serialize(e.document.uri);
50+
const identifier = TextDocumentIdentifier.create(uriString);
51+
await vscode.commands.executeCommand(
52+
DynamicFileInfoHandler.dynamicFileUpdatedCommand,
53+
new RazorDynamicFileChangedParams(identifier)
54+
);
55+
}
56+
});
57+
}
5458
}
5559

5660
// Given Razor document URIs, returns associated generated doc URIs

src/razor/src/mapping/mappingHandler.ts

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
import * as vscode from 'vscode';
77
import { RazorLanguageServiceClient } from '../razorLanguageServiceClient';
88
import { RazorMapSpansParams } from './razorMapSpansParams';
9+
import { RazorMapTextChangesParams } from './razorMapTextChangesParams';
910

1011
export class MappingHandler {
1112
public static readonly MapSpansCommand = 'razor.mapSpansCommand';
@@ -21,7 +22,9 @@ export class MappingHandler {
2122
}),
2223
vscode.commands.registerCommand(
2324
MappingHandler.MapChangesCommand,
24-
async (params: RazorMapSpansParams) => {}
25+
async (params: RazorMapTextChangesParams) => {
26+
return this.languageServiceClient.mapTextChanges(params);
27+
}
2528
),
2629
]
2730
);

src/razor/src/mapping/mappingHelpers.ts

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,6 @@ import { RazorLogger } from '../razorLogger';
1111

1212
export class MappingHelpers {
1313
public static readonly language = 'Razor';
14-
public static readonly MapSpansCommand = 'razor.mapSpansCommand';
1514

1615
public static async remapGeneratedFileWorkspaceEdit(
1716
workspaceEdit: vscode.WorkspaceEdit,

src/razor/src/mapping/razorMapSpansResponse.ts

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,15 +3,16 @@
33
* Licensed under the MIT License. See License.txt in the project root for license information.
44
*--------------------------------------------------------------------------------------------*/
55

6-
import { Range, TextDocumentIdentifier } from 'vscode-languageserver-types';
6+
import { TextDocumentIdentifier } from 'vscode-languageserver-types';
77
import { razorTextSpan } from '../dynamicFile/razorTextSpan';
8+
import { SerializableRange } from '../rpc/serializableRange';
89

910
// matches https://github.com/dotnet/razor/blob/main/src/Razor/src/Microsoft.VisualStudioCode.RazorExtension/Services/RazorMapSpansResponse.cs
1011
export class RazorMapSpansResponse {
1112
static empty: RazorMapSpansResponse = new RazorMapSpansResponse([], [], { uri: '' });
1213

1314
constructor(
14-
public readonly ranges: Range[],
15+
public readonly ranges: SerializableRange[],
1516
public readonly spans: razorTextSpan[],
1617
public readonly razorDocument: TextDocumentIdentifier
1718
) {}

src/razor/src/mapping/razorMapTextChangesResponse.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ import { razorTextChange } from '../dynamicFile/razorTextChange';
77

88
// Matches https://github.com/dotnet/razor/blob/401f6f8632a7e0320bc12804fa7e9659b3b3aeab/src/Razor/src/Microsoft.VisualStudioCode.RazorExtension/Services/RazorMapTextChangesResponse.cs
99
export class RazorMapTextChangesResponse {
10+
static empty: RazorMapTextChangesResponse | PromiseLike<RazorMapTextChangesResponse>;
1011
constructor(
1112
public readonly razorDocument: TextDocumentIdentifier,
1213
public readonly textChanges: razorTextChange[]

0 commit comments

Comments
 (0)