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 expressions and method calls #52127

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -6,15 +6,16 @@
using System.Threading;
using System.Threading.Tasks;
using Microsoft.CodeAnalysis.Host;
using Microsoft.CodeAnalysis.Text;

namespace Microsoft.CodeAnalysis.ValueTracking
{
internal interface IValueTrackingService : IWorkspaceService
{
Task<ImmutableArray<ValueTrackedItem>> TrackValueSourceAsync(Solution solution, ISymbol symbol, CancellationToken cancellationToken);
Task TrackValueSourceAsync(Solution solution, ISymbol symbol, ValueTrackingProgressCollector progressCollector, CancellationToken cancellationToken);
Task<ImmutableArray<ValueTrackedItem>> TrackValueSourceAsync(TextSpan selection, Document document, CancellationToken cancellationToken);
Copy link
Contributor

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?

Copy link
Contributor Author

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

Task TrackValueSourceAsync(TextSpan selection, Document document, ValueTrackingProgressCollector progressCollector, CancellationToken cancellationToken);

Task<ImmutableArray<ValueTrackedItem>> TrackValueSourceAsync(Solution solution, ValueTrackedItem previousTrackedItem, CancellationToken cancellationToken);
Task TrackValueSourceAsync(Solution solution, ValueTrackedItem previousTrackedItem, ValueTrackingProgressCollector progressCollector, CancellationToken cancellationToken);
Task<ImmutableArray<ValueTrackedItem>> TrackValueSourceAsync(ValueTrackedItem previousTrackedItem, CancellationToken cancellationToken);
Task TrackValueSourceAsync(ValueTrackedItem previousTrackedItem, ValueTrackingProgressCollector progressCollector, CancellationToken cancellationToken);
}
}
6 changes: 6 additions & 0 deletions src/EditorFeatures/Core/ValueTracking/ValueTrackedItem.cs
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,12 @@ private ValueTrackedItem(
Document = document;
}

public override string ToString()
{
var subText = SourceText.GetSubText(Span);
return subText.ToString();
}

public static async Task<ValueTrackedItem?> TryCreateAsync(Solution solution, Location location, ISymbol symbol, ValueTrackedItem? parent = null, CancellationToken cancellationToken = default)
{
Contract.ThrowIfNull(location.SourceTree);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@
using System;
using System.Collections.Generic;
using System.Collections.Immutable;
using System.Threading;
using System.Threading.Tasks;

namespace Microsoft.CodeAnalysis.ValueTracking
{
Expand All @@ -15,6 +17,8 @@ internal class ValueTrackingProgressCollector : IProgress<ValueTrackedItem>

public event EventHandler<ValueTrackedItem>? OnNewItem;

internal ValueTrackedItem? Parent { get; set; }

public void Report(ValueTrackedItem item)
{
lock (_lock)
Expand All @@ -32,5 +36,17 @@ public ImmutableArray<ValueTrackedItem> GetItems()
return _items.ToImmutableArray();
}
}

internal async Task<bool> TryReportAsync(Solution solution, Location location, ISymbol symbol, CancellationToken cancellationToken = default)
{
var item = await ValueTrackedItem.TryCreateAsync(solution, location, symbol, Parent, cancellationToken).ConfigureAwait(false);
if (item is not null)
{
Report(item);
return true;
}

return false;
}
}
}
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);
}
}
}
}
Loading