diff --git a/src/Razor/src/Microsoft.AspNetCore.Razor.LanguageServer/Formatting/CSharpFormatter.cs b/src/Razor/src/Microsoft.AspNetCore.Razor.LanguageServer/Formatting/CSharpFormatter.cs index 1b7206444b2..ae02a03f540 100644 --- a/src/Razor/src/Microsoft.AspNetCore.Razor.LanguageServer/Formatting/CSharpFormatter.cs +++ b/src/Razor/src/Microsoft.AspNetCore.Razor.LanguageServer/Formatting/CSharpFormatter.cs @@ -49,10 +49,19 @@ public CSharpFormatter( _server = languageServer; _filePathNormalizer = filePathNormalizer; - var type = typeof(CSharpFormattingOptions).Assembly.GetType("Microsoft.CodeAnalysis.CSharp.Indentation.CSharpIndentationService", throwOnError: true); - _indentationService = Activator.CreateInstance(type); - var indentationService = type.GetInterface("IIndentationService"); - _getIndentationMethod = indentationService.GetMethod("GetIndentation"); + try + { + var type = typeof(CSharpFormattingOptions).Assembly.GetType("Microsoft.CodeAnalysis.CSharp.Indentation.CSharpIndentationService", throwOnError: true); + _indentationService = Activator.CreateInstance(type); + var indentationService = type.GetInterface("IIndentationService"); + _getIndentationMethod = indentationService.GetMethod("GetIndentation"); + } + catch (Exception ex) + { + throw new InvalidOperationException( + "Error occured when creating an instance of Roslyn's IIndentationService. Roslyn may have changed in an unexpected way.", + ex); + } } public async Task FormatAsync( diff --git a/src/Razor/src/Microsoft.AspNetCore.Razor.LanguageServer/Formatting/CSharpFormattingPass.cs b/src/Razor/src/Microsoft.AspNetCore.Razor.LanguageServer/Formatting/CSharpFormattingPass.cs index cf2accb06c8..905785d3316 100644 --- a/src/Razor/src/Microsoft.AspNetCore.Razor.LanguageServer/Formatting/CSharpFormattingPass.cs +++ b/src/Razor/src/Microsoft.AspNetCore.Razor.LanguageServer/Formatting/CSharpFormattingPass.cs @@ -166,7 +166,7 @@ private List AdjustIndentation(FormattingContext context, Cancellati // Couldn't remap. This is probably a non-C# location. // Use SourceMapping indentations to locate the C# scope of this line. // E.g, - // + // // @if (true) { //
// |
@@ -175,16 +175,16 @@ private List AdjustIndentation(FormattingContext context, Cancellati // We can't find a direct mapping at |, but we can infer its base indentation from the // indentation of the latest source mapping prior to this line. // We use binary search to find that spot. - + var index = Array.BinarySearch(sourceMappingIndentationScopes, lineStart); if (index < 0) { - // Couldn't find the exact value. Find the index of the element to the right of the searched value. - index = ~index; + // Couldn't find the exact value. Find the index of the element to the left of the searched value. + index = (~index) - 1; } // This will now be set to the same value as the end of the closest source mapping. - csharpDesiredIndentation = index == 0 ? 0 : sourceMappingIndentations[sourceMappingIndentationScopes[index - 1]]; + csharpDesiredIndentation = index < 0 ? 0 : sourceMappingIndentations[sourceMappingIndentationScopes[index]]; } // Now let's use that information to figure out the effective C# indentation. diff --git a/src/Razor/test/Microsoft.AspNetCore.Razor.LanguageServer.Test/Formatting/CodeDirectiveFormattingTest.cs b/src/Razor/test/Microsoft.AspNetCore.Razor.LanguageServer.Test/Formatting/CodeDirectiveFormattingTest.cs index 3c6fe91c908..9f9f89bf70d 100644 --- a/src/Razor/test/Microsoft.AspNetCore.Razor.LanguageServer.Test/Formatting/CodeDirectiveFormattingTest.cs +++ b/src/Razor/test/Microsoft.AspNetCore.Razor.LanguageServer.Test/Formatting/CodeDirectiveFormattingTest.cs @@ -254,7 +254,7 @@ public class HelloWorld } @functions{ - + public class Bar {} } |", @@ -323,7 +323,7 @@ public class Foo { } "); } - [Fact(Skip = "https://github.com/dotnet/aspnetcore/issues/25475")] + [Fact] public async Task IndentsCodeBlockDirectiveStart() { await RunFormattingTestAsync( @@ -340,7 +340,7 @@ public class Foo { } "); } - [Fact(Skip = "https://github.com/dotnet/aspnetcore/issues/25475")] + [Fact] public async Task IndentsCodeBlockDirectiveEnd() { await RunFormattingTestAsync( diff --git a/src/Razor/test/Microsoft.AspNetCore.Razor.LanguageServer.Test/Formatting/FormattingLanguageServerClient.cs b/src/Razor/test/Microsoft.AspNetCore.Razor.LanguageServer.Test/Formatting/FormattingLanguageServerClient.cs index 8187210f7fd..dd8ffa24c4b 100644 --- a/src/Razor/test/Microsoft.AspNetCore.Razor.LanguageServer.Test/Formatting/FormattingLanguageServerClient.cs +++ b/src/Razor/test/Microsoft.AspNetCore.Razor.LanguageServer.Test/Formatting/FormattingLanguageServerClient.cs @@ -81,6 +81,7 @@ private RazorDocumentRangeFormattingResponse Format(RazorDocumentRangeFormatting var workspace = new AdhocWorkspace(); var cSharpOptions = workspace.Options .WithChangedOption(FormattingOptions.TabSize, LanguageNames.CSharp, (int)options.TabSize) + .WithChangedOption(FormattingOptions.IndentationSize, LanguageNames.CSharp, (int)options.TabSize) .WithChangedOption(FormattingOptions.UseTabs, LanguageNames.CSharp, !options.InsertSpaces); var codeDocument = _documents[@params.HostDocumentFilePath]; @@ -99,8 +100,7 @@ private RazorDocumentRangeFormattingResponse Format(RazorDocumentRangeFormatting response.Edits = Array.Empty(); var codeDocument = _documents[@params.HostDocumentFilePath]; - var generatedHtml = codeDocument.GetHtmlDocument().GeneratedHtml; - var inputText = SourceText.From(generatedHtml); + var generatedHtml = codeDocument.GetHtmlDocument().GeneratedHtml.Replace("\r", "").Replace("\n", "\r\n"); // Get formatted baseline file var baselineInputFileName = Path.ChangeExtension(_baselineFileName, ".input.html"); diff --git a/src/Razor/test/Microsoft.AspNetCore.Razor.LanguageServer.Test/Formatting/FormattingTestBase.cs b/src/Razor/test/Microsoft.AspNetCore.Razor.LanguageServer.Test/Formatting/FormattingTestBase.cs index 9d6a1de1398..9af1697b825 100644 --- a/src/Razor/test/Microsoft.AspNetCore.Razor.LanguageServer.Test/Formatting/FormattingTestBase.cs +++ b/src/Razor/test/Microsoft.AspNetCore.Razor.LanguageServer.Test/Formatting/FormattingTestBase.cs @@ -73,7 +73,7 @@ protected async Task RunFormattingTestAsync(string input, string expected, int t #if GENERATE_BASELINES Assert.False(true, "GENERATE_BASELINES is set to true."); #else - Assert.Equal(expected, actual); + Assert.Equal(expected, actual, ignoreLineEndingDifferences: true); #endif } diff --git a/src/Razor/test/Microsoft.AspNetCore.Razor.LanguageServer.Test/Formatting/TestFiles/CodeDirectiveFormattingTest/IndentsCodeBlockDirectiveEnd.input.html b/src/Razor/test/Microsoft.AspNetCore.Razor.LanguageServer.Test/Formatting/TestFiles/CodeDirectiveFormattingTest/IndentsCodeBlockDirectiveEnd.input.html new file mode 100644 index 00000000000..c66280585df --- /dev/null +++ b/src/Razor/test/Microsoft.AspNetCore.Razor.LanguageServer.Test/Formatting/TestFiles/CodeDirectiveFormattingTest/IndentsCodeBlockDirectiveEnd.input.html @@ -0,0 +1,4 @@ + + ~~~~~~~~~~ ~ +~~~~~~ ~~~~~ ~~~~~ + ~ diff --git a/src/Razor/test/Microsoft.AspNetCore.Razor.LanguageServer.Test/Formatting/TestFiles/CodeDirectiveFormattingTest/IndentsCodeBlockDirectiveEnd.output.html b/src/Razor/test/Microsoft.AspNetCore.Razor.LanguageServer.Test/Formatting/TestFiles/CodeDirectiveFormattingTest/IndentsCodeBlockDirectiveEnd.output.html new file mode 100644 index 00000000000..6ce3818453b --- /dev/null +++ b/src/Razor/test/Microsoft.AspNetCore.Razor.LanguageServer.Test/Formatting/TestFiles/CodeDirectiveFormattingTest/IndentsCodeBlockDirectiveEnd.output.html @@ -0,0 +1,3 @@ +~~~~~~~~~~ ~ +~~~~~~ ~~~~~ ~~~~~ +~ diff --git a/src/Razor/test/Microsoft.AspNetCore.Razor.LanguageServer.Test/Formatting/TestFiles/CodeDirectiveFormattingTest/IndentsCodeBlockDirectiveStart.input.html b/src/Razor/test/Microsoft.AspNetCore.Razor.LanguageServer.Test/Formatting/TestFiles/CodeDirectiveFormattingTest/IndentsCodeBlockDirectiveStart.input.html new file mode 100644 index 00000000000..39e8a639c5e --- /dev/null +++ b/src/Razor/test/Microsoft.AspNetCore.Razor.LanguageServer.Test/Formatting/TestFiles/CodeDirectiveFormattingTest/IndentsCodeBlockDirectiveStart.input.html @@ -0,0 +1,4 @@ + +Hello World + ~~~~~~~~~~ ~~~~~~~ ~~~~~ ~~~~~ +~ diff --git a/src/Razor/test/Microsoft.AspNetCore.Razor.LanguageServer.Test/Formatting/TestFiles/CodeDirectiveFormattingTest/IndentsCodeBlockDirectiveStart.output.html b/src/Razor/test/Microsoft.AspNetCore.Razor.LanguageServer.Test/Formatting/TestFiles/CodeDirectiveFormattingTest/IndentsCodeBlockDirectiveStart.output.html new file mode 100644 index 00000000000..ee1f93c1c69 --- /dev/null +++ b/src/Razor/test/Microsoft.AspNetCore.Razor.LanguageServer.Test/Formatting/TestFiles/CodeDirectiveFormattingTest/IndentsCodeBlockDirectiveStart.output.html @@ -0,0 +1,3 @@ +Hello World +~~~~~~~~~~ ~~~~~~~ ~~~~~ ~~~~~ +~