-
Notifications
You must be signed in to change notification settings - Fork 199
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
Conversation
src/Razor/src/Microsoft.CodeAnalysis.Razor.Workspaces/DocumentMapping/RazorEditHelper.cs
Outdated
Show resolved
Hide resolved
|
||
private static string GetNamespaceFromDirective(UsingDirectiveSyntax usingDirectiveSyntax) | ||
{ | ||
var str = usingDirectiveSyntax.ToFullString(); |
There was a problem hiding this comment.
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);
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
💯
There was a problem hiding this comment.
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));
}
There was a problem hiding this comment.
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?
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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
There was a problem hiding this comment.
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?
There was a problem hiding this comment.
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
There was a problem hiding this comment.
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.
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.