Skip to content

Commit d0250e8

Browse files
committed
Allow Razor to format new documents via Roslyn
1 parent b63245d commit d0250e8

File tree

3 files changed

+79
-0
lines changed

3 files changed

+79
-0
lines changed

src/lsptoolshost/razorCommands.ts

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ import {
2626
} from 'vscode-languageclient/node';
2727
import SerializableSimplifyMethodParams from '../razor/src/simplify/serializableSimplifyMethodParams';
2828
import { TextEdit } from 'vscode-html-languageservice';
29+
import { SerializableFormatNewFileParams } from '../razor/src/formatNewFile/serializableFormatNewFileParams';
2930

3031
// These are commands that are invoked by the Razor extension, and are used to send LSP requests to the Roslyn LSP server
3132
export const roslynDidChangeCommand = 'roslyn.changeRazorCSharp';
@@ -36,6 +37,7 @@ export const resolveCodeActionCommand = 'roslyn.resolveCodeAction';
3637
export const provideCompletionsCommand = 'roslyn.provideCompletions';
3738
export const resolveCompletionsCommand = 'roslyn.resolveCompletion';
3839
export const roslynSimplifyMethodCommand = 'roslyn.simplifyMethod';
40+
export const roslynFormatNewFileCommand = 'roslyn.formatNewFile';
3941
export const razorInitializeCommand = 'razor.initialize';
4042

4143
export function registerRazorCommands(context: vscode.ExtensionContext, languageServer: RoslynLanguageServer) {
@@ -70,6 +72,19 @@ export function registerRazorCommands(context: vscode.ExtensionContext, language
7072
}
7173
)
7274
);
75+
context.subscriptions.push(
76+
vscode.commands.registerCommand(
77+
roslynFormatNewFileCommand,
78+
async (request: SerializableFormatNewFileParams) => {
79+
const formatNewFileRequestType = new RequestType<
80+
SerializableFormatNewFileParams,
81+
string | undefined,
82+
any
83+
>('roslyn/formatNewFile');
84+
return await languageServer.sendRequest(formatNewFileRequestType, request, CancellationToken.None);
85+
}
86+
)
87+
);
7388

7489
// The VS Code API for code actions (and the vscode.CodeAction type) doesn't support everything that LSP supports,
7590
// namely the data property, which Razor needs to identify which code actions are on their allow list, so we need
Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
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+
}
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
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 { SerializableTextDocumentIdentifier } from '../rpc/serializableTextDocumentIdentifier';
7+
8+
export interface SerializableFormatNewFileParams {
9+
project: SerializableTextDocumentIdentifier;
10+
document: SerializableTextDocumentIdentifier;
11+
contents: string;
12+
}

0 commit comments

Comments
 (0)