-
Notifications
You must be signed in to change notification settings - Fork 4k
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
Reduce Linq usage in SymbolDisplayVisitor.CreateAliasMap #73793
Merged
Merged
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -195,27 +195,41 @@ private IDictionary<INamespaceOrTypeSymbol, IAliasSymbol> CreateAliasMap() | |
|
||
// NOTE(cyrusn): If we're currently in a block of usings, then we want to collect the | ||
// aliases that are higher up than this block. Using aliases declared in a block of | ||
// usings are not usable from within that same block. | ||
var usingDirective = GetAncestorOrThis<UsingDirectiveSyntax>(startNode); | ||
if (usingDirective != null) | ||
// usings are not usable from within that same block. This loop moves us outside of the | ||
// immediately enclosing using directive, if any. | ||
while (startNode != null) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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 commentThe reason will be displayed to describe this comment to others. Learn more. Done in commit 2 |
||
{ | ||
startNode = usingDirective.Parent!.Parent!; | ||
if (startNode is UsingDirectiveSyntax) | ||
{ | ||
startNode = startNode.Parent!.Parent!; | ||
break; | ||
} | ||
|
||
startNode = startNode.Parent; | ||
} | ||
|
||
var usingAliases = GetAncestorsOrThis<BaseNamespaceDeclarationSyntax>(startNode) | ||
.SelectMany(n => n.Usings) | ||
.Concat(GetAncestorsOrThis<CompilationUnitSyntax>(startNode).SelectMany(c => c.Usings)) | ||
.Where(u => u.Alias != null) | ||
.Select(u => semanticModel.GetDeclaredSymbol(u) as IAliasSymbol) | ||
.WhereNotNull(); | ||
startNode ??= token.Parent; | ||
|
||
var builder = ImmutableDictionary.CreateBuilder<INamespaceOrTypeSymbol, IAliasSymbol>(); | ||
foreach (var alias in usingAliases) | ||
while (startNode != null) | ||
{ | ||
if (!builder.ContainsKey(alias.Target)) | ||
var usings = (startNode as BaseNamespaceDeclarationSyntax)?.Usings; | ||
usings ??= (startNode as CompilationUnitSyntax)?.Usings; | ||
|
||
if (usings != null) | ||
{ | ||
builder.Add(alias.Target, alias); | ||
foreach (var u in usings) | ||
{ | ||
if (u.Alias != null | ||
&& semanticModel.GetDeclaredSymbol(u) is IAliasSymbol aliasSymbol | ||
&& !builder.ContainsKey(aliasSymbol.Target)) | ||
{ | ||
builder.Add(aliasSymbol.Target, aliasSymbol); | ||
} | ||
} | ||
} | ||
|
||
startNode = startNode.Parent; | ||
} | ||
|
||
return builder.ToImmutable(); | ||
|
@@ -287,18 +301,6 @@ private string RemoveAttributeSuffixIfNecessary(INamedTypeSymbol symbol, string | |
return symbolName; | ||
} | ||
|
||
private static T? GetAncestorOrThis<T>(SyntaxNode node) where T : SyntaxNode | ||
{ | ||
return GetAncestorsOrThis<T>(node).FirstOrDefault(); | ||
} | ||
|
||
private static IEnumerable<T> GetAncestorsOrThis<T>(SyntaxNode node) where T : SyntaxNode | ||
{ | ||
return node == null | ||
? SpecializedCollections.EmptyEnumerable<T>() | ||
: node.AncestorsAndSelf().OfType<T>(); | ||
} | ||
|
||
private IDictionary<INamespaceOrTypeSymbol, IAliasSymbol> AliasMap | ||
{ | ||
get | ||
|
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
It looks like, if we are not in in a using directive, we will traverse syntax all the way to the top in this loop, and then restart reversion from the beginning looking for a namespace declaration or a compilation unit node. Instead, we could also stop once we reached a
BaseNamespaceDeclarationSyntax
orCompilationUnitSyntax
node. Then the next loop could continue from the currentstartNode
instead of resetting it.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 think I'd prefer to pass on this unless you feel strongly. It seems like the current code is authentic to the original implementation, and making the change that you suggested will complicate things slightly. If this method shows up in CPU profiles, then this suggestion sounds like a good idea to pursue.