Skip to content

Commit b4e8e8a

Browse files
author
Andrew Hall
committed
Add mapTextEdits
1 parent 8ec4bac commit b4e8e8a

File tree

7 files changed

+108
-4
lines changed

7 files changed

+108
-4
lines changed

src/lsptoolshost/razor/razorEndpoints.ts

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +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';
3335

3436
export function registerRazorEndpoints(
3537
context: vscode.ExtensionContext,
@@ -95,6 +97,15 @@ export function registerRazorEndpoints(
9597
registerRequestHandler<RazorMapSpansParams, RazorMapSpansResponse>('razor/mapSpans', async (params) => {
9698
return await vscode.commands.executeCommand<RazorMapSpansResponse>(MappingHandler.MapSpansCommand, params);
9799
});
100+
registerRequestHandler<RazorMapTextChangesParams, RazorMapTextChangesResponse>(
101+
'razor/mapTextChanges',
102+
async (params) => {
103+
return await vscode.commands.executeCommand<RazorMapTextChangesResponse>(
104+
MappingHandler.MapChangesCommand,
105+
params
106+
);
107+
}
108+
);
98109
}
99110

100111
// Helper method that registers a request handler, and logs errors to the Razor logger.

src/razor/src/mapping/mappingHandler.ts

Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -9,11 +9,21 @@ import { RazorMapSpansParams } from './razorMapSpansParams';
99

1010
export class MappingHandler {
1111
public static readonly MapSpansCommand = 'razor.mapSpansCommand';
12+
public static readonly MapChangesCommand = 'razor.mapChangesCommand';
13+
1214
constructor(private readonly languageServiceClient: RazorLanguageServiceClient) {}
1315

14-
public async register(): Promise<void> {
15-
vscode.commands.registerCommand(MappingHandler.MapSpansCommand, async (params: RazorMapSpansParams) => {
16-
return this.languageServiceClient.mapSpans(params);
17-
});
16+
public async register(): Promise<vscode.Disposable> {
17+
return vscode.Disposable.from(
18+
...[
19+
vscode.commands.registerCommand(MappingHandler.MapSpansCommand, async (params: RazorMapSpansParams) => {
20+
return this.languageServiceClient.mapSpans(params);
21+
}),
22+
vscode.commands.registerCommand(
23+
MappingHandler.MapChangesCommand,
24+
async (params: RazorMapSpansParams) => {}
25+
),
26+
]
27+
);
1828
}
1929
}
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
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+
import { TextDocumentIdentifier } from 'vscode-languageserver-types';
6+
import { razorTextChange } from '../dynamicFile/razorTextChange';
7+
8+
// Matches https://github.com/dotnet/razor/blob/401f6f8632a7e0320bc12804fa7e9659b3b3aeab/src/Razor/src/Microsoft.VisualStudioCode.RazorExtension/Services/RazorMapTextChangesParams.cs
9+
export class RazorMapTextChangesParams {
10+
constructor(
11+
public readonly csharpDocument: TextDocumentIdentifier,
12+
public readonly textChanges: razorTextChange[]
13+
) {}
14+
}
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
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+
import { TextDocumentIdentifier } from 'vscode-languageserver-types';
6+
import { razorTextChange } from '../dynamicFile/razorTextChange';
7+
8+
// Matches https://github.com/dotnet/razor/blob/401f6f8632a7e0320bc12804fa7e9659b3b3aeab/src/Razor/src/Microsoft.VisualStudioCode.RazorExtension/Services/RazorMapTextChangesResponse.cs
9+
export class RazorMapTextChangesResponse {
10+
constructor(
11+
public readonly razorDocument: TextDocumentIdentifier,
12+
public readonly textChanges: razorTextChange[]
13+
) {}
14+
}
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
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+
import { Uri } from 'vscode';
6+
import { LanguageKind } from 'vscode-languageserver-types';
7+
import { razorTextChange } from '../dynamicFile/razorTextChange';
8+
9+
// Matches https://github.com/dotnet/razor/blob/401f6f8632a7e0320bc12804fa7e9659b3b3aeab/src/Razor/src/Microsoft.CodeAnalysis.Razor.Workspaces/Protocol/DocumentMapping/RazorMapToDocumentEditsParams.cs
10+
export class RazorMapToDocumentEditsParams {
11+
constructor(
12+
public readonly kind: LanguageKind,
13+
public readonly razorDocumentUri: Uri,
14+
public readonly textChanges: razorTextChange[]
15+
) {}
16+
}
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
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+
import { razorTextChange } from '../dynamicFile/razorTextChange';
6+
7+
// Matches https://github.com/dotnet/razor/blob/401f6f8632a7e0320bc12804fa7e9659b3b3aeab/src/Razor/src/Microsoft.CodeAnalysis.Razor.Workspaces/Protocol/DocumentMapping/RazorMapToDocumentEditsResponse.cs
8+
export class RazorMapToDocumentEditsResponse {
9+
constructor(public readonly textChanges: razorTextChange[], public readonly hostDocumentVersion: number) {}
10+
}

src/razor/src/razorLanguageServiceClient.ts

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,10 @@ import { RazorMapSpansParams } from './mapping/razorMapSpansParams';
1515
import { RazorMapSpansResponse } from './mapping/razorMapSpansResponse';
1616
import { UriConverter } from '../../lsptoolshost/utils/uriConverter';
1717
import { RazorDocumentManager } from './document/razorDocumentManager';
18+
import { RazorMapTextChangesParams } from './mapping/RazorMapTextChangesParams';
19+
import { RazorMapTextChangesResponse } from './mapping/RazorMapTextChangesResponse';
20+
import { RazorMapToDocumentEditsParams } from './mapping/RazorMapToDocumentEditsParams';
21+
import { RazorMapToDocumentEditsResponse } from './mapping/RazorMapToDocumentEditsResponse';
1822

1923
export class RazorLanguageServiceClient {
2024
constructor(
@@ -100,6 +104,31 @@ export class RazorLanguageServiceClient {
100104
);
101105
}
102106

107+
async mapTextChanges(params: RazorMapTextChangesParams): Promise<RazorMapTextChangesResponse> {
108+
const csharpUri = UriConverter.deserialize(params.csharpDocument.uri);
109+
const document = await this.documentManager.getDocumentForCSharpUri(csharpUri);
110+
if (!document) {
111+
return RazorMapTextChangesResponse.empty;
112+
}
113+
114+
const request = new RazorMapToDocumentEditsParams(LanguageKind.CSharp, document.uri, params.textChanges);
115+
const response = await this.serverClient.sendRequest<RazorMapToDocumentEditsResponse>(
116+
'razor/mapToDocumentEdits',
117+
request
118+
);
119+
120+
if (!response) {
121+
return RazorMapTextChangesResponse.empty;
122+
}
123+
124+
return new RazorMapTextChangesResponse(
125+
{
126+
uri: UriConverter.serialize(document.uri),
127+
},
128+
response.textChanges
129+
);
130+
}
131+
103132
private async ensureStarted() {
104133
// If the server is already started this will instantly return.
105134
await this.serverClient.start();

0 commit comments

Comments
 (0)