From 7375b251d94465fdaa0ee85345e54a06877aa249 Mon Sep 17 00:00:00 2001 From: Allison Chou Date: Sat, 26 Aug 2023 15:13:59 -0700 Subject: [PATCH] Fix Razor formatting bug --- src/razor/src/formatting/formattingHandler.ts | 16 ++++++++++++++-- 1 file changed, 14 insertions(+), 2 deletions(-) diff --git a/src/razor/src/formatting/formattingHandler.ts b/src/razor/src/formatting/formattingHandler.ts index c6d4474f3..7d2fea028 100644 --- a/src/razor/src/formatting/formattingHandler.ts +++ b/src/razor/src/formatting/formattingHandler.ts @@ -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 ); @@ -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.executeFormatDocumentProvider', + 'vscode.executeFormatRangeProvider', virtualHtmlUri, + range, formattingParams.options );