Skip to content

Commit a068170

Browse files
Fix #10891 - Formatting does not respect indentation within Razor comment blocks (#10893)
Altered `HtmlFormattingPassBase` to discard any `TextChange`s that start within a `RazorCommentBlockSyntax` and added a test to catch any regressions in the future. Fixes #10891
2 parents 4467256 + dda1b20 commit a068170

File tree

2 files changed

+49
-1
lines changed

2 files changed

+49
-1
lines changed

src/Razor/src/Microsoft.CodeAnalysis.Razor.Workspaces/Formatting/Passes/HtmlFormattingPassBase.cs

Lines changed: 24 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,9 @@ public virtual async Task<ImmutableArray<TextChange>> ExecuteAsync(FormattingCon
3030

3131
if (changes.Length > 0)
3232
{
33-
changedText = originalText.WithChanges(changes);
33+
var filteredChanges = FilterIncomingChanges(changedContext, changes);
34+
35+
changedText = originalText.WithChanges(filteredChanges);
3436
// Create a new formatting context for the changed razor document.
3537
changedContext = await context.WithTextAsync(changedText).ConfigureAwait(false);
3638

@@ -48,6 +50,27 @@ public virtual async Task<ImmutableArray<TextChange>> ExecuteAsync(FormattingCon
4850
return changedText.GetTextChangesArray(originalText);
4951
}
5052

53+
private static ImmutableArray<TextChange> FilterIncomingChanges(FormattingContext context, ImmutableArray<TextChange> changes)
54+
{
55+
var syntaxTree = context.CodeDocument.GetSyntaxTree();
56+
57+
using var changesToKeep = new PooledArrayBuilder<TextChange>(capacity: changes.Length);
58+
59+
foreach (var change in changes)
60+
{
61+
// Don't keep changes that start inside of a razor comment block.
62+
var comment = syntaxTree.Root.FindInnermostNode(change.Span.Start)?.FirstAncestorOrSelf<RazorCommentBlockSyntax>();
63+
if (comment is not null)
64+
{
65+
continue;
66+
}
67+
68+
changesToKeep.Add(change);
69+
}
70+
71+
return changesToKeep.DrainToImmutable();
72+
}
73+
5174
private static ImmutableArray<TextChange> AdjustRazorIndentation(FormattingContext context)
5275
{
5376
// Assume HTML formatter has already run at this point and HTML is relatively indented correctly.

src/Razor/test/Microsoft.AspNetCore.Razor.LanguageServer.Test/Formatting_NetFx/RazorFormattingTest.cs

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -705,6 +705,31 @@ await RunFormattingTestAsync(
705705
fileKind: FileKinds.Legacy);
706706
}
707707

708+
[Fact]
709+
public async Task MultiLineComment_WithinHtml ()
710+
{
711+
await RunFormattingTestAsync(
712+
input: """
713+
<div>
714+
@* <div>
715+
This comment's opening at-star will be aligned, and the
716+
indentation of the rest of its lines will be preserved.
717+
</div>
718+
*@
719+
</div>
720+
""",
721+
expected: """
722+
<div>
723+
@* <div>
724+
This comment's opening at-star will be aligned, and the
725+
indentation of the rest of its lines will be preserved.
726+
</div>
727+
*@
728+
</div>
729+
""",
730+
fileKind: FileKinds.Legacy);
731+
}
732+
708733
// Regression prevention tests:
709734
[Fact]
710735
public async Task Using()

0 commit comments

Comments
 (0)