-
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
Handle expressions and method calls #52127
Merged
7 commits merged into
dotnet:features/value_tracking
from
ryzngard:feature/value_tracking/handle_single_expression
Mar 30, 2021
Merged
Changes from all commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
adb3873
Update interface to take in textspan
ryzngard 4d93147
More tests, now covering across method calls
ryzngard dfda2ef
More methods handled correctly
ryzngard 326f950
Fixed arguments for invocation expressions
ryzngard e28fc84
Update src/EditorFeatures/Core/ValueTracking/ValueTrackingService.cs
ryzngard e4ed2b1
Correctness fixes
ryzngard 7b1fabf
Merge branch 'feature/value_tracking/handle_single_expression' of htt…
ryzngard 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
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
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
141 changes: 141 additions & 0 deletions
141
src/EditorFeatures/Core/ValueTracking/ValueTrackingService.FindReferencesProgress.cs
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 |
---|---|---|
@@ -0,0 +1,141 @@ | ||
// Licensed to the .NET Foundation under one or more agreements. | ||
// The .NET Foundation licenses this file to you under the MIT license. | ||
// See the LICENSE file in the project root for more information. | ||
|
||
using System; | ||
using System.Collections.Immutable; | ||
using System.Diagnostics; | ||
using System.Linq; | ||
using System.Reflection.Metadata; | ||
using System.Threading; | ||
using System.Threading.Tasks; | ||
using Microsoft.CodeAnalysis.FindSymbols; | ||
using Microsoft.CodeAnalysis.LanguageServices; | ||
using Microsoft.CodeAnalysis.Operations; | ||
using Microsoft.CodeAnalysis.Shared.Extensions; | ||
using Microsoft.CodeAnalysis.Shared.Utilities; | ||
using Microsoft.CodeAnalysis.Text; | ||
|
||
namespace Microsoft.CodeAnalysis.ValueTracking | ||
{ | ||
internal partial class ValueTrackingService | ||
{ | ||
private class FindReferencesProgress : IStreamingFindReferencesProgress, IStreamingProgressTracker | ||
{ | ||
private readonly CancellationToken _cancellationToken; | ||
private readonly ValueTrackingProgressCollector _valueTrackingProgressCollector; | ||
public FindReferencesProgress(ValueTrackingProgressCollector valueTrackingProgressCollector, CancellationToken cancellationToken = default) | ||
{ | ||
_valueTrackingProgressCollector = valueTrackingProgressCollector; | ||
_cancellationToken = cancellationToken; | ||
} | ||
|
||
public IStreamingProgressTracker ProgressTracker => this; | ||
|
||
public ValueTask AddItemsAsync(int count) => new(); | ||
|
||
public ValueTask ItemCompletedAsync() => new(); | ||
|
||
public ValueTask OnCompletedAsync() => new(); | ||
|
||
public ValueTask OnDefinitionFoundAsync(ISymbol symbol) => new(); | ||
|
||
public ValueTask OnFindInDocumentCompletedAsync(Document document) => new(); | ||
|
||
public ValueTask OnFindInDocumentStartedAsync(Document document) => new(); | ||
|
||
public async ValueTask OnReferenceFoundAsync(ISymbol symbol, ReferenceLocation location) | ||
{ | ||
if (!location.Location.IsInSource) | ||
{ | ||
return; | ||
} | ||
|
||
var solution = location.Document.Project.Solution; | ||
|
||
if (symbol is IMethodSymbol methodSymbol) | ||
{ | ||
if (methodSymbol.IsConstructor()) | ||
{ | ||
await TrackConstructorAsync(location).ConfigureAwait(false); | ||
} | ||
else | ||
{ | ||
// If we're searching for references to a method, we don't want to store the symbol as that method again. Instead | ||
// we want to track the invocations and how to follow their source | ||
await TrackMethodInvocationArgumentsAsync(location).ConfigureAwait(false); | ||
} | ||
} | ||
else if (location.IsWrittenTo) | ||
{ | ||
var syntaxFacts = location.Document.GetRequiredLanguageService<ISyntaxFactsService>(); | ||
var node = location.Location.FindNode(CancellationToken.None); | ||
|
||
if (syntaxFacts.IsLeftSideOfAnyAssignment(node)) | ||
{ | ||
await AddItemsFromAssignmentAsync(location.Document, node, _valueTrackingProgressCollector, _cancellationToken).ConfigureAwait(false); | ||
} | ||
else | ||
{ | ||
await _valueTrackingProgressCollector.TryReportAsync(solution, location.Location, symbol, _cancellationToken).ConfigureAwait(false); | ||
} | ||
} | ||
} | ||
|
||
public ValueTask OnStartedAsync() => new(); | ||
|
||
private async Task TrackConstructorAsync(ReferenceLocation referenceLocation) | ||
{ | ||
var document = referenceLocation.Document; | ||
var span = referenceLocation.Location.SourceSpan; | ||
|
||
var syntaxRoot = await document.GetRequiredSyntaxRootAsync(_cancellationToken).ConfigureAwait(false); | ||
var originalNode = syntaxRoot.FindNode(span); | ||
|
||
if (originalNode is null || originalNode.Parent is null) | ||
{ | ||
return; | ||
} | ||
|
||
var semanticModel = await document.GetRequiredSemanticModelAsync(_cancellationToken).ConfigureAwait(false); | ||
var operation = semanticModel.GetOperation(originalNode.Parent, _cancellationToken); | ||
if (operation is not IObjectCreationOperation objectCreationOperation) | ||
{ | ||
return; | ||
} | ||
|
||
await TrackArgumentsAsync(objectCreationOperation.Arguments, document, _valueTrackingProgressCollector, _cancellationToken).ConfigureAwait(false); | ||
} | ||
|
||
private async Task TrackMethodInvocationArgumentsAsync(ReferenceLocation referenceLocation) | ||
{ | ||
var document = referenceLocation.Document; | ||
var span = referenceLocation.Location.SourceSpan; | ||
|
||
var syntaxRoot = await document.GetRequiredSyntaxRootAsync(_cancellationToken).ConfigureAwait(false); | ||
var originalNode = syntaxRoot.FindNode(span); | ||
|
||
if (originalNode is null) | ||
{ | ||
return; | ||
} | ||
|
||
var syntaxFacts = document.GetRequiredLanguageService<ISyntaxFactsService>(); | ||
var invocationSyntax = originalNode.FirstAncestorOrSelf<SyntaxNode>(syntaxFacts.IsInvocationExpression); | ||
if (invocationSyntax is null) | ||
{ | ||
return; | ||
} | ||
|
||
var semanticModel = await document.GetRequiredSemanticModelAsync(_cancellationToken).ConfigureAwait(false); | ||
var operation = semanticModel.GetOperation(invocationSyntax, _cancellationToken); | ||
if (operation is not IInvocationOperation invocationOperation) | ||
{ | ||
return; | ||
} | ||
|
||
await TrackArgumentsAsync(invocationOperation.Arguments, document, _valueTrackingProgressCollector, _cancellationToken).ConfigureAwait(false); | ||
} | ||
} | ||
} | ||
} |
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.
Why isn't the non-progress-reporting version just an extension/helper method that just collects the items and returns them all in one go?
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 basically is... I can add as an implementation in the interface rather than leaving it as a contract. That seems more clear than an extension method just for code navigation