Skip to content

Commit 7abb2ad

Browse files
authored
Normalize line endings in on type formatting (#12398)
Fixes #12395 Fixes #10836 This just brings the fix from #10802 to on type formatting. I was surprised it was that easy, but I suspect the simplification due to only having one formatting engine might have helped. Extra evidence for this is that all of the other tests we had that skipped LF endings passed without any effort on my behalf really.
2 parents 0d4d79b + ddc4eec commit 7abb2ad

File tree

9 files changed

+86
-65
lines changed

9 files changed

+86
-65
lines changed

src/Razor/src/Microsoft.CodeAnalysis.Razor.Workspaces/Extensions/SourceTextExtensions.cs

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -341,13 +341,12 @@ public static bool HasLFLineEndings(this SourceText text)
341341

342342
foreach (var line in text.Lines)
343343
{
344-
var lineBreakSpan = TextSpan.FromBounds(line.End, line.EndIncludingLineBreak);
345-
var lineBreak = line.Text?.ToString(lineBreakSpan) ?? string.Empty;
346-
if (lineBreak == "\r\n")
344+
var lineBreakLength = line.EndIncludingLineBreak - line.End;
345+
if (lineBreakLength == 2)
347346
{
348347
crlfCount++;
349348
}
350-
else if (lineBreak == "\n")
349+
else if (lineBreakLength != 0)
351350
{
352351
lfCount++;
353352
}

src/Razor/src/Microsoft.CodeAnalysis.Razor.Workspaces/Formatting/RazorFormattingService.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -300,6 +300,7 @@ private async Task<ImmutableArray<TextChange>> ApplyFormattedChangesAsync(
300300

301301
var result = await formattingPass.ExecuteAsync(context, generatedDocumentChanges, cancellationToken).ConfigureAwait(false);
302302
var originalText = context.SourceText;
303+
result = NormalizeLineEndings(originalText, result);
303304
var razorChanges = originalText.MinimizeTextChanges(result);
304305

305306
if (validate)

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ namespace Microsoft.AspNetCore.Razor.LanguageServer.Formatting;
1515
public class CodeDirectiveFormattingTest(FormattingTestContext context, HtmlFormattingFixture fixture, ITestOutputHelper testOutput)
1616
: FormattingTestBase(context, fixture.Service, testOutput), IClassFixture<FormattingTestContext>
1717
{
18-
[FormattingTestFact(SkipFlipLineEnding = true)]
18+
[FormattingTestFact]
1919
[WorkItem("https://github.com/dotnet/razor-tooling/issues/5648")]
2020
public async Task GenericComponentWithCascadingTypeParameter()
2121
{
@@ -123,7 +123,7 @@ await RunFormattingTestAsync(
123123
tagHelpers: GetComponentWithCascadingTypeParameter());
124124
}
125125

126-
[FormattingTestFact(SkipFlipLineEnding = true)] // tracked by https://github.com/dotnet/razor/issues/10836
126+
[FormattingTestFact]
127127
[WorkItem("https://github.com/dotnet/razor-tooling/issues/5648")]
128128
public async Task GenericComponentWithCascadingTypeParameter_MultipleParameters()
129129
{

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

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1872,14 +1872,18 @@ void Method() { }
18721872
""");
18731873
}
18741874

1875-
[FormattingTestFact(SkipFlipLineEnding = true)] // tracked by https://github.com/dotnet/razor/issues/10836
1875+
[FormattingTestFact]
18761876
public async Task FormatsShortBlock()
18771877
{
18781878
await RunFormattingTestAsync(
18791879
input: """
1880+
<div>
1881+
</div>
18801882
@{<p></p>}
18811883
""",
18821884
expected: """
1885+
<div>
1886+
</div>
18831887
@{
18841888
<p></p>
18851889
}

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ namespace Microsoft.AspNetCore.Razor.LanguageServer.Formatting;
1616
public class HtmlFormattingTest(FormattingTestContext context, HtmlFormattingFixture fixture, ITestOutputHelper testOutput)
1717
: FormattingTestBase(context, fixture.Service, testOutput), IClassFixture<FormattingTestContext>
1818
{
19-
[FormattingTestFact(SkipFlipLineEnding = true)] // tracked by https://github.com/dotnet/razor/issues/10836
19+
[FormattingTestFact]
2020
public async Task FormatsComponentTags()
2121
{
2222
var tagHelpers = GetComponents();
@@ -363,7 +363,7 @@ await RunFormattingTestAsync(
363363
tagHelpers: GetComponents());
364364
}
365365

366-
[FormattingTestFact(SkipFlipLineEnding = true)] // tracked by https://github.com/dotnet/razor/issues/10836
366+
[FormattingTestFact]
367367
[WorkItem("https://github.com/dotnet/razor/issues/6211")]
368368
public async Task FormatCascadingValueWithCascadingTypeParameter()
369369
{

0 commit comments

Comments
 (0)