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

Fix Razor formatting bug #6236

Merged
Merged
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
16 changes: 14 additions & 2 deletions src/razor/src/formatting/formattingHandler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ export class FormattingHandler {
const textDocument = await vscode.workspace.openTextDocument(razorDocumentUri);
const synchronized = await this.documentSynchronizer.trySynchronizeProjectedDocument(
textDocument,
razorDocument.csharpDocument,
razorDocument.htmlDocument,
formattingParams.hostDocumentVersion,
cancellationToken
);
Expand All @@ -70,9 +70,21 @@ export class FormattingHandler {

const virtualHtmlUri = razorDocument.htmlDocument.uri;

// This is a workaround for https://github.com/microsoft/vscode/issues/191395.
// We need to call the HTML range formatter instead of document formattter since
// the latter does not respect HTML settings.
const htmlDocContent = razorDocument.htmlDocument.getContent();
const zeroBasedNumLinesHtmlDoc = this.countLines(htmlDocContent);
const lastLineLengthHtmlDoc = this.getLastLineLength(htmlDocContent);
const range = new vscode.Range(
new vscode.Position(0, 0),
new vscode.Position(zeroBasedNumLinesHtmlDoc, lastLineLengthHtmlDoc)
);

const textEdits = await vscode.commands.executeCommand<vscode.TextEdit[]>(
'vscode.executeFormatDocumentProvider',
'vscode.executeFormatRangeProvider',
virtualHtmlUri,
range,
formattingParams.options
);

Expand Down