diff --git a/src/Razor/src/Razor/src/Microsoft.CodeAnalysis.Remote.Razor/FoldingRanges/AbstractSyntaxNodeFoldingProvider.cs b/src/Razor/src/Razor/src/Microsoft.CodeAnalysis.Remote.Razor/FoldingRanges/AbstractSyntaxNodeFoldingProvider.cs index 132c69a985346..344564e535489 100644 --- a/src/Razor/src/Razor/src/Microsoft.CodeAnalysis.Remote.Razor/FoldingRanges/AbstractSyntaxNodeFoldingProvider.cs +++ b/src/Razor/src/Razor/src/Microsoft.CodeAnalysis.Remote.Razor/FoldingRanges/AbstractSyntaxNodeFoldingProvider.cs @@ -22,6 +22,15 @@ public ImmutableArray GetFoldingRanges(RazorCodeDocument codeDocum foreach (var node in nodes) { var (start, end) = sourceText.GetLinePositionSpan(node.Span); + + // Razor nodes can include the line break after their closing brace. Folding that line break + // pulls the following content onto the collapsed line. + if (end.Character == 0 && end.Line > start.Line) + { + var previousLine = sourceText.Lines[end.Line - 1]; + end = new(end.Line - 1, previousLine.Span.Length); + } + var foldingRange = new FoldingRange() { StartCharacter = start.Character, diff --git a/src/Razor/src/Razor/test/Microsoft.CodeAnalysis.Razor.CohostingShared.UnitTests/Endpoints/CohostFoldingRangeEndpointTest.cs b/src/Razor/src/Razor/test/Microsoft.CodeAnalysis.Razor.CohostingShared.UnitTests/Endpoints/CohostFoldingRangeEndpointTest.cs index 1e7a679ea84d2..133f5a2bbd32f 100644 --- a/src/Razor/src/Razor/test/Microsoft.CodeAnalysis.Razor.CohostingShared.UnitTests/Endpoints/CohostFoldingRangeEndpointTest.cs +++ b/src/Razor/src/Razor/test/Microsoft.CodeAnalysis.Razor.CohostingShared.UnitTests/Endpoints/CohostFoldingRangeEndpointTest.cs @@ -27,15 +27,15 @@ public Task BadLooseFileUri()
Hello World
- } - |] + }|] + @if (true) {[|
Hello World
- } - |] + }|] + @if (true) {[| }|] """, @@ -52,20 +52,35 @@ public Task IfStatements(bool miscellaneousFile)
Hello World
- } - |] + }|] + @if (true) {[|
Hello World
- } - |] + }|] + @if (true) {[| }|] """, miscellaneousFile: miscellaneousFile); + [Fact] + public Task IfStatements_Adjacent() + => VerifyFoldingRangesAsync(""" + @if (true) {[| +
+ Hello World +
+ }|] + @if (false) {[| +
+ Goodbye World +
+ }|] + """); + [Fact] public Task LockStatement() => VerifyFoldingRangesAsync(""" @@ -92,8 +107,7 @@ Hello World
Goodbye World
- }|] - |] } + }|]|] """); @@ -285,8 +299,7 @@ Hello World
Goodbye World
- |] } - |] } + |] }|] @code[| diff --git a/src/Razor/src/Razor/test/Microsoft.VisualStudio.Razor.IntegrationTests/CodeFoldingTests.cs b/src/Razor/src/Razor/test/Microsoft.VisualStudio.Razor.IntegrationTests/CodeFoldingTests.cs index b2278f057d345..fc5c2ff006f5b 100644 --- a/src/Razor/src/Razor/test/Microsoft.VisualStudio.Razor.IntegrationTests/CodeFoldingTests.cs +++ b/src/Razor/src/Razor/test/Microsoft.VisualStudio.Razor.IntegrationTests/CodeFoldingTests.cs @@ -178,7 +178,7 @@ private void IncrementCount() """); } - [IdeFact(Skip = "https://github.com/dotnet/razor/issues/10860")] // FUSE changes whitespace on folding ranges + [IdeFact] public async Task CodeFolding_IfBlock() { await TestServices.SolutionExplorer.AddFileAsync(