Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow Razor to format new documents via Roslyn #6329

Merged
merged 1 commit into from
Sep 15, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 15 additions & 0 deletions src/lsptoolshost/razorCommands.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ import {
} from 'vscode-languageclient/node';
import SerializableSimplifyMethodParams from '../razor/src/simplify/serializableSimplifyMethodParams';
import { TextEdit } from 'vscode-html-languageservice';
import { SerializableFormatNewFileParams } from '../razor/src/formatNewFile/serializableFormatNewFileParams';

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

export function registerRazorCommands(context: vscode.ExtensionContext, languageServer: RoslynLanguageServer) {
Expand Down Expand Up @@ -70,6 +72,19 @@ export function registerRazorCommands(context: vscode.ExtensionContext, language
}
)
);
context.subscriptions.push(
vscode.commands.registerCommand(
roslynFormatNewFileCommand,
async (request: SerializableFormatNewFileParams) => {
const formatNewFileRequestType = new RequestType<
SerializableFormatNewFileParams,
string | undefined,
any
>('roslyn/formatNewFile');
return await languageServer.sendRequest(formatNewFileRequestType, request, CancellationToken.None);
}
)
);

// The VS Code API for code actions (and the vscode.CodeAction type) doesn't support everything that LSP supports,
// namely the data property, which Razor needs to identify which code actions are on their allow list, so we need
Expand Down
52 changes: 52 additions & 0 deletions src/razor/src/formatNewFile/razorFormatNewFileHandler.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
/*---------------------------------------------------------------------------------------------
* Copyright (c) Microsoft Corporation. All rights reserved.
* Licensed under the MIT License. See License.txt in the project root for license information.
*--------------------------------------------------------------------------------------------*/

import * as vscode from 'vscode';
import { RequestType } from 'vscode-languageclient';
import { RazorLanguageServerClient } from '../razorLanguageServerClient';
import { RazorDocumentManager } from '../document/razorDocumentManager';
import { RazorLanguageServiceClient } from '../razorLanguageServiceClient';
import { RazorLanguageFeatureBase } from '../razorLanguageFeatureBase';
import { RazorDocumentSynchronizer } from '../document/razorDocumentSynchronizer';
import { RazorLogger } from '../razorLogger';
import { SerializableFormatNewFileParams } from './serializableFormatNewFileParams';
import { roslynFormatNewFileCommand } from '../../../lsptoolshost/razorCommands';

export class RazorFormatNewFileHandler extends RazorLanguageFeatureBase {
private static readonly razorFormatNewFileCommand = 'razor/formatNewFile';
private formatNewFileRequestType: RequestType<SerializableFormatNewFileParams, string | undefined, any> =
new RequestType(RazorFormatNewFileHandler.razorFormatNewFileCommand);

constructor(
documentSynchronizer: RazorDocumentSynchronizer,
protected readonly serverClient: RazorLanguageServerClient,
protected readonly serviceClient: RazorLanguageServiceClient,
protected readonly documentManager: RazorDocumentManager,
protected readonly logger: RazorLogger
) {
super(documentSynchronizer, documentManager, serviceClient, logger);
}

public async register() {
await this.serverClient.onRequestWithParams<SerializableFormatNewFileParams, string | undefined, any>(
this.formatNewFileRequestType,
async (request: SerializableFormatNewFileParams, token: vscode.CancellationToken) =>
this.getFormatNewFile(request, token)
);
}

private async getFormatNewFile(
request: SerializableFormatNewFileParams,
_: vscode.CancellationToken
): Promise<string | undefined> {
if (!this.documentManager.roslynActivated) {
return undefined;
}

const response: string | undefined = await vscode.commands.executeCommand(roslynFormatNewFileCommand, request);

return response;
}
}
12 changes: 12 additions & 0 deletions src/razor/src/formatNewFile/serializableFormatNewFileParams.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
/*---------------------------------------------------------------------------------------------
* Copyright (c) Microsoft Corporation. All rights reserved.
* Licensed under the MIT License. See License.txt in the project root for license information.
*--------------------------------------------------------------------------------------------*/

import { SerializableTextDocumentIdentifier } from '../rpc/serializableTextDocumentIdentifier';

export interface SerializableFormatNewFileParams {
project: SerializableTextDocumentIdentifier;
document: SerializableTextDocumentIdentifier;
contents: string;
}