-
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
Add value tracking service #51898
Merged
14 commits merged into
dotnet:features/value_tracking
from
ryzngard:feature/value_tracking/service
Mar 19, 2021
Merged
Add value tracking service #51898
Changes from 13 commits
Commits
Show all changes
14 commits
Select commit
Hold shift + click to select a range
fd9b756
Merge pull request #51899 from dotnet/main
ryzngard 1727f28
Working UI and some functions
ryzngard a9b6e07
Add unit tests
ryzngard 03999cc
Remove package changes
ryzngard 085e7c3
WIP
ryzngard 7920278
Add more tests
ryzngard 2906847
Undo unneeded changes
ryzngard 1dd8dc3
Add comments to test. Make abstract base for tests in prep for VB
ryzngard 465dc80
Fix test accessibility modifier
ryzngard ec0ca3c
WIP
ryzngard 87c0fa6
Update tests to use line base. Use SymbolFinder
ryzngard ef3c027
Use ProgressTracking for SymbolFinder
ryzngard 86ba825
Fix correctness build
ryzngard cdfb8dc
PR feedback
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
20 changes: 20 additions & 0 deletions
20
src/EditorFeatures/Core/ValueTracking/IValueTrackingService.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,20 @@ | ||
// 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.Collections.Immutable; | ||
using System.Threading; | ||
using System.Threading.Tasks; | ||
using Microsoft.CodeAnalysis.Host; | ||
|
||
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(Solution solution, ValueTrackedItem previousTrackedItem, CancellationToken cancellationToken); | ||
Task TrackValueSourceAsync(Solution solution, ValueTrackedItem previousTrackedItem, ValueTrackingProgressCollector progressCollector, CancellationToken cancellationToken); | ||
} | ||
} |
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,20 @@ | ||
// 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. | ||
|
||
namespace Microsoft.CodeAnalysis.ValueTracking | ||
{ | ||
internal class ValueTrackedItem | ||
{ | ||
public Location Location { get; } | ||
public ISymbol Symbol { get; } | ||
|
||
public ValueTrackedItem( | ||
Location location, | ||
ISymbol symbol) | ||
{ | ||
Location = location; | ||
Symbol = symbol; | ||
} | ||
} | ||
} |
36 changes: 36 additions & 0 deletions
36
src/EditorFeatures/Core/ValueTracking/ValueTrackingProgressCollector.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,36 @@ | ||
// 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.Generic; | ||
using System.Collections.Immutable; | ||
|
||
namespace Microsoft.CodeAnalysis.ValueTracking | ||
{ | ||
internal class ValueTrackingProgressCollector | ||
ryzngard marked this conversation as resolved.
Show resolved
Hide resolved
|
||
{ | ||
private readonly object _lock = new(); | ||
private readonly Stack<ValueTrackedItem> _items = new(); | ||
|
||
public event EventHandler<ValueTrackedItem>? OnNewItem; | ||
|
||
public void Push(ValueTrackedItem item) | ||
{ | ||
lock (_lock) | ||
{ | ||
_items.Push(item); | ||
} | ||
|
||
OnNewItem?.Invoke(null, item); | ||
} | ||
|
||
public ImmutableArray<ValueTrackedItem> GetItems() | ||
{ | ||
lock (_lock) | ||
{ | ||
return _items.ToImmutableArray(); | ||
} | ||
} | ||
} | ||
} |
116 changes: 116 additions & 0 deletions
116
src/EditorFeatures/Core/ValueTracking/ValueTrackingService.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,116 @@ | ||
// 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.Composition; | ||
using System.Threading; | ||
using System.Threading.Tasks; | ||
using Microsoft.CodeAnalysis.FindSymbols; | ||
using Microsoft.CodeAnalysis.Host.Mef; | ||
using Microsoft.CodeAnalysis.Shared.Utilities; | ||
|
||
namespace Microsoft.CodeAnalysis.ValueTracking | ||
{ | ||
[ExportWorkspaceService(typeof(IValueTrackingService)), Shared] | ||
internal class ValueTrackingService : IValueTrackingService | ||
{ | ||
[ImportingConstructor] | ||
[Obsolete(MefConstruction.ImportingConstructorMessage, error: true)] | ||
public ValueTrackingService() | ||
{ | ||
} | ||
|
||
public async Task<ImmutableArray<ValueTrackedItem>> TrackValueSourceAsync( | ||
Solution solution, | ||
ISymbol symbol, | ||
CancellationToken cancellationToken) | ||
{ | ||
var progressTracker = new ValueTrackingProgressCollector(); | ||
await TrackValueSourceAsync(solution, symbol, progressTracker, cancellationToken).ConfigureAwait(false); | ||
return progressTracker.GetItems(); | ||
} | ||
|
||
public async Task TrackValueSourceAsync( | ||
Solution solution, | ||
ISymbol symbol, | ||
ValueTrackingProgressCollector progressCollector, | ||
CancellationToken cancellationToken) | ||
{ | ||
if (symbol | ||
is IPropertySymbol | ||
or IFieldSymbol | ||
or ILocalSymbol | ||
or IParameterSymbol) | ||
{ | ||
// Add all initializations of the symbol. Those are not caught in | ||
// the reference finder but should still show up in the tree | ||
foreach (var syntaxRef in symbol.DeclaringSyntaxReferences) | ||
{ | ||
var location = Location.Create(syntaxRef.SyntaxTree, syntaxRef.Span); | ||
progressCollector.Push(new ValueTrackedItem(location, symbol)); | ||
} | ||
|
||
var findReferenceProgressCollector = new FindReferencesProgress(progressCollector); | ||
await SymbolFinder.FindReferencesAsync( | ||
symbol, solution, findReferenceProgressCollector, | ||
documents: null, FindReferencesSearchOptions.Default, cancellationToken).ConfigureAwait(false); | ||
} | ||
} | ||
|
||
public async Task<ImmutableArray<ValueTrackedItem>> TrackValueSourceAsync( | ||
Solution solution, | ||
ValueTrackedItem previousTrackedItem, | ||
CancellationToken cancellationToken) | ||
{ | ||
var progressTracker = new ValueTrackingProgressCollector(); | ||
await TrackValueSourceAsync(solution, previousTrackedItem, progressTracker, cancellationToken).ConfigureAwait(false); | ||
return progressTracker.GetItems(); | ||
} | ||
|
||
public Task TrackValueSourceAsync( | ||
Solution solution, | ||
ValueTrackedItem previousTrackedItem, | ||
ValueTrackingProgressCollector progressCollector, | ||
CancellationToken cancellationToken) | ||
{ | ||
throw new NotImplementedException(); | ||
} | ||
|
||
private class FindReferencesProgress : IStreamingFindReferencesProgress, IStreamingProgressTracker | ||
{ | ||
private readonly ValueTrackingProgressCollector _valueTrackingProgressCollector; | ||
public FindReferencesProgress(ValueTrackingProgressCollector valueTrackingProgressCollector) | ||
{ | ||
_valueTrackingProgressCollector = valueTrackingProgressCollector; | ||
} | ||
|
||
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 ValueTask OnReferenceFoundAsync(ISymbol symbol, ReferenceLocation location) | ||
{ | ||
if (location.IsWrittenTo) | ||
{ | ||
_valueTrackingProgressCollector.Push(new ValueTrackedItem(location.Location, symbol)); | ||
} | ||
|
||
return new(); | ||
} | ||
|
||
public ValueTask OnStartedAsync() => new(); | ||
} | ||
} | ||
} |
54 changes: 54 additions & 0 deletions
54
src/EditorFeatures/Test/ValueTracking/AbstractBaseValueTrackingTests.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,54 @@ | ||
// 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.Collections.Immutable; | ||
using System.Threading.Tasks; | ||
using Microsoft.CodeAnalysis.ValueTracking; | ||
using Microsoft.CodeAnalysis.Editor.UnitTests.Workspaces; | ||
using Microsoft.CodeAnalysis.Shared.Extensions; | ||
using System.Threading; | ||
using Microsoft.CodeAnalysis.Text; | ||
using Xunit; | ||
using Microsoft.CodeAnalysis.Test.Utilities; | ||
|
||
namespace Microsoft.CodeAnalysis.Editor.UnitTests.ValueTracking | ||
{ | ||
public abstract class AbstractBaseValueTrackingTests | ||
{ | ||
internal static async Task<ImmutableArray<ValueTrackedItem>> GetTrackedItemsAsync(TestWorkspace testWorkspace, CancellationToken cancellationToken = default) | ||
{ | ||
var cursorDocument = testWorkspace.DocumentWithCursor; | ||
var document = testWorkspace.CurrentSolution.GetRequiredDocument(cursorDocument.Id); | ||
var syntaxTree = await document.GetRequiredSyntaxTreeAsync(cancellationToken); | ||
var textSpan = new TextSpan(cursorDocument.CursorPosition!.Value, 0); | ||
var location = Location.Create(syntaxTree, textSpan); | ||
var symbol = await GetSelectedSymbolAsync(textSpan, document, cancellationToken); | ||
var service = testWorkspace.Services.GetRequiredService<IValueTrackingService>(); | ||
return await service.TrackValueSourceAsync(testWorkspace.CurrentSolution, symbol, cancellationToken); | ||
|
||
} | ||
|
||
internal static async Task<ISymbol> GetSelectedSymbolAsync(TextSpan textSpan, Document document, CancellationToken cancellationToken) | ||
{ | ||
var root = await document.GetRequiredSyntaxRootAsync(cancellationToken).ConfigureAwait(false); | ||
var selectedNode = root.FindNode(textSpan); | ||
|
||
Assert.NotNull(selectedNode); | ||
|
||
var semanticModel = await document.GetRequiredSemanticModelAsync(cancellationToken).ConfigureAwait(false); | ||
var selectedSymbol = | ||
semanticModel.GetSymbolInfo(selectedNode, cancellationToken).Symbol | ||
?? semanticModel.GetDeclaredSymbol(selectedNode, cancellationToken); | ||
|
||
Assert.NotNull(selectedSymbol); | ||
return selectedSymbol!; | ||
} | ||
|
||
internal static void ValidateItem(ValueTrackedItem item, int line) | ||
{ | ||
var lineSpan = item.Location.GetLineSpan(); | ||
Assert.Equal(line, lineSpan.StartLinePosition.Line); | ||
} | ||
} | ||
} |
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.
Could this be a record?
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.
Sure, why not :)
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.
Actually, can we use records in Roslyn yet?