From e97ad73964793bc9d8ab25f45f1e164e781bb745 Mon Sep 17 00:00:00 2001 From: Jordi Ramos Date: Tue, 27 Aug 2024 12:02:48 -0700 Subject: [PATCH 01/11] Implemented a line normalization function that prevents the language server from sending /r to LF line ending docs --- .../Formatting/RazorFormattingService.cs | 44 ++++++++++++++++++- 1 file changed, 43 insertions(+), 1 deletion(-) diff --git a/src/Razor/src/Microsoft.CodeAnalysis.Razor.Workspaces/Formatting/RazorFormattingService.cs b/src/Razor/src/Microsoft.CodeAnalysis.Razor.Workspaces/Formatting/RazorFormattingService.cs index 1b14b3fa013..209cbfe5ccd 100644 --- a/src/Razor/src/Microsoft.CodeAnalysis.Razor.Workspaces/Formatting/RazorFormattingService.cs +++ b/src/Razor/src/Microsoft.CodeAnalysis.Razor.Workspaces/Formatting/RazorFormattingService.cs @@ -83,8 +83,10 @@ public async Task FormatAsync( var filteredEdits = range is null ? result.Edits : result.Edits.Where(e => range.LineOverlapsWith(e.Range)); + var minimalEdits = GetMinimalEdits(originalText, filteredEdits); + minimalEdits = NormalizeLineEndings(originalText, minimalEdits); - return GetMinimalEdits(originalText, filteredEdits); + return minimalEdits; } private static TextEdit[] GetMinimalEdits(SourceText originalText, IEnumerable filteredEdits) @@ -240,4 +242,44 @@ private static void UnwrapCSharpSnippets(TextEdit[] snippetEdits) snippetEdit.NewText = unwrappedText; } } + + // This method counts the occurrences of CRLF and LF line endings in the original text. If LF line endings are more + // prevalent, it removes any standalone CR characters from the text edits to ensure consistency with the LF style. + // This can be removed once we figure out how to get proper line ending information from the client. + private TextEdit[] NormalizeLineEndings(SourceText originalText, TextEdit[] minimalEdits) + { + var (crlfCount, lfCount) = CountLineEndings(originalText); + if (lfCount > crlfCount) + { + minimalEdits = minimalEdits + .Where(edit => edit.NewText != "\r") + .ToArray(); + } + + return minimalEdits; + } + + private (int crlfCount, int lfCount) CountLineEndings(SourceText sourceText) + { + var crlfCount = 0; + var lfCount = 0; + + for (var i = 0; i < sourceText.Length; i++) + { + if (sourceText[i] == '\r') + { + if (i + 1 < sourceText.Length && sourceText[i + 1] == '\n') + { + crlfCount++; + i++; // Skip the next character as it's part of the CRLF sequence + } + } + else if (sourceText[i] == '\n') + { + lfCount++; + } + } + + return (crlfCount, lfCount); + } } From 7cca6bda0af37153f627461384a2d26e1d4aed66 Mon Sep 17 00:00:00 2001 From: Jordi Ramos Date: Wed, 28 Aug 2024 20:59:01 -0700 Subject: [PATCH 02/11] improved the line ending normalization where we only delete the \r and keep everything else intact in the text edit. Added test cases --- .../Formatting/RazorFormattingService.cs | 6 +- .../CodeDirectiveFormattingTest.cs | 96 +++++++++++++++++++ 2 files changed, 101 insertions(+), 1 deletion(-) diff --git a/src/Razor/src/Microsoft.CodeAnalysis.Razor.Workspaces/Formatting/RazorFormattingService.cs b/src/Razor/src/Microsoft.CodeAnalysis.Razor.Workspaces/Formatting/RazorFormattingService.cs index 209cbfe5ccd..42a52db4f0c 100644 --- a/src/Razor/src/Microsoft.CodeAnalysis.Razor.Workspaces/Formatting/RazorFormattingService.cs +++ b/src/Razor/src/Microsoft.CodeAnalysis.Razor.Workspaces/Formatting/RazorFormattingService.cs @@ -252,7 +252,11 @@ private TextEdit[] NormalizeLineEndings(SourceText originalText, TextEdit[] mini if (lfCount > crlfCount) { minimalEdits = minimalEdits - .Where(edit => edit.NewText != "\r") + .Select(edit => + { + edit.NewText = edit.NewText.Replace("\r", string.Empty); + return edit; + }) .ToArray(); } diff --git a/src/Razor/test/Microsoft.AspNetCore.Razor.LanguageServer.Test/Formatting_NetFx/CodeDirectiveFormattingTest.cs b/src/Razor/test/Microsoft.AspNetCore.Razor.LanguageServer.Test/Formatting_NetFx/CodeDirectiveFormattingTest.cs index 80589eb18ff..6cb824b692b 100644 --- a/src/Razor/test/Microsoft.AspNetCore.Razor.LanguageServer.Test/Formatting_NetFx/CodeDirectiveFormattingTest.cs +++ b/src/Razor/test/Microsoft.AspNetCore.Razor.LanguageServer.Test/Formatting_NetFx/CodeDirectiveFormattingTest.cs @@ -37,6 +37,44 @@ public interface Bar """); } + [Fact] + public async Task FormatsCodeBlockDirectiveWithCRLFLineEnding() + { + await RunFormattingTestAsync( + input: + "@code {\r\n" + + " public class Foo{}\r\n" + + " public interface Bar {\r\n" + + "}\r\n" + + "}", + expected: + "@code {\r\n" + + " public class Foo { }\r\n" + + " public interface Bar\r\n" + + " {\r\n" + + " }\r\n" + + "}"); + } + + [Fact] + public async Task FormatsCodeBlockDirectiveWithLFLineEnding() + { + await RunFormattingTestAsync( + input: + "@code {\n" + + " public class Foo{}\n" + + " public interface Bar {\n" + + "}\n" + + "}", + expected: + "@code {\n" + + " public class Foo { }\n" + + " public interface Bar\n" + + " {\n" + + " }\n" + + "}"); + } + [Fact] public async Task FormatCSharpInsideHtmlTag() { @@ -149,6 +187,64 @@ void Method() """); } + [Fact] + public async Task Formats_MultipleBlocksInADirectiveWithCRLFLineEnding() + { + await RunFormattingTestAsync( + input: + "@{\r\n" + + "void Method(){\r\n" + + "var x = \"foo\";\r\n" + + "@(DateTime.Now)\r\n" + + "

\r\n" + + "var y= \"fooo\";\r\n" + + "}\r\n" + + "}\r\n" + + "
\r\n" + + "
", + expected: + "@{\r\n" + + " void Method()\r\n" + + " {\r\n" + + " var x = \"foo\";\r\n" + + " @(DateTime.Now)\r\n" + + "

\r\n" + + " var y = \"fooo\";\r\n" + + " }\r\n" + + "}\r\n" + + "
\r\n" + + "
"); + } + + [Fact] + public async Task Formats_MultipleBlocksInADirectiveWithLFLineEnding() + { + await RunFormattingTestAsync( + input: + "@{\n" + + "void Method(){\n" + + "var x = \"foo\";\n" + + "@(DateTime.Now)\n" + + "

\n" + + "var y= \"fooo\";\n" + + "}\n" + + "}\n" + + "
\n" + + "
", + expected: + "@{\n" + + " void Method()\n" + + " {\n" + + " var x = \"foo\";\n" + + " @(DateTime.Now)\n" + + "

\n" + + " var y = \"fooo\";\n" + + " }\n" + + "}\n" + + "
\n" + + "
"); + } + [Fact] public async Task Formats_NonCodeBlockDirectives() { From edea7feb26bd836cbdeea3ffe19e4255434a00aa Mon Sep 17 00:00:00 2001 From: Jordi Ramos Date: Wed, 28 Aug 2024 21:30:03 -0700 Subject: [PATCH 03/11] changed NormalizeLineEndings documentation to reflect the change in implementation --- .../Formatting/RazorFormattingService.cs | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/src/Razor/src/Microsoft.CodeAnalysis.Razor.Workspaces/Formatting/RazorFormattingService.cs b/src/Razor/src/Microsoft.CodeAnalysis.Razor.Workspaces/Formatting/RazorFormattingService.cs index 42a52db4f0c..28b63e9b6ce 100644 --- a/src/Razor/src/Microsoft.CodeAnalysis.Razor.Workspaces/Formatting/RazorFormattingService.cs +++ b/src/Razor/src/Microsoft.CodeAnalysis.Razor.Workspaces/Formatting/RazorFormattingService.cs @@ -197,7 +197,6 @@ internal static TextEdit MergeEdits(TextEdit[] edits, SourceText sourceText) { return edits[0]; } - var textChanges = new List(); foreach (var edit in edits) { @@ -244,7 +243,7 @@ private static void UnwrapCSharpSnippets(TextEdit[] snippetEdits) } // This method counts the occurrences of CRLF and LF line endings in the original text. If LF line endings are more - // prevalent, it removes any standalone CR characters from the text edits to ensure consistency with the LF style. + // prevalent, it removes any CR characters from the text edits to ensure consistency with the LF style. // This can be removed once we figure out how to get proper line ending information from the client. private TextEdit[] NormalizeLineEndings(SourceText originalText, TextEdit[] minimalEdits) { From 1c7e136915fef20410ff952a8fc6f86d723995f1 Mon Sep 17 00:00:00 2001 From: Jordi Ramos Date: Thu, 29 Aug 2024 11:28:09 -0700 Subject: [PATCH 04/11] Iterate through text.Lines to count line ending. Moved HasLFLineEndings to SourceTextExtensions --- .../Extensions/SourceTextExtensions.cs | 29 ++++++++++++++++ .../Formatting/RazorFormattingService.cs | 33 +++---------------- 2 files changed, 33 insertions(+), 29 deletions(-) diff --git a/src/Razor/src/Microsoft.CodeAnalysis.Razor.Workspaces/Extensions/SourceTextExtensions.cs b/src/Razor/src/Microsoft.CodeAnalysis.Razor.Workspaces/Extensions/SourceTextExtensions.cs index 6d72290f117..cf85d07a821 100644 --- a/src/Razor/src/Microsoft.CodeAnalysis.Razor.Workspaces/Extensions/SourceTextExtensions.cs +++ b/src/Razor/src/Microsoft.CodeAnalysis.Razor.Workspaces/Extensions/SourceTextExtensions.cs @@ -269,4 +269,33 @@ public static bool TryGetSourceLocation(this SourceText text, int line, int char location = default; return false; } + + /// + /// Determines if the given has more LF line endings ('\n') than CRLF line endings ('\r\n'). + /// + /// The to examine. + /// + /// true if the is deemed to use LF line endings; otherwise, false. + /// + public static bool HasLFLineEndings(this SourceText text) + { + var crlfCount = 0; + var lfCount = 0; + + foreach (var line in text.Lines) + { + var lineBreakSpan = TextSpan.FromBounds(line.End, line.EndIncludingLineBreak); + var lineBreak = line.Text?.ToString(lineBreakSpan) ?? string.Empty; + if (lineBreak == "\r\n") + { + crlfCount++; + } + else if (lineBreak == "\n") + { + lfCount++; + } + } + + return lfCount > crlfCount; + } } diff --git a/src/Razor/src/Microsoft.CodeAnalysis.Razor.Workspaces/Formatting/RazorFormattingService.cs b/src/Razor/src/Microsoft.CodeAnalysis.Razor.Workspaces/Formatting/RazorFormattingService.cs index 28b63e9b6ce..e610515fef1 100644 --- a/src/Razor/src/Microsoft.CodeAnalysis.Razor.Workspaces/Formatting/RazorFormattingService.cs +++ b/src/Razor/src/Microsoft.CodeAnalysis.Razor.Workspaces/Formatting/RazorFormattingService.cs @@ -83,8 +83,8 @@ public async Task FormatAsync( var filteredEdits = range is null ? result.Edits : result.Edits.Where(e => range.LineOverlapsWith(e.Range)); + filteredEdits = NormalizeLineEndings(originalText, filteredEdits); var minimalEdits = GetMinimalEdits(originalText, filteredEdits); - minimalEdits = NormalizeLineEndings(originalText, minimalEdits); return minimalEdits; } @@ -244,11 +244,10 @@ private static void UnwrapCSharpSnippets(TextEdit[] snippetEdits) // This method counts the occurrences of CRLF and LF line endings in the original text. If LF line endings are more // prevalent, it removes any CR characters from the text edits to ensure consistency with the LF style. - // This can be removed once we figure out how to get proper line ending information from the client. - private TextEdit[] NormalizeLineEndings(SourceText originalText, TextEdit[] minimalEdits) + // This can be removed once we figure out how to generate the formatted CSharp file with the correct line endings. + private IEnumerable NormalizeLineEndings(SourceText originalText, IEnumerable minimalEdits) { - var (crlfCount, lfCount) = CountLineEndings(originalText); - if (lfCount > crlfCount) + if (originalText.HasLFLineEndings()) { minimalEdits = minimalEdits .Select(edit => @@ -261,28 +260,4 @@ private TextEdit[] NormalizeLineEndings(SourceText originalText, TextEdit[] mini return minimalEdits; } - - private (int crlfCount, int lfCount) CountLineEndings(SourceText sourceText) - { - var crlfCount = 0; - var lfCount = 0; - - for (var i = 0; i < sourceText.Length; i++) - { - if (sourceText[i] == '\r') - { - if (i + 1 < sourceText.Length && sourceText[i + 1] == '\n') - { - crlfCount++; - i++; // Skip the next character as it's part of the CRLF sequence - } - } - else if (sourceText[i] == '\n') - { - lfCount++; - } - } - - return (crlfCount, lfCount); - } } From 0ef7de5de80e525fa67111128233ffb689fcb6ad Mon Sep 17 00:00:00 2001 From: Jordi Ramos Date: Thu, 29 Aug 2024 11:36:15 -0700 Subject: [PATCH 05/11] changing the TextEdit in place instead of creating a copy --- .../Formatting/RazorFormattingService.cs | 11 ++++------- 1 file changed, 4 insertions(+), 7 deletions(-) diff --git a/src/Razor/src/Microsoft.CodeAnalysis.Razor.Workspaces/Formatting/RazorFormattingService.cs b/src/Razor/src/Microsoft.CodeAnalysis.Razor.Workspaces/Formatting/RazorFormattingService.cs index e610515fef1..75624af2e70 100644 --- a/src/Razor/src/Microsoft.CodeAnalysis.Razor.Workspaces/Formatting/RazorFormattingService.cs +++ b/src/Razor/src/Microsoft.CodeAnalysis.Razor.Workspaces/Formatting/RazorFormattingService.cs @@ -249,13 +249,10 @@ private IEnumerable NormalizeLineEndings(SourceText originalText, IEnu { if (originalText.HasLFLineEndings()) { - minimalEdits = minimalEdits - .Select(edit => - { - edit.NewText = edit.NewText.Replace("\r", string.Empty); - return edit; - }) - .ToArray(); + foreach (var edit in minimalEdits) + { + edit.NewText = edit.NewText.Replace("\r", ""); + } } return minimalEdits; From 7e8e056a30a19298996790dd85f08afc0ca63281 Mon Sep 17 00:00:00 2001 From: Jordi Ramos Date: Fri, 30 Aug 2024 12:48:12 -0700 Subject: [PATCH 06/11] changed var name and remove hard-coded LF format tests --- .../Formatting/RazorFormattingService.cs | 6 +- .../CodeDirectiveFormattingTest.cs | 96 ------------------- 2 files changed, 3 insertions(+), 99 deletions(-) diff --git a/src/Razor/src/Microsoft.CodeAnalysis.Razor.Workspaces/Formatting/RazorFormattingService.cs b/src/Razor/src/Microsoft.CodeAnalysis.Razor.Workspaces/Formatting/RazorFormattingService.cs index 75624af2e70..c6bde55ec01 100644 --- a/src/Razor/src/Microsoft.CodeAnalysis.Razor.Workspaces/Formatting/RazorFormattingService.cs +++ b/src/Razor/src/Microsoft.CodeAnalysis.Razor.Workspaces/Formatting/RazorFormattingService.cs @@ -245,16 +245,16 @@ private static void UnwrapCSharpSnippets(TextEdit[] snippetEdits) // This method counts the occurrences of CRLF and LF line endings in the original text. If LF line endings are more // prevalent, it removes any CR characters from the text edits to ensure consistency with the LF style. // This can be removed once we figure out how to generate the formatted CSharp file with the correct line endings. - private IEnumerable NormalizeLineEndings(SourceText originalText, IEnumerable minimalEdits) + private IEnumerable NormalizeLineEndings(SourceText originalText, IEnumerable edits) { if (originalText.HasLFLineEndings()) { - foreach (var edit in minimalEdits) + foreach (var edit in edits) { edit.NewText = edit.NewText.Replace("\r", ""); } } - return minimalEdits; + return edits; } } diff --git a/src/Razor/test/Microsoft.AspNetCore.Razor.LanguageServer.Test/Formatting_NetFx/CodeDirectiveFormattingTest.cs b/src/Razor/test/Microsoft.AspNetCore.Razor.LanguageServer.Test/Formatting_NetFx/CodeDirectiveFormattingTest.cs index 6cb824b692b..80589eb18ff 100644 --- a/src/Razor/test/Microsoft.AspNetCore.Razor.LanguageServer.Test/Formatting_NetFx/CodeDirectiveFormattingTest.cs +++ b/src/Razor/test/Microsoft.AspNetCore.Razor.LanguageServer.Test/Formatting_NetFx/CodeDirectiveFormattingTest.cs @@ -37,44 +37,6 @@ public interface Bar """); } - [Fact] - public async Task FormatsCodeBlockDirectiveWithCRLFLineEnding() - { - await RunFormattingTestAsync( - input: - "@code {\r\n" + - " public class Foo{}\r\n" + - " public interface Bar {\r\n" + - "}\r\n" + - "}", - expected: - "@code {\r\n" + - " public class Foo { }\r\n" + - " public interface Bar\r\n" + - " {\r\n" + - " }\r\n" + - "}"); - } - - [Fact] - public async Task FormatsCodeBlockDirectiveWithLFLineEnding() - { - await RunFormattingTestAsync( - input: - "@code {\n" + - " public class Foo{}\n" + - " public interface Bar {\n" + - "}\n" + - "}", - expected: - "@code {\n" + - " public class Foo { }\n" + - " public interface Bar\n" + - " {\n" + - " }\n" + - "}"); - } - [Fact] public async Task FormatCSharpInsideHtmlTag() { @@ -187,64 +149,6 @@ void Method() """); } - [Fact] - public async Task Formats_MultipleBlocksInADirectiveWithCRLFLineEnding() - { - await RunFormattingTestAsync( - input: - "@{\r\n" + - "void Method(){\r\n" + - "var x = \"foo\";\r\n" + - "@(DateTime.Now)\r\n" + - "

\r\n" + - "var y= \"fooo\";\r\n" + - "}\r\n" + - "}\r\n" + - "
\r\n" + - "
", - expected: - "@{\r\n" + - " void Method()\r\n" + - " {\r\n" + - " var x = \"foo\";\r\n" + - " @(DateTime.Now)\r\n" + - "

\r\n" + - " var y = \"fooo\";\r\n" + - " }\r\n" + - "}\r\n" + - "
\r\n" + - "
"); - } - - [Fact] - public async Task Formats_MultipleBlocksInADirectiveWithLFLineEnding() - { - await RunFormattingTestAsync( - input: - "@{\n" + - "void Method(){\n" + - "var x = \"foo\";\n" + - "@(DateTime.Now)\n" + - "

\n" + - "var y= \"fooo\";\n" + - "}\n" + - "}\n" + - "
\n" + - "
", - expected: - "@{\n" + - " void Method()\n" + - " {\n" + - " var x = \"foo\";\n" + - " @(DateTime.Now)\n" + - "

\n" + - " var y = \"fooo\";\n" + - " }\n" + - "}\n" + - "
\n" + - "
"); - } - [Fact] public async Task Formats_NonCodeBlockDirectives() { From c04f865578dc0bddc71507ae17714f5d98dbf8e5 Mon Sep 17 00:00:00 2001 From: Jordi Ramos Date: Tue, 3 Sep 2024 11:46:11 -0700 Subject: [PATCH 07/11] check if indentation location has been processed --- .../Formatting/CSharpFormattingPassBase.cs | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/src/Razor/src/Microsoft.CodeAnalysis.Razor.Workspaces/Formatting/CSharpFormattingPassBase.cs b/src/Razor/src/Microsoft.CodeAnalysis.Razor.Workspaces/Formatting/CSharpFormattingPassBase.cs index 27b38a0e31a..1a25ee5a318 100644 --- a/src/Razor/src/Microsoft.CodeAnalysis.Razor.Workspaces/Formatting/CSharpFormattingPassBase.cs +++ b/src/Razor/src/Microsoft.CodeAnalysis.Razor.Workspaces/Formatting/CSharpFormattingPassBase.cs @@ -138,7 +138,11 @@ protected async Task> AdjustIndentationAsync(FormattingContext } var scopeOwner = syntaxTreeRoot.FindInnermostNode(originalLocation); - sourceMappingIndentations[originalLocation] = new IndentationData(indentation); + if (!sourceMappingIndentations.ContainsKey(originalLocation)) + { + sourceMappingIndentations[originalLocation] = new IndentationData(indentation); + } + // For @section blocks we have special handling to add a fake source mapping/significant location at the end of the // section, to return the indentation back to before the start of the section block. From 3dc6961d514612704a87493e629f2a73606350ef Mon Sep 17 00:00:00 2001 From: Jordi Ramos Date: Tue, 3 Sep 2024 11:47:11 -0700 Subject: [PATCH 08/11] added LF line ending document to all previous razor formatting test cases --- .../Formatting_NetFx/FormattingTestBase.cs | 28 +++++++++++++++++++ 1 file changed, 28 insertions(+) diff --git a/src/Razor/test/Microsoft.AspNetCore.Razor.LanguageServer.Test/Formatting_NetFx/FormattingTestBase.cs b/src/Razor/test/Microsoft.AspNetCore.Razor.LanguageServer.Test/Formatting_NetFx/FormattingTestBase.cs index 497b2b175d9..2963577c922 100644 --- a/src/Razor/test/Microsoft.AspNetCore.Razor.LanguageServer.Test/Formatting_NetFx/FormattingTestBase.cs +++ b/src/Razor/test/Microsoft.AspNetCore.Razor.LanguageServer.Test/Formatting_NetFx/FormattingTestBase.cs @@ -53,6 +53,12 @@ private protected async Task RunFormattingTestAsync( // Run with and without forceRuntimeCodeGeneration await RunFormattingTestAsync(input, expected, tabSize, insertSpaces, fileKind, tagHelpers, allowDiagnostics, razorLSPOptions, inGlobalNamespace, forceRuntimeCodeGeneration: true); await RunFormattingTestAsync(input, expected, tabSize, insertSpaces, fileKind, tagHelpers, allowDiagnostics, razorLSPOptions, inGlobalNamespace, forceRuntimeCodeGeneration: false); + + // flip the line endings of the stings (LF to CRLF and vice versa) and run again + input = FlipLineEndings(input); + expected = FlipLineEndings(expected); + await RunFormattingTestAsync(input, expected, tabSize, insertSpaces, fileKind, tagHelpers, allowDiagnostics, razorLSPOptions, inGlobalNamespace, forceRuntimeCodeGeneration: true); + await RunFormattingTestAsync(input, expected, tabSize, insertSpaces, fileKind, tagHelpers, allowDiagnostics, razorLSPOptions, inGlobalNamespace, forceRuntimeCodeGeneration: false); } private async Task RunFormattingTestAsync(string input, string expected, int tabSize, bool insertSpaces, string? fileKind, ImmutableArray tagHelpers, bool allowDiagnostics, RazorLSPOptions? razorLSPOptions, bool inGlobalNamespace, bool forceRuntimeCodeGeneration) @@ -326,4 +332,26 @@ internal static IDocumentSnapshot CreateDocumentSnapshot(string path, ImmutableA }); return documentSnapshot.Object; } + + private static string FlipLineEndings(string input) + { + if (string.IsNullOrEmpty(input)) + { + return input; + } + + bool hasCRLF = input.Contains("\r\n"); + bool hasLF = input.Contains("\n") && !hasCRLF; + + if (hasCRLF) + { + return input.Replace("\r\n", "\n"); + } + else if (hasLF) + { + return input.Replace("\n", "\r\n"); + } + + return input; + } } From 624e5d609047f4b38187eca99931390fd0ec4522 Mon Sep 17 00:00:00 2001 From: Jordi Ramos Date: Wed, 4 Sep 2024 10:51:28 -0700 Subject: [PATCH 09/11] skipping some LF line ending formatting tests. Created an issue to track the progress --- .../CodeDirectiveFormattingTest.cs | 6 ++-- .../Formatting_NetFx/FormattingTestBase.cs | 30 ++++++++++++------- .../Formatting_NetFx/HtmlFormattingTest.cs | 18 +++++++---- 3 files changed, 36 insertions(+), 18 deletions(-) diff --git a/src/Razor/test/Microsoft.AspNetCore.Razor.LanguageServer.Test/Formatting_NetFx/CodeDirectiveFormattingTest.cs b/src/Razor/test/Microsoft.AspNetCore.Razor.LanguageServer.Test/Formatting_NetFx/CodeDirectiveFormattingTest.cs index 80589eb18ff..24ca9cf007a 100644 --- a/src/Razor/test/Microsoft.AspNetCore.Razor.LanguageServer.Test/Formatting_NetFx/CodeDirectiveFormattingTest.cs +++ b/src/Razor/test/Microsoft.AspNetCore.Razor.LanguageServer.Test/Formatting_NetFx/CodeDirectiveFormattingTest.cs @@ -1699,7 +1699,8 @@ await RunFormattingTestAsync( private IEnumerable _items = new[] { 1, 2, 3, 4, 5 }; } """, - tagHelpers: GetComponentWithCascadingTypeParameter()); + tagHelpers: GetComponentWithCascadingTypeParameter(), + skipFlipLineEndingTest: true); } [Fact] @@ -1789,7 +1790,8 @@ await RunFormattingTestAsync( private IEnumerable _items2 = new long[] { 1, 2, 3, 4, 5 }; } """, - tagHelpers: GetComponentWithTwoCascadingTypeParameter()); + tagHelpers: GetComponentWithTwoCascadingTypeParameter(), + skipFlipLineEndingTest: true); // tracked by https://github.com/dotnet/razor/issues/10836 } [Fact] diff --git a/src/Razor/test/Microsoft.AspNetCore.Razor.LanguageServer.Test/Formatting_NetFx/FormattingTestBase.cs b/src/Razor/test/Microsoft.AspNetCore.Razor.LanguageServer.Test/Formatting_NetFx/FormattingTestBase.cs index c9c6e1379a8..ac1517a0e01 100644 --- a/src/Razor/test/Microsoft.AspNetCore.Razor.LanguageServer.Test/Formatting_NetFx/FormattingTestBase.cs +++ b/src/Razor/test/Microsoft.AspNetCore.Razor.LanguageServer.Test/Formatting_NetFx/FormattingTestBase.cs @@ -4,6 +4,7 @@ using System; using System.Collections.Generic; using System.Collections.Immutable; +using System.Diagnostics.Eventing.Reader; using System.IO; using System.Linq; using System.Threading; @@ -49,20 +50,29 @@ private protected async Task RunFormattingTestAsync( ImmutableArray tagHelpers = default, bool allowDiagnostics = false, RazorLSPOptions? razorLSPOptions = null, - bool inGlobalNamespace = false) + bool inGlobalNamespace = false, + bool skipFlipLineEndingTest = false) { // Run with and without forceRuntimeCodeGeneration - await RunFormattingTestAsync(input, expected, tabSize, insertSpaces, fileKind, tagHelpers, allowDiagnostics, razorLSPOptions, inGlobalNamespace, forceRuntimeCodeGeneration: true); - await RunFormattingTestAsync(input, expected, tabSize, insertSpaces, fileKind, tagHelpers, allowDiagnostics, razorLSPOptions, inGlobalNamespace, forceRuntimeCodeGeneration: false); - - // flip the line endings of the stings (LF to CRLF and vice versa) and run again - input = FlipLineEndings(input); - expected = FlipLineEndings(expected); - await RunFormattingTestAsync(input, expected, tabSize, insertSpaces, fileKind, tagHelpers, allowDiagnostics, razorLSPOptions, inGlobalNamespace, forceRuntimeCodeGeneration: true); - await RunFormattingTestAsync(input, expected, tabSize, insertSpaces, fileKind, tagHelpers, allowDiagnostics, razorLSPOptions, inGlobalNamespace, forceRuntimeCodeGeneration: false); + await RunFormattingTestInternalAsync(input, expected, tabSize, insertSpaces, fileKind, tagHelpers, allowDiagnostics, razorLSPOptions, inGlobalNamespace, forceRuntimeCodeGeneration: true); + await RunFormattingTestInternalAsync(input, expected, tabSize, insertSpaces, fileKind, tagHelpers, allowDiagnostics, razorLSPOptions, inGlobalNamespace, forceRuntimeCodeGeneration: false); + + + // some tests are failing, skip for now, tracked by https://github.com/dotnet/razor/issues/10836 + if (!skipFlipLineEndingTest) + { + // flip the line endings of the stings (LF to CRLF and vice versa) and run again + input = FlipLineEndings(input); + expected = FlipLineEndings(expected); + + await RunFormattingTestInternalAsync(input, expected, tabSize, insertSpaces, fileKind, tagHelpers, allowDiagnostics, razorLSPOptions, inGlobalNamespace, forceRuntimeCodeGeneration: true); + await RunFormattingTestInternalAsync(input, expected, tabSize, insertSpaces, fileKind, tagHelpers, allowDiagnostics, razorLSPOptions, inGlobalNamespace, forceRuntimeCodeGeneration: false); + } + + } - private async Task RunFormattingTestAsync(string input, string expected, int tabSize, bool insertSpaces, string? fileKind, ImmutableArray tagHelpers, bool allowDiagnostics, RazorLSPOptions? razorLSPOptions, bool inGlobalNamespace, bool forceRuntimeCodeGeneration) + private async Task RunFormattingTestInternalAsync(string input, string expected, int tabSize, bool insertSpaces, string? fileKind, ImmutableArray tagHelpers, bool allowDiagnostics, RazorLSPOptions? razorLSPOptions, bool inGlobalNamespace, bool forceRuntimeCodeGeneration) { // Arrange fileKind ??= FileKinds.Component; diff --git a/src/Razor/test/Microsoft.AspNetCore.Razor.LanguageServer.Test/Formatting_NetFx/HtmlFormattingTest.cs b/src/Razor/test/Microsoft.AspNetCore.Razor.LanguageServer.Test/Formatting_NetFx/HtmlFormattingTest.cs index 6ef32d08326..300f2fc42c1 100644 --- a/src/Razor/test/Microsoft.AspNetCore.Razor.LanguageServer.Test/Formatting_NetFx/HtmlFormattingTest.cs +++ b/src/Razor/test/Microsoft.AspNetCore.Razor.LanguageServer.Test/Formatting_NetFx/HtmlFormattingTest.cs @@ -487,7 +487,8 @@ await RunFormattingTestAsync( } """, - tagHelpers: tagHelpers); + tagHelpers: tagHelpers, + skipFlipLineEndingTest: true); // tracked by https://github.com/dotnet/razor/issues/10836 } [Fact] @@ -595,7 +596,8 @@ await RunFormattingTestAsync( @{

} - """); + """, + skipFlipLineEndingTest: true); // tracked by https://github.com/dotnet/razor/issues/10836 } [Fact] @@ -1316,7 +1318,8 @@ await RunFormattingTestAsync( public bool VarBool { get; set; } } """, - fileKind: FileKinds.Component); + fileKind: FileKinds.Component, + skipFlipLineEndingTest: true); // tracked by https://github.com/dotnet/razor/issues/10836 } [Fact] @@ -1428,7 +1431,8 @@ await RunFormattingTestAsync( public bool VarBool { get; set; } } """, - fileKind: FileKinds.Component); + fileKind: FileKinds.Component, + skipFlipLineEndingTest: true); // tracked by https://github.com/dotnet/razor/issues/10836 } [Fact] @@ -1486,7 +1490,8 @@ await RunFormattingTestAsync( public bool VarBool { get; set; } } """, - fileKind: FileKinds.Component); + fileKind: FileKinds.Component, + skipFlipLineEndingTest: true); // tracked by https://github.com/dotnet/razor/issues/10836 } [Fact] @@ -1794,7 +1799,8 @@ await RunFormattingTestAsync( } """, - tagHelpers: CreateTagHelpers()); + tagHelpers: CreateTagHelpers(), + skipFlipLineEndingTest: true); // tracked by https://github.com/dotnet/razor/issues/10836 ImmutableArray CreateTagHelpers() { From 95d7a72b40b579f9575b303225b52241366a74ec Mon Sep 17 00:00:00 2001 From: Jordi Ramos Date: Wed, 4 Sep 2024 11:07:26 -0700 Subject: [PATCH 10/11] updated NormalizeLineEndings comment into XML format --- .../Formatting/RazorFormattingService.cs | 8 +++++--- 1 file changed, 5 insertions(+), 3 deletions(-) diff --git a/src/Razor/src/Microsoft.CodeAnalysis.Razor.Workspaces/Formatting/RazorFormattingService.cs b/src/Razor/src/Microsoft.CodeAnalysis.Razor.Workspaces/Formatting/RazorFormattingService.cs index 634b200be0e..176d050550b 100644 --- a/src/Razor/src/Microsoft.CodeAnalysis.Razor.Workspaces/Formatting/RazorFormattingService.cs +++ b/src/Razor/src/Microsoft.CodeAnalysis.Razor.Workspaces/Formatting/RazorFormattingService.cs @@ -281,9 +281,11 @@ private static void UnwrapCSharpSnippets(TextEdit[] razorEdits) } } - // This method counts the occurrences of CRLF and LF line endings in the original text. If LF line endings are more - // prevalent, it removes any CR characters from the text edits to ensure consistency with the LF style. - // This can be removed once we figure out how to generate the formatted CSharp file with the correct line endings. + /// + /// This method counts the occurrences of CRLF and LF line endings in the original text. + /// If LF line endings are more prevalent, it removes any CR characters from the text edits + /// to ensure consistency with the LF style. + /// private TextEdit[] NormalizeLineEndings(SourceText originalText, TextEdit[] edits) { if (originalText.HasLFLineEndings()) From b4453450da200b85a44aa5909c4f183b97b3a293 Mon Sep 17 00:00:00 2001 From: Jordi Ramos Date: Wed, 4 Sep 2024 13:43:08 -0700 Subject: [PATCH 11/11] delete unncessary lines and using directive, swap boolean values for productivity --- .../Formatting_NetFx/FormattingTestBase.cs | 8 ++------ 1 file changed, 2 insertions(+), 6 deletions(-) diff --git a/src/Razor/test/Microsoft.AspNetCore.Razor.LanguageServer.Test/Formatting_NetFx/FormattingTestBase.cs b/src/Razor/test/Microsoft.AspNetCore.Razor.LanguageServer.Test/Formatting_NetFx/FormattingTestBase.cs index ac1517a0e01..a43adfed09a 100644 --- a/src/Razor/test/Microsoft.AspNetCore.Razor.LanguageServer.Test/Formatting_NetFx/FormattingTestBase.cs +++ b/src/Razor/test/Microsoft.AspNetCore.Razor.LanguageServer.Test/Formatting_NetFx/FormattingTestBase.cs @@ -4,7 +4,6 @@ using System; using System.Collections.Generic; using System.Collections.Immutable; -using System.Diagnostics.Eventing.Reader; using System.IO; using System.Linq; using System.Threading; @@ -57,7 +56,6 @@ private protected async Task RunFormattingTestAsync( await RunFormattingTestInternalAsync(input, expected, tabSize, insertSpaces, fileKind, tagHelpers, allowDiagnostics, razorLSPOptions, inGlobalNamespace, forceRuntimeCodeGeneration: true); await RunFormattingTestInternalAsync(input, expected, tabSize, insertSpaces, fileKind, tagHelpers, allowDiagnostics, razorLSPOptions, inGlobalNamespace, forceRuntimeCodeGeneration: false); - // some tests are failing, skip for now, tracked by https://github.com/dotnet/razor/issues/10836 if (!skipFlipLineEndingTest) { @@ -68,8 +66,6 @@ private protected async Task RunFormattingTestAsync( await RunFormattingTestInternalAsync(input, expected, tabSize, insertSpaces, fileKind, tagHelpers, allowDiagnostics, razorLSPOptions, inGlobalNamespace, forceRuntimeCodeGeneration: true); await RunFormattingTestInternalAsync(input, expected, tabSize, insertSpaces, fileKind, tagHelpers, allowDiagnostics, razorLSPOptions, inGlobalNamespace, forceRuntimeCodeGeneration: false); } - - } private async Task RunFormattingTestInternalAsync(string input, string expected, int tabSize, bool insertSpaces, string? fileKind, ImmutableArray tagHelpers, bool allowDiagnostics, RazorLSPOptions? razorLSPOptions, bool inGlobalNamespace, bool forceRuntimeCodeGeneration) @@ -375,8 +371,8 @@ private static string FlipLineEndings(string input) return input; } - bool hasCRLF = input.Contains("\r\n"); - bool hasLF = input.Contains("\n") && !hasCRLF; + var hasCRLF = input.Contains("\r\n"); + var hasLF = !hasCRLF && input.Contains("\n"); if (hasCRLF) {