From 005eb138b4583944a449f2cf754dd2ab31654f4f Mon Sep 17 00:00:00 2001 From: David Wengier Date: Wed, 6 Oct 2021 10:13:12 +1100 Subject: [PATCH] Format the first code block in a file to be at the start of a line --- .../Formatting/CSharpFormattingPassBase.cs | 8 ++- .../Formatting/CodeDirectiveFormattingTest.cs | 65 ++++++++++++++++++- 2 files changed, 69 insertions(+), 4 deletions(-) diff --git a/src/Razor/src/Microsoft.AspNetCore.Razor.LanguageServer/Formatting/CSharpFormattingPassBase.cs b/src/Razor/src/Microsoft.AspNetCore.Razor.LanguageServer/Formatting/CSharpFormattingPassBase.cs index 017bbd1971..5abc9df81d 100644 --- a/src/Razor/src/Microsoft.AspNetCore.Razor.LanguageServer/Formatting/CSharpFormattingPassBase.cs +++ b/src/Razor/src/Microsoft.AspNetCore.Razor.LanguageServer/Formatting/CSharpFormattingPassBase.cs @@ -178,13 +178,17 @@ namespace Microsoft.AspNetCore.Razor.LanguageServer.Formatting index = (~index) - 1; } - // This will now be set to the same value as the end of the closest source mapping. if (index < 0) { - csharpDesiredIndentation = 0; + // If we _still_ couldn't find the right indentation, then it probably means that the text is + // before the first source mapping location, so we can just place it in the minimum spot (realistically + // at index 0 in the razor file, but we use minCSharpIndentation because we're adjusting based on the + // generated file here) + csharpDesiredIndentation = minCSharpIndentation; } else { + // index will now be set to the same value as the end of the closest source mapping. var absoluteIndex = sourceMappingIndentationScopes[index]; csharpDesiredIndentation = sourceMappingIndentations[absoluteIndex]; 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 485b99e634..b9d4367774 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 @@ -419,7 +419,7 @@ Hello World } ", expected: @"Hello World - @functions { +@functions { public class Foo { } } "); @@ -434,7 +434,7 @@ input: @" public class Foo{} } ", -expected: @" @functions { +expected: @"@functions { public class Foo { } } "); @@ -731,6 +731,67 @@ expected: @"@code { } }; } +"); + } + + [Fact] + [WorkItem("https://github.com/dotnet/aspnetcore/issues/4498")] + public async Task IfBlock_TopLevel() + { + await RunFormattingTestAsync( +input: @" + @if (true) +{ +} +", +expected: @"@if (true) +{ +} +"); + } + + [Fact] + [WorkItem("https://github.com/dotnet/aspnetcore/issues/4498")] + public async Task IfBlock_TopLevel_WithOtherCode() + { + await RunFormattingTestAsync( +input: @" +@{ + // foo +} + + @if (true) +{ +} +", +expected: @"@{ + // foo +} + +@if (true) +{ +} +"); + } + + [Fact] + [WorkItem("https://github.com/dotnet/aspnetcore/issues/4498")] + public async Task IfBlock_Nested() + { + await RunFormattingTestAsync( +input: @" +
+ @if (true) +{ +} +
+", +expected: @" +
+ @if (true) + { + } +
"); } }