Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Handle aliased usings in RazorEditHelper #11208

Merged
merged 7 commits into from
Nov 20, 2024

Conversation

ryzngard
Copy link
Contributor

Actually ended up being easier than I thought. I didn't move AddUsings to the same functionality but definitely could? It doesn't seem like it would need it as much.

@ryzngard ryzngard requested a review from a team as a code owner November 14, 2024 00:03

private static string GetNamespaceFromDirective(UsingDirectiveSyntax usingDirectiveSyntax)
{
var str = usingDirectiveSyntax.ToFullString();
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Random code written without a compiler, but rather than realising the string here and trimming twice, is it more efficient to do something like this:

// .Span ignores trivia, so by the rules of Roslyn it won't contain whitespace, right??
var span = usingDirectiveSyntax.Span;
if (!usingDirectiveSyntax.SemicolonToken.IsMissing)
{
	span = new TextSpan(span.Start, span.Length -1);
}

if (!usingDirectiveSyntax.UsingKeyword.IsMissing)
{
	span = ....
}

return sourceText.GetSubTextString(span);

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

💯

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Honestly I got so in the habit of how the razor syntax tree looks I forgot about sensible things like the span not including trivia. This can be simplified even more by making some assumptions (like explicitly only handling things where the Name is not null,)

private static string GetNamespaceFromDirective(UsingDirectiveSyntax usingDirectiveSyntax, SourceText sourceText)
{
    var nameSyntax = usingDirectiveSyntax.Name.AssumeNotNull();

    if (usingDirectiveSyntax.Alias is null)
    {
        return sourceText.GetSubTextString(nameSyntax.Span);
    }

    return sourceText.GetSubTextString(TextSpan.FromBounds(usingDirectiveSyntax.Alias.Span.Start, nameSyntax.Span.End));
}

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

using static?

No idea if it's possible in a razor file, but worth adding a test for, either to validate or prove it's not supported?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm guessing since it's just a csharp block that it is supported. I'll add a test and I think I can just find the leftmost ChildNode since the using keyword is a token.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Scratch that, static is a token. I guess it's left most child nodes and tokens excluding the using keyword

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What about the start position just being the using keywords span end, plus end trivia length?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is it guarantee that the end trivia is attached to the using keyword? That's the only thing I wasn't sure of. I'm rusty on my Roslyn syntax

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Guaranteed, no idea. From a quick test, it was.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants