dotnet / razor

Compiler and tooling experience for Razor ASP.NET Core apps in Visual Studio, Visual Studio for Mac, and VS Code.
https://asp.net
MIT License
507 stars 196 forks source link

Formatting issue with HTML comments inside C# blocks #6090

Closed vsfeedback closed 2 years ago

vsfeedback commented 2 years ago

This issue has been moved from a ticket on Developer Community.


When using html comments in a razor view (haven't tested razor pages) like this

some html between

and then using visual studio formatter to format the code (ctrl+ k, ctrl + d), then indentation does not work correctly. Please refer to screenshots for more info


Original Comments

Feedback Bot on 14/02/2022, 07:22 PM:

We have directed your feedback to the appropriate engineering team for further evaluation. The team will review the feedback and notify you about the next steps.


Original Solutions

(no solutions)

davidwengier commented 2 years ago

Simple repro:

@foreach (var num in Enumerable.Range(1, 10))
{
    <span class="skill_result btn">
        <!--asdfasd-->
        <span style="margin-left:0px">
            <svg>
                <rect width="1" height="1" />
            </svg>
        </span>
        <!--adfasfd-->
    </span>
}
NTaylorMullen commented 2 years ago

Super interesting that this repro'd. Will bereally curious as to why we barf on HTML comments

davidwengier commented 2 years ago

Something about the razor syntax tree I guess.. In the presence of the comment, LocateOwner returns a different result for the line the span is on.. very strange.

davidwengier commented 2 years ago

But the differences between the tree with and without the comment look perfectly reasonable.. so weird..

Click here to see the differences (I'm hiding a stupidly long URL from you)

will need to debug through LocateOwner I guess..

davidwengier commented 2 years ago

I think this is a compiler issue...

In the code snippet from my comment above, for the line <span style="margin-left:0px">, for the MarkupTextLiteral node that is the whitespace in front of the span, GetSpanContext() returns a SpanEditHandler that accepts None characters. If I remove the comment above it, then it accepts Any.

@TanayParikh @pranavkm @javiercn I have no idea what a span context even is, does that sound remotely reasonable? Or am I misunderstanding something? I don't want to just create a compiler issue if I'm missing something.

davidwengier commented 2 years ago

.. and now that I know what to look for, I can see it in the tree diff linked above. I've highlighted the whitespace-before-span-element:

image
NTaylorMullen commented 2 years ago

@davidwengier updated your comment to include @javiercn. If LocateOwner is failing here then that's concerning

davidwengier commented 2 years ago

Another report https://developercommunity.visualstudio.com/t/Razor-format-when-have-html-comment/1669280

davidwengier commented 2 years ago

This is (another instance of) dotnet/razor-tooling#7505