Skip to content

Commit

Permalink
Merge pull request #73516 from CyrusNajmabadi/farAsync
Browse files Browse the repository at this point in the history
  • Loading branch information
CyrusNajmabadi authored May 16, 2024
2 parents cdbed67 + 151e653 commit 248da13
Show file tree
Hide file tree
Showing 26 changed files with 126 additions and 150 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -236,7 +236,7 @@ private static async Task<ImmutableArray<ReferencedSymbol>> FindChangeSignatureR
var engine = new FindReferencesSearchEngine(
solution,
documents: null,
ReferenceFinders.DefaultReferenceFinders.Add(DelegateInvokeMethodReferenceFinder.DelegateInvokeMethod),
[.. ReferenceFinders.DefaultReferenceFinders, DelegateInvokeMethodReferenceFinder.Instance],
streamingProgress,
FindReferencesSearchOptions.Default);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,9 +26,13 @@ namespace Microsoft.CodeAnalysis.ChangeSignature;
/// <remarks>
/// TODO: Rewrite this to track backward through references instead of binding everything
/// </remarks>
internal class DelegateInvokeMethodReferenceFinder : AbstractReferenceFinder<IMethodSymbol>
internal sealed class DelegateInvokeMethodReferenceFinder : AbstractReferenceFinder<IMethodSymbol>
{
public static readonly IReferenceFinder DelegateInvokeMethod = new DelegateInvokeMethodReferenceFinder();
public static readonly DelegateInvokeMethodReferenceFinder Instance = new();

private DelegateInvokeMethodReferenceFinder()
{
}

protected override bool CanFind(IMethodSymbol symbol)
=> symbol.MethodKind == MethodKind.DelegateInvoke;
Expand Down Expand Up @@ -76,7 +80,7 @@ protected override Task DetermineDocumentsToSearchAsync<TData>(
return Task.CompletedTask;
}

protected override async ValueTask FindReferencesInDocumentAsync<TData>(
protected override void FindReferencesInDocument<TData>(
IMethodSymbol methodSymbol,
FindReferencesDocumentState state,
Action<FinderLocation, TData> processResult,
Expand Down Expand Up @@ -105,8 +109,7 @@ protected override async ValueTask FindReferencesInDocumentAsync<TData>(
var convertedType = (ISymbol?)state.SemanticModel.GetTypeInfo(node, cancellationToken).ConvertedType;
if (convertedType != null)
{
convertedType = await SymbolFinder.FindSourceDefinitionAsync(convertedType, state.Solution, cancellationToken).ConfigureAwait(false)
?? convertedType;
convertedType = SymbolFinder.FindSourceDefinition(convertedType, state.Solution, cancellationToken) ?? convertedType;
}

if (convertedType == methodSymbol.ContainingType)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
using Microsoft.CodeAnalysis.Editing;
using Microsoft.CodeAnalysis.FindSymbols;
using Microsoft.CodeAnalysis.LanguageService;
using Microsoft.CodeAnalysis.PooledObjects;
using Microsoft.CodeAnalysis.Shared.Extensions;
using Microsoft.CodeAnalysis.Shared.Utilities;
using Microsoft.CodeAnalysis.Simplification;
Expand Down Expand Up @@ -66,7 +67,7 @@ protected override async Task<IEnumerable<CodeActionOperation>> ComputeOperation
// we already have our destination type, but we need to find the document it is in
// When it is an existing type, "FileName" points to a full path rather than just the name
// There should be no two docs that have the same file path
var destinationDocId = _document.Project.Solution.GetDocumentIdsWithFilePath(moveOptions.FileName).Single();
var destinationDocId = sourceDoc.Project.Solution.GetDocumentIdsWithFilePath(moveOptions.FileName).Single();
var fixedSolution = await RefactorAndMoveAsync(
moveOptions.SelectedMembers,
memberNodes,
Expand All @@ -77,7 +78,7 @@ protected override async Task<IEnumerable<CodeActionOperation>> ComputeOperation
sourceDoc.Id,
destinationDocId,
cancellationToken).ConfigureAwait(false);
return new CodeActionOperation[] { new ApplyChangesOperation(fixedSolution) };
return [new ApplyChangesOperation(fixedSolution)];
}

// otherwise, we need to create a destination ourselves
Expand All @@ -100,10 +101,10 @@ protected override async Task<IEnumerable<CodeActionOperation>> ComputeOperation
sourceDoc.Project.Solution,
moveOptions.NamespaceDisplay,
moveOptions.FileName,
_document.Project.Id,
_document.Folders,
sourceDoc.Project.Id,
sourceDoc.Folders,
newType,
_document,
sourceDoc,
_fallbackOptions,
cancellationToken).ConfigureAwait(false);

Expand All @@ -113,7 +114,7 @@ protected override async Task<IEnumerable<CodeActionOperation>> ComputeOperation
newType = (INamedTypeSymbol)destSemanticModel.GetRequiredDeclaredSymbol(destRoot.GetAnnotatedNodes(annotation).Single(), cancellationToken);

// refactor references across the entire solution
var memberReferenceLocations = await FindMemberReferencesAsync(moveOptions.SelectedMembers, newDoc.Project.Solution, cancellationToken).ConfigureAwait(false);
var memberReferenceLocations = await FindMemberReferencesAsync(newDoc.Project.Solution, newDoc.Project.Id, moveOptions.SelectedMembers, cancellationToken).ConfigureAwait(false);
var projectToLocations = memberReferenceLocations.ToLookup(loc => loc.location.Document.Project.Id);
var solutionWithFixedReferences = await RefactorReferencesAsync(projectToLocations, newDoc.Project.Solution, newType, typeArgIndices, cancellationToken).ConfigureAwait(false);

Expand All @@ -130,7 +131,7 @@ protected override async Task<IEnumerable<CodeActionOperation>> ComputeOperation
var pullMembersUpOptions = PullMembersUpOptionsBuilder.BuildPullMembersUpOptions(newType, members);
var movedSolution = await MembersPuller.PullMembersUpAsync(sourceDoc, pullMembersUpOptions, _fallbackOptions, cancellationToken).ConfigureAwait(false);

return new CodeActionOperation[] { new ApplyChangesOperation(movedSolution) };
return [new ApplyChangesOperation(movedSolution)];
}

/// <summary>
Expand Down Expand Up @@ -174,7 +175,8 @@ private async Task<Solution> RefactorAndMoveAsync(
oldSolution = newTypeDoc.WithSyntaxRoot(newTypeRoot).Project.Solution;

// refactor references across the entire solution
var memberReferenceLocations = await FindMemberReferencesAsync(selectedMembers, oldSolution, cancellationToken).ConfigureAwait(false);
var memberReferenceLocations = await FindMemberReferencesAsync(
oldSolution, sourceDocId.ProjectId, selectedMembers, cancellationToken).ConfigureAwait(false);
var projectToLocations = memberReferenceLocations.ToLookup(loc => loc.location.Document.Project.Id);
var solutionWithFixedReferences = await RefactorReferencesAsync(projectToLocations, oldSolution, newType, typeArgIndices, cancellationToken).ConfigureAwait(false);

Expand Down Expand Up @@ -326,11 +328,27 @@ private static async Task<SyntaxNode> FixReferencesSingleDocumentAsync(
}

private static async Task<ImmutableArray<(ReferenceLocation location, bool isExtension)>> FindMemberReferencesAsync(
ImmutableArray<ISymbol> members,
Solution solution,
ProjectId projectId,
ImmutableArray<ISymbol> members,
CancellationToken cancellationToken)
{
var tasks = members.Select(symbol => SymbolFinder.FindReferencesAsync(symbol, solution, cancellationToken));
var project = solution.GetRequiredProject(projectId);
var compilation = await project.GetRequiredCompilationAsync(cancellationToken).ConfigureAwait(false);

using var _ = ArrayBuilder<Task<IEnumerable<ReferencedSymbol>>>.GetInstance(out var tasks);
foreach (var member in members)
{
tasks.Add(Task.Run(async () =>
{
var symbolKey = member.GetSymbolKey(cancellationToken);
var resolvedMember = symbolKey.Resolve(compilation, ignoreAssemblyKey: false, cancellationToken).GetAnySymbol();
return resolvedMember is null
? []
: await SymbolFinder.FindReferencesAsync(resolvedMember, solution, cancellationToken).ConfigureAwait(false);
}));
}

var symbolRefs = await Task.WhenAll(tasks).ConfigureAwait(false);
return symbolRefs
.Flatten()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -290,19 +290,20 @@ private async ValueTask ProcessDocumentAsync(
await RoslynParallel.ForEachAsync(
symbols,
GetParallelOptions(cancellationToken),
async (symbol, cancellationToken) =>
(symbol, cancellationToken) =>
{
// symbolToGlobalAliases is safe to read in parallel. It is created fully before this point and is no
// longer mutated.
var state = new FindReferencesDocumentState(
cache, TryGet(symbolToGlobalAliases, symbol));

await ProcessDocumentAsync(symbol, state, onReferenceFound).ConfigureAwait(false);
ProcessDocument(symbol, state, onReferenceFound);
return ValueTaskFactory.CompletedTask;
}).ConfigureAwait(false);

return;

async Task ProcessDocumentAsync(
void ProcessDocument(
ISymbol symbol, FindReferencesDocumentState state, Action<Reference> onReferenceFound)
{
cancellationToken.ThrowIfCancellationRequested();
Expand All @@ -318,12 +319,12 @@ async Task ProcessDocumentAsync(
// and only do interesting work on the single relevant one.
foreach (var finder in _finders)
{
await finder.FindReferencesInDocumentAsync(
finder.FindReferencesInDocument(
symbol, state,
static (loc, tuple) => tuple.onReferenceFound((tuple.group, tuple.symbol, loc.Location)),
(group, symbol, onReferenceFound),
_options,
cancellationToken).ConfigureAwait(false);
cancellationToken);
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -113,8 +113,8 @@ async ValueTask DirectSymbolSearchAsync(ISymbol symbol, FindReferencesDocumentSt
using var _ = ArrayBuilder<FinderLocation>.GetInstance(out var referencesForFinder);
foreach (var finder in _finders)
{
await finder.FindReferencesInDocumentAsync(
symbol, state, StandardCallbacks<FinderLocation>.AddToArrayBuilder, referencesForFinder, _options, cancellationToken).ConfigureAwait(false);
finder.FindReferencesInDocument(
symbol, state, StandardCallbacks<FinderLocation>.AddToArrayBuilder, referencesForFinder, _options, cancellationToken);
}

if (referencesForFinder.Count > 0)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ protected sealed override Task DetermineDocumentsToSearchAsync<TData>(
return Task.CompletedTask;
}

protected sealed override ValueTask FindReferencesInDocumentAsync<TData>(
protected sealed override void FindReferencesInDocument<TData>(
TSymbol symbol,
FindReferencesDocumentState state,
Action<FinderLocation, TData> processResult,
Expand All @@ -67,8 +67,6 @@ protected sealed override ValueTask FindReferencesInDocumentAsync<TData>(
var tokens = FindMatchingIdentifierTokens(state, symbol.Name, cancellationToken);
FindReferencesInTokens(symbol, state, tokens, processResult, processResultData, cancellationToken);
}

return ValueTaskFactory.CompletedTask;
}

private static ISymbol? GetContainer(ISymbol symbol)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ public abstract ValueTask<ImmutableArray<ISymbol>> DetermineCascadedSymbolsAsync
public abstract Task DetermineDocumentsToSearchAsync<TData>(
ISymbol symbol, HashSet<string>? globalAliases, Project project, IImmutableSet<Document>? documents, Action<Document, TData> processResult, TData processResultData, FindReferencesSearchOptions options, CancellationToken cancellationToken);

public abstract ValueTask FindReferencesInDocumentAsync<TData>(
public abstract void FindReferencesInDocument<TData>(
ISymbol symbol, FindReferencesDocumentState state, Action<FinderLocation, TData> processResult, TData processResultData, FindReferencesSearchOptions options, CancellationToken cancellationToken);

private static (bool matched, CandidateReason reason) SymbolsMatch(
Expand Down Expand Up @@ -837,7 +837,7 @@ protected abstract Task DetermineDocumentsToSearchAsync<TData>(
Action<Document, TData> processResult, TData processResultData,
FindReferencesSearchOptions options, CancellationToken cancellationToken);

protected abstract ValueTask FindReferencesInDocumentAsync<TData>(
protected abstract void FindReferencesInDocument<TData>(
TSymbol symbol, FindReferencesDocumentState state,
Action<FinderLocation, TData> processResult, TData processResultData,
FindReferencesSearchOptions options, CancellationToken cancellationToken);
Expand Down Expand Up @@ -867,12 +867,11 @@ public sealed override Task DetermineDocumentsToSearchAsync<TData>(
return Task.CompletedTask;
}

public sealed override ValueTask FindReferencesInDocumentAsync<TData>(
public sealed override void FindReferencesInDocument<TData>(
ISymbol symbol, FindReferencesDocumentState state, Action<FinderLocation, TData> processResult, TData processResultData, FindReferencesSearchOptions options, CancellationToken cancellationToken)
{
return symbol is TSymbol typedSymbol && CanFind(typedSymbol)
? FindReferencesInDocumentAsync(typedSymbol, state, processResult, processResultData, options, cancellationToken)
: ValueTaskFactory.CompletedTask;
if (symbol is TSymbol typedSymbol && CanFind(typedSymbol))
FindReferencesInDocument(typedSymbol, state, processResult, processResultData, options, cancellationToken);
}

public sealed override ValueTask<ImmutableArray<ISymbol>> DetermineCascadedSymbolsAsync(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ namespace Microsoft.CodeAnalysis.FindSymbols.Finders;

internal abstract class AbstractTypeParameterSymbolReferenceFinder : AbstractReferenceFinder<ITypeParameterSymbol>
{
protected sealed override ValueTask FindReferencesInDocumentAsync<TData>(
protected sealed override void FindReferencesInDocument<TData>(
ITypeParameterSymbol symbol,
FindReferencesDocumentState state,
Action<FinderLocation, TData> processResult,
Expand Down Expand Up @@ -43,7 +43,7 @@ protected sealed override ValueTask FindReferencesInDocumentAsync<TData>(
processResult,
processResultData);

return ValueTaskFactory.CompletedTask;
return;

static bool IsObjectCreationToken(SyntaxToken token, FindReferencesDocumentState state)
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ protected override Task DetermineDocumentsToSearchAsync<TData>(
}, symbol.ContainingType.Name, processResult, processResultData, cancellationToken);
}

protected sealed override ValueTask FindReferencesInDocumentAsync<TData>(
protected sealed override void FindReferencesInDocument<TData>(
IMethodSymbol methodSymbol,
FindReferencesDocumentState state,
Action<FinderLocation, TData> processResult,
Expand All @@ -68,7 +68,7 @@ protected sealed override ValueTask FindReferencesInDocumentAsync<TData>(
(state, methodSymbol, cancellationToken));

FindReferencesInTokens(methodSymbol, state, totalTokens, processResult, processResultData, cancellationToken);
return ValueTaskFactory.CompletedTask;
return;

// local functions
static bool TokensMatch(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@

namespace Microsoft.CodeAnalysis.FindSymbols.Finders;

internal class ConstructorSymbolReferenceFinder : AbstractReferenceFinder<IMethodSymbol>
internal sealed class ConstructorSymbolReferenceFinder : AbstractReferenceFinder<IMethodSymbol>
{
public static readonly ConstructorSymbolReferenceFinder Instance = new();

Expand Down Expand Up @@ -90,7 +90,7 @@ private static bool IsPotentialReference(PredefinedType predefinedType, ISyntaxF
=> syntaxFacts.TryGetPredefinedType(token, out var actualType) &&
predefinedType == actualType;

protected override ValueTask FindReferencesInDocumentAsync<TData>(
protected override void FindReferencesInDocument<TData>(
IMethodSymbol methodSymbol,
FindReferencesDocumentState state,
Action<FinderLocation, TData> processResult,
Expand Down Expand Up @@ -126,8 +126,6 @@ protected override ValueTask FindReferencesInDocumentAsync<TData>(

FindReferencesInDocumentInsideGlobalSuppressions(
methodSymbol, state, processResult, processResultData, cancellationToken);

return ValueTaskFactory.CompletedTask;
}

/// <summary>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,14 +29,13 @@ protected override Task DetermineDocumentsToSearchAsync<TData>(
return Task.CompletedTask;
}

protected override ValueTask FindReferencesInDocumentAsync<TData>(
protected override void FindReferencesInDocument<TData>(
IMethodSymbol methodSymbol,
FindReferencesDocumentState state,
Action<FinderLocation, TData> processResult,
TData processResultData,
FindReferencesSearchOptions options,
CancellationToken cancellationToken)
{
return ValueTaskFactory.CompletedTask;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@

namespace Microsoft.CodeAnalysis.FindSymbols.Finders;

internal class EventSymbolReferenceFinder : AbstractMethodOrPropertyOrEventSymbolReferenceFinder<IEventSymbol>
internal sealed class EventSymbolReferenceFinder : AbstractMethodOrPropertyOrEventSymbolReferenceFinder<IEventSymbol>
{
protected override bool CanFind(IEventSymbol symbol)
=> true;
Expand Down Expand Up @@ -49,7 +49,7 @@ protected sealed override async Task DetermineDocumentsToSearchAsync<TData>(
await FindDocumentsWithGlobalSuppressMessageAttributeAsync(project, documents, processResult, processResultData, cancellationToken).ConfigureAwait(false);
}

protected sealed override ValueTask FindReferencesInDocumentAsync<TData>(
protected sealed override void FindReferencesInDocument<TData>(
IEventSymbol symbol,
FindReferencesDocumentState state,
Action<FinderLocation, TData> processResult,
Expand All @@ -58,6 +58,5 @@ protected sealed override ValueTask FindReferencesInDocumentAsync<TData>(
CancellationToken cancellationToken)
{
FindReferencesInDocumentUsingSymbolName(symbol, state, processResult, processResultData, cancellationToken);
return ValueTaskFactory.CompletedTask;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ protected sealed override async Task DetermineDocumentsToSearchAsync<TData>(
}
}

protected sealed override ValueTask FindReferencesInDocumentAsync<TData>(
protected sealed override void FindReferencesInDocument<TData>(
IMethodSymbol symbol,
FindReferencesDocumentState state,
Action<FinderLocation, TData> processResult,
Expand All @@ -74,7 +74,6 @@ protected sealed override ValueTask FindReferencesInDocumentAsync<TData>(
state);

FindReferencesInTokens(symbol, state, tokens, processResult, processResultData, cancellationToken);
return ValueTaskFactory.CompletedTask;
}

private static bool IsPotentialReference(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ protected sealed override Task DetermineDocumentsToSearchAsync<TData>(
return Task.CompletedTask;
}

protected sealed override ValueTask FindReferencesInDocumentAsync<TData>(
protected sealed override void FindReferencesInDocument<TData>(
IMethodSymbol symbol,
FindReferencesDocumentState state,
Action<FinderLocation, TData> processResult,
Expand All @@ -39,6 +39,5 @@ protected sealed override ValueTask FindReferencesInDocumentAsync<TData>(
CancellationToken cancellationToken)
{
// An explicit method can't be referenced anywhere.
return ValueTaskFactory.CompletedTask;
}
}
Loading

0 comments on commit 248da13

Please sign in to comment.