Skip to content

Fix nested script indentation in Razor - #85289

Merged
davidwengier merged 4 commits into
dotnet:mainfrom
davidwengier:fix-nested-script-indentation
Sep 15, 2026
Merged

davidwengier merged 4 commits into
dotnet:mainfrom
davidwengier:fix-nested-script-indentation

Conversation

@davidwengier

@davidwengier davidwengier commented Sep 14, 2026 •

Copy link
Copy Markdown
Member

Fixes #85297

We had a bad assumption when combining html indentation from script blocks with razor/c# indentation for the same area. The fix is pretty simple, and I tried to create reasonable exhaustive tests so hopefully we won't regress this in future.

Microsoft Reviewers: Open in CodeFlow

@davidwengier
davidwengier requested review from a team and a lite review from Copilot September 14, 2026 08:11
@azure-pipelines

Copy link
Copy Markdown
Azure Pipelines:
Successfully started running 2 pipeline(s).
There may be pipelines that require an authorized user to comment /azp run to run.

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🟡 Changes recommended

An unresolved moderate indentation issue remains, along with a requested nested style regression test.

Get a fresh assessment by requesting another Copilot review.

Pull request overview

Fixes over-indentation of nested Razor script blocks by correcting HTML/C# indentation reconciliation.

Changes:

  • Tracks enclosing HTML indentation levels.
  • Adds regression coverage for nested script formatting.

Outstanding feedback includes a moderate indentation issue and a requested nested style test.

File summaries
File Summary
src/Razor/src/Razor/src/Microsoft.CodeAnalysis.Remote.Razor/Formatting/Passes/CSharpFormattingPass.CSharpDocumentGenerator.cs Corrects indentation mapping.
src/Razor/src/Razor/test/Microsoft.CodeAnalysis.Razor.CohostingShared.UnitTests/Formatting/DocumentFormattingTest.cs Adds formatting regression tests.
Review details

Suppressed comments (1)

src/Razor/src/Razor/src/Microsoft.CodeAnalysis.Remote.Razor/Formatting/Passes/CSharpFormattingPass.CSharpDocumentGenerator.cs:706

  • ElementCausesIndentation is not equivalent to an emitted structural brace here: VisitStartTag returns before calling it for significant-whitespace elements such as <pre> and <textarea>. Therefore a multiline <pre> containing a multiline <script> or <style> is counted as an extra HTML level even though no brace was emitted for the <pre>, and the subtraction in CreateLineInfo removes one level too many. Exclude significant-whitespace ancestors, or otherwise count only elements that emit EmitOpenBraceLine.
                    if (node is BaseMarkupElementSyntax { StartTag: { } ancestorStartTag } &&
                        ElementCausesIndentation(ancestorStartTag))
  • Files reviewed: 2/2 changed files
  • Comments generated: 1
  • Review effort level: Lite

Copilot AI review requested due to automatic review settings September 14, 2026 08:34

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🔵 Needs a closer look

One or more issues must be addressed before approval.

Review details

Suppressed comments (1)

src/Razor/src/Razor/src/Microsoft.CodeAnalysis.Remote.Razor/Formatting/Passes/CSharpFormattingPass.CSharpDocumentGenerator.cs:706

  • ElementCausesIndentation does not imply that a structural brace was emitted for every ancestor here. VisitStartTag returns from the ElementHasSignificantWhitespace branch before calling it for <pre> and <textarea> (lines 654-663), so a multiline significant-whitespace ancestor that contains a multiline <script>/<style> is counted here even though it contributes no generated C# indentation level. CreateLineInfo then subtracts one level too many and can under-indent the embedded content; exclude significant-whitespace ancestors or otherwise count only ancestors that emit EmitOpenBraceLine.
                    if (node is BaseMarkupElementSyntax { StartTag: { } ancestorStartTag } &&
                        ElementCausesIndentation(ancestorStartTag))
  • Files reviewed: 2/2 changed files
  • Comments generated: 0 new
  • Review effort level: Lite

@davidwengier
davidwengier merged commit 13c72d2 into dotnet:main Sep 15, 2026
23 checks passed
@davidwengier
davidwengier deleted the fix-nested-script-indentation branch September 15, 2026 02:07
@dotnet-policy-service dotnet-policy-service Bot added this to the Next milestone Sep 15, 2026
@jjonescz jjonescz modified the milestones: Next, 18.12 Sep 29, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Visual Studio .cshtml formatter over-indents nested script blocks

4 participants