Skip to content

Commit 80dcfc7

Browse files
authored
2 parents ae20c81 + 3cb69ae commit 80dcfc7

File tree

3 files changed

+77
-5
lines changed

3 files changed

+77
-5
lines changed

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

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,15 +2,16 @@
22
// Licensed under the MIT license. See License.txt in the project root for license information.
33

44
using Microsoft.AspNetCore.Razor;
5-
using Microsoft.CodeAnalysis.Razor;
65

76
namespace Microsoft.CodeAnalysis.Text;
87

98
internal static class TextLineExtensions
109
{
1110
public static string GetLeadingWhitespace(this TextLine line)
1211
{
13-
return line.ToString().GetLeadingWhitespace();
12+
return line.GetFirstNonWhitespaceOffset() is int offset
13+
? line.Text.AssumeNotNull().ToString(TextSpan.FromBounds(line.Start, line.Start + offset))
14+
: string.Empty;
1415
}
1516

1617
public static int GetIndentationSize(this TextLine line, long tabSize)

src/Razor/src/Microsoft.CodeAnalysis.Razor.Workspaces/Formatting/Passes/RazorFormattingPass.cs

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@
33

44
using System.Collections.Generic;
55
using System.Collections.Immutable;
6-
using System.Diagnostics;
76
using System.Diagnostics.CodeAnalysis;
87
using System.Linq;
98
using System.Threading;
@@ -419,8 +418,15 @@ private bool FormatBlock(FormattingContext context, RazorSourceDocument source,
419418
{
420419
var openBraceLineNumber = openBraceNode.GetLinePositionSpan(source).Start.Line;
421420
var openBraceLine = source.Text.Lines[openBraceLineNumber];
422-
Debug.Assert(openBraceLine.GetFirstNonWhitespacePosition().HasValue);
423-
additionalIndentation = source.Text.GetSubTextString(TextSpan.FromBounds(openBraceLine.Start, openBraceLine.GetFirstNonWhitespacePosition().GetValueOrDefault()));
421+
422+
// The open brace node might actually start with a newline on the line before, which could be blank,
423+
// so make sure we find some actual content.
424+
while (!openBraceLine.GetFirstNonWhitespacePosition().HasValue)
425+
{
426+
openBraceLine = source.Text.Lines[++openBraceLineNumber];
427+
}
428+
429+
additionalIndentation = openBraceLine.GetLeadingWhitespace();
424430
}
425431
}
426432

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

Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1770,6 +1770,71 @@ await RunFormattingTestAsync(
17701770
""");
17711771
}
17721772

1773+
[FormattingTestFact]
1774+
[WorkItem("https://devdiv.visualstudio.com/DevDiv/_workitems/edit/2471065")]
1775+
public Task MultipleHtmlElementsInCSharpCode()
1776+
=> RunFormattingTestAsync(
1777+
input: """
1778+
<div class="p-3 pb-0" style="min-height:24rem;">
1779+
1780+
@if (productsForExport != null)
1781+
{
1782+
<label class="section-label">
1783+
@(!showResult ? "some label" : "another label")
1784+
</label>
1785+
1786+
<div class="row">
1787+
<div class="col">
1788+
@if (!showResult)
1789+
{
1790+
<label>
1791+
<input type="checkbox" class="me-2 mt-1" />
1792+
<span>some label</span>
1793+
</label>
1794+
}
1795+
</div>
1796+
1797+
</div>
1798+
}
1799+
1800+
</div>
1801+
1802+
@code {
1803+
bool showResult = false;
1804+
object? productsForExport = null;
1805+
}
1806+
""",
1807+
expected: """
1808+
<div class="p-3 pb-0" style="min-height:24rem;">
1809+
1810+
@if (productsForExport != null)
1811+
{
1812+
<label class="section-label">
1813+
@(!showResult ? "some label" : "another label")
1814+
</label>
1815+
1816+
<div class="row">
1817+
<div class="col">
1818+
@if (!showResult)
1819+
{
1820+
<label>
1821+
<input type="checkbox" class="me-2 mt-1" />
1822+
<span>some label</span>
1823+
</label>
1824+
}
1825+
</div>
1826+
1827+
</div>
1828+
}
1829+
1830+
</div>
1831+
1832+
@code {
1833+
bool showResult = false;
1834+
object? productsForExport = null;
1835+
}
1836+
""");
1837+
17731838
[FormattingTestFact]
17741839
[WorkItem("https://github.com/dotnet/aspnetcore/issues/29645")]
17751840
public async Task FormatHtmlInIf_Range()

0 commit comments

Comments
 (0)