Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ namespace Microsoft.AspNetCore.Razor.LanguageServer.Formatting;

internal class FormattingVisitor : SyntaxWalker
{
private const string HtmlTagName = "html";
private const string HtmlTag = "html";

private readonly List<FormattingSpan> _spans;
private FormattingBlockKind _currentBlockKind;
Expand Down Expand Up @@ -127,8 +127,13 @@ public override void VisitMarkupElement(MarkupElementSyntax node)
{
Visit(node.StartTag);

// Temporary fix to not break the default Html formatting behavior. Remove after https://github.com/dotnet/aspnetcore/issues/25475.
if (!string.Equals(node.StartTag?.Name?.Content, HtmlTagName, StringComparison.OrdinalIgnoreCase))
// Void elements, like <meta> or <input> which don't need an end tag don't cause indentation.
// We also cheat and treat the <html> tag as a void element, so it doesn't cause indentation,
// as that's what the Html formatter does, to avoid one level of indentation in every html file.
var voidElement = node.StartTag is { } startTag &&
(startTag.IsVoidElement() || string.Equals(startTag.Name.Content, HtmlTag, StringComparison.OrdinalIgnoreCase));

if (!voidElement)
{
_currentHtmlIndentationLevel++;
}
Expand All @@ -138,8 +143,7 @@ public override void VisitMarkupElement(MarkupElementSyntax node)
Visit(child);
}

// Temporary fix to not break the default Html formatting behavior. Remove after https://github.com/dotnet/aspnetcore/issues/25475.
if (!string.Equals(node.StartTag?.Name?.Content, HtmlTagName, StringComparison.OrdinalIgnoreCase))
if (!voidElement)
{
_currentHtmlIndentationLevel--;
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
// Copyright (c) .NET Foundation. All rights reserved.
// Licensed under the MIT license. See License.txt in the project root for license information.

using System.Collections.Generic;
using System.Collections.Immutable;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Razor.Language;
Expand Down Expand Up @@ -43,6 +42,36 @@ public interface Bar
""");
}

[Fact]
public async Task FormatCSharpInsideHtmlTag()
{
await RunFormattingTestAsync(
input: """
<html>
<body>
<div>
@{
<span>foo</span>
<span>foo</span>
}
</div>
</body>
</html>
""",
expected: """
<html>
<body>
<div>
@{
<span>foo</span>
<span>foo</span>
}
</div>
</body>
</html>
""");
}

[Fact]
public async Task Format_DocumentWithDiagnostics()
{
Expand Down Expand Up @@ -437,6 +466,50 @@ @section Scripts {
fileKind: FileKinds.Legacy);
}

[Fact]
public async Task Format_SectionDirectiveBlock6()
{
await RunFormattingTestAsync(
input: """
@functions {
public class Foo{
void Method() { }
}
}

@section Scripts {
<meta property="a" content="b">
<meta property="a" content="b"/>
<meta property="a" content="b">

@if(true)
{
<p>this is a paragraph</p>
}
}
""",
expected: """
@functions {
public class Foo
{
void Method() { }
}
}

@section Scripts {
<meta property="a" content="b">
<meta property="a" content="b" />
<meta property="a" content="b">

@if (true)
{
<p>this is a paragraph</p>
}
}
""",
fileKind: FileKinds.Legacy);
}

[Fact]
public async Task Formats_CodeBlockDirectiveWithRazorComments()
{
Expand Down