Skip to content
Merged
Show file tree
Hide file tree
Changes from 2 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,8 +16,6 @@ namespace Microsoft.AspNetCore.Razor.LanguageServer.Formatting;

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

private readonly List<FormattingSpan> _spans;
private FormattingBlockKind _currentBlockKind;
private SyntaxNode? _currentBlock;
Expand Down Expand Up @@ -127,8 +125,10 @@ 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
var causesIndentation = node.StartTag is null || !node.StartTag.IsVoidElement();

if (causesIndentation)
{
_currentHtmlIndentationLevel++;
}
Expand All @@ -138,8 +138,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 (causesIndentation)
{
_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 @@ -437,6 +436,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