From 54e4b34cb9b33225aab103bb93704fcaf190936f Mon Sep 17 00:00:00 2001 From: tmat Date: Thu, 10 Feb 2022 11:18:17 -0800 Subject: [PATCH] Fixes --- .../Core}/FindUsages/SimpleFindUsagesContext.cs | 8 +++++++- .../Handlers/References/FindAllReferencesHandler.cs | 1 - .../Handlers/References/FindImplementationsHandler.cs | 1 - .../Handlers/References/FindUsagesLSPContext.cs | 6 +++++- .../FindReferencesCommandHandlerTests.cs | 8 +++++--- .../FindReferencesCommandHandlerTests.vb | 2 +- .../Test2/FindReferences/FindReferencesTests.vb | 9 ++++++--- .../Core/Portable/FindUsages/FindUsagesContext.cs | 8 ++------ .../Core/Portable/FindUsages/FindUsagesOptions.cs | 10 +++++++++- .../AbstractTableDataSourceFindUsagesContext.cs | 6 +++++- 10 files changed, 40 insertions(+), 19 deletions(-) rename src/{Features/Core/Portable => EditorFeatures/Core}/FindUsages/SimpleFindUsagesContext.cs (88%) diff --git a/src/Features/Core/Portable/FindUsages/SimpleFindUsagesContext.cs b/src/EditorFeatures/Core/FindUsages/SimpleFindUsagesContext.cs similarity index 88% rename from src/Features/Core/Portable/FindUsages/SimpleFindUsagesContext.cs rename to src/EditorFeatures/Core/FindUsages/SimpleFindUsagesContext.cs index 926a3718a7859..8be625c226bb8 100644 --- a/src/Features/Core/Portable/FindUsages/SimpleFindUsagesContext.cs +++ b/src/EditorFeatures/Core/FindUsages/SimpleFindUsagesContext.cs @@ -9,6 +9,7 @@ using System.Threading.Tasks; using Microsoft.CodeAnalysis.FindUsages; using Microsoft.CodeAnalysis.Options; +using Roslyn.Utilities; namespace Microsoft.CodeAnalysis.FindUsages { @@ -19,6 +20,8 @@ namespace Microsoft.CodeAnalysis.FindUsages internal sealed class SimpleFindUsagesContext : FindUsagesContext { private readonly object _gate = new(); + private readonly IGlobalOptionService _globalOptions; + private readonly ImmutableArray.Builder _definitionItems = ImmutableArray.CreateBuilder(); @@ -26,13 +29,16 @@ internal sealed class SimpleFindUsagesContext : FindUsagesContext ImmutableArray.CreateBuilder(); public SimpleFindUsagesContext(IGlobalOptionService globalOptions) - : base(globalOptions) { + _globalOptions = globalOptions; } public string Message { get; private set; } public string SearchTitle { get; private set; } + public override ValueTask GetOptionsAsync(string language, CancellationToken cancellationToken) + => ValueTaskFactory.FromResult(_globalOptions.GetFindUsagesOptions(language)); + public override ValueTask ReportMessageAsync(string message, CancellationToken cancellationToken) { Message = message; diff --git a/src/EditorFeatures/Core/Implementation/LanguageServer/Handlers/References/FindAllReferencesHandler.cs b/src/EditorFeatures/Core/Implementation/LanguageServer/Handlers/References/FindAllReferencesHandler.cs index 42a72ea4ee48e..858a994a40d2e 100644 --- a/src/EditorFeatures/Core/Implementation/LanguageServer/Handlers/References/FindAllReferencesHandler.cs +++ b/src/EditorFeatures/Core/Implementation/LanguageServer/Handlers/References/FindAllReferencesHandler.cs @@ -7,7 +7,6 @@ using System.Diagnostics; using System.Threading; using System.Threading.Tasks; -using Microsoft.CodeAnalysis.Editor.FindUsages; using Microsoft.CodeAnalysis.FindUsages; using Microsoft.CodeAnalysis.Host.Mef; using Microsoft.CodeAnalysis.LanguageServer.CustomProtocol; diff --git a/src/EditorFeatures/Core/Implementation/LanguageServer/Handlers/References/FindImplementationsHandler.cs b/src/EditorFeatures/Core/Implementation/LanguageServer/Handlers/References/FindImplementationsHandler.cs index 88c0e3e83088a..f999ac967aa35 100644 --- a/src/EditorFeatures/Core/Implementation/LanguageServer/Handlers/References/FindImplementationsHandler.cs +++ b/src/EditorFeatures/Core/Implementation/LanguageServer/Handlers/References/FindImplementationsHandler.cs @@ -5,7 +5,6 @@ using System.Composition; using System.Threading; using System.Threading.Tasks; -using Microsoft.CodeAnalysis.Editor.FindUsages; using Microsoft.CodeAnalysis.FindUsages; using Microsoft.CodeAnalysis.Host.Mef; using Microsoft.CodeAnalysis.Options; diff --git a/src/EditorFeatures/Core/Implementation/LanguageServer/Handlers/References/FindUsagesLSPContext.cs b/src/EditorFeatures/Core/Implementation/LanguageServer/Handlers/References/FindUsagesLSPContext.cs index e4a7218a913d1..3da4dac625de0 100644 --- a/src/EditorFeatures/Core/Implementation/LanguageServer/Handlers/References/FindUsagesLSPContext.cs +++ b/src/EditorFeatures/Core/Implementation/LanguageServer/Handlers/References/FindUsagesLSPContext.cs @@ -34,6 +34,7 @@ internal sealed class FindUsagesLSPContext : FindUsagesContext private readonly Document _document; private readonly int _position; private readonly IMetadataAsSourceFileService _metadataAsSourceFileService; + private readonly IGlobalOptionService _globalOptions; /// /// Methods in FindUsagesLSPContext can be called by multiple threads concurrently. @@ -78,16 +79,19 @@ public FindUsagesLSPContext( IAsynchronousOperationListener asyncListener, IGlobalOptionService globalOptions, CancellationToken cancellationToken) - : base(globalOptions) { _progress = progress; _document = document; _position = position; _metadataAsSourceFileService = metadataAsSourceFileService; + _globalOptions = globalOptions; _workQueue = new AsyncBatchingWorkQueue( TimeSpan.FromMilliseconds(500), ReportReferencesAsync, asyncListener, cancellationToken); } + public override ValueTask GetOptionsAsync(string language, CancellationToken cancellationToken) + => ValueTaskFactory.FromResult(_globalOptions.GetFindUsagesOptions(language)); + // After all definitions/references have been found, wait here until all results have been reported. public override async ValueTask OnCompletedAsync(CancellationToken cancellationToken) => await _workQueue.WaitUntilCurrentBatchCompletesAsync().ConfigureAwait(false); diff --git a/src/EditorFeatures/Test/FindReferences/FindReferencesCommandHandlerTests.cs b/src/EditorFeatures/Test/FindReferences/FindReferencesCommandHandlerTests.cs index c995272eec89f..9843a1c315aa2 100644 --- a/src/EditorFeatures/Test/FindReferences/FindReferencesCommandHandlerTests.cs +++ b/src/EditorFeatures/Test/FindReferences/FindReferencesCommandHandlerTests.cs @@ -32,11 +32,13 @@ private class MockFindUsagesContext : FindUsagesContext { public readonly List Result = new(); - public MockFindUsagesContext(IGlobalOptionService globalOptions) - : base(globalOptions) + public MockFindUsagesContext() { } + public override ValueTask GetOptionsAsync(string language, CancellationToken cancellationToken) + => ValueTaskFactory.FromResult(FindUsagesOptions.Default); + public override ValueTask OnDefinitionFoundAsync(DefinitionItem definition, CancellationToken cancellationToken) { lock (Result) @@ -70,7 +72,7 @@ public void ClearAll() public async Task TestFindReferencesAsynchronousCall() { using var workspace = TestWorkspace.CreateCSharp("class C { C() { new C(); } }"); - var context = new MockFindUsagesContext(workspace.GlobalOptions); + var context = new MockFindUsagesContext(); var presenter = new MockStreamingFindUsagesPresenter(context); var listenerProvider = workspace.ExportProvider.GetExportedValue(); diff --git a/src/EditorFeatures/Test2/FindReferences/FindReferencesCommandHandlerTests.vb b/src/EditorFeatures/Test2/FindReferences/FindReferencesCommandHandlerTests.vb index c2d968de81c59..9e1b933f67fa8 100644 --- a/src/EditorFeatures/Test2/FindReferences/FindReferencesCommandHandlerTests.vb +++ b/src/EditorFeatures/Test2/FindReferences/FindReferencesCommandHandlerTests.vb @@ -38,7 +38,7 @@ class C Dim listenerProvider = workspace.ExportProvider.GetExportedValue(Of IAsynchronousOperationListenerProvider) - Dim context = New FindReferencesTests.TestContext(workspace.GlobalOptions) + Dim context = New FindReferencesTests.TestContext() Dim commandHandler = New FindReferencesCommandHandler( New MockStreamingFindReferencesPresenter(context), workspace.GlobalOptions, diff --git a/src/EditorFeatures/Test2/FindReferences/FindReferencesTests.vb b/src/EditorFeatures/Test2/FindReferences/FindReferencesTests.vb index 24e81c54de8df..5a6fa64e6c50e 100644 --- a/src/EditorFeatures/Test2/FindReferences/FindReferencesTests.vb +++ b/src/EditorFeatures/Test2/FindReferences/FindReferencesTests.vb @@ -72,7 +72,7 @@ Namespace Microsoft.CodeAnalysis.Editor.UnitTests.FindReferences Assert.NotNull(startDocument) Dim findRefsService = startDocument.GetLanguageService(Of IFindUsagesService) - Dim context = New TestContext(workspace.GlobalOptions) + Dim context = New TestContext() Await findRefsService.FindReferencesAsync(context, startDocument, cursorPosition, CancellationToken.None) Dim expectedDefinitions = @@ -224,10 +224,13 @@ Namespace Microsoft.CodeAnalysis.Editor.UnitTests.FindReferences Public ReadOnly Definitions As List(Of DefinitionItem) = New List(Of DefinitionItem)() Public ReadOnly References As List(Of SourceReferenceItem) = New List(Of SourceReferenceItem)() - Public Sub New(globalOptions As IGlobalOptionService) - MyBase.New(globalOptions) + Public Sub New() End Sub + Public Overrides Function GetOptionsAsync(language As String, cancellationToken As CancellationToken) As ValueTask(Of FindUsagesOptions) + Return ValueTaskFactory.FromResult(FindUsagesOptions.Default) + End Function + Public Function ShouldShow(definition As DefinitionItem) As Boolean If References.Any(Function(r) r.Definition Is definition) Then Return True diff --git a/src/Features/Core/Portable/FindUsages/FindUsagesContext.cs b/src/Features/Core/Portable/FindUsages/FindUsagesContext.cs index 0d4cd684c13e0..64ccc1393cef3 100644 --- a/src/Features/Core/Portable/FindUsages/FindUsagesContext.cs +++ b/src/Features/Core/Portable/FindUsages/FindUsagesContext.cs @@ -12,18 +12,14 @@ namespace Microsoft.CodeAnalysis.FindUsages { internal abstract class FindUsagesContext : IFindUsagesContext { - private readonly IGlobalOptionService _globalOptions; - public IStreamingProgressTracker ProgressTracker { get; } - protected FindUsagesContext(IGlobalOptionService globalOptions) + protected FindUsagesContext() { ProgressTracker = new StreamingProgressTracker(ReportProgressAsync); - _globalOptions = globalOptions; } - public ValueTask GetOptionsAsync(string language, CancellationToken cancellationToken) - => ValueTaskFactory.FromResult(_globalOptions.GetFindUsagesOptions(language)); + public abstract ValueTask GetOptionsAsync(string language, CancellationToken cancellationToken); public virtual ValueTask ReportMessageAsync(string message, CancellationToken cancellationToken) => default; diff --git a/src/Features/Core/Portable/FindUsages/FindUsagesOptions.cs b/src/Features/Core/Portable/FindUsages/FindUsagesOptions.cs index 806bcd52e26dd..99c48595a6e8b 100644 --- a/src/Features/Core/Portable/FindUsages/FindUsagesOptions.cs +++ b/src/Features/Core/Portable/FindUsages/FindUsagesOptions.cs @@ -9,5 +9,13 @@ namespace Microsoft.CodeAnalysis.FindUsages { [DataContract] internal readonly record struct FindUsagesOptions( - [property: DataMember(Order = 0)] ClassificationOptions ClassificationOptions); + [property: DataMember(Order = 0)] ClassificationOptions ClassificationOptions) + { + public FindUsagesOptions() + : this(ClassificationOptions.Default) + { + } + + public static readonly FindUsagesOptions Default = new(); + } } diff --git a/src/VisualStudio/Core/Def/Implementation/FindReferences/Contexts/AbstractTableDataSourceFindUsagesContext.cs b/src/VisualStudio/Core/Def/Implementation/FindReferences/Contexts/AbstractTableDataSourceFindUsagesContext.cs index 64691856f8c5a..0accad77828d9 100644 --- a/src/VisualStudio/Core/Def/Implementation/FindReferences/Contexts/AbstractTableDataSourceFindUsagesContext.cs +++ b/src/VisualStudio/Core/Def/Implementation/FindReferences/Contexts/AbstractTableDataSourceFindUsagesContext.cs @@ -55,6 +55,7 @@ private abstract class AbstractTableDataSourceFindUsagesContext : public readonly StreamingFindUsagesPresenter Presenter; private readonly IFindAllReferencesWindow _findReferencesWindow; + private readonly IGlobalOptionService _globalOptions; protected readonly IWpfTableControl2 TableControl; private readonly AsyncBatchingWorkQueue<(int current, int maximum)> _progressQueue; @@ -117,12 +118,12 @@ protected AbstractTableDataSourceFindUsagesContext( IGlobalOptionService globalOptions, bool includeContainingTypeAndMemberColumns, bool includeKindColumn) - : base(globalOptions) { presenter.AssertIsForeground(); Presenter = presenter; _findReferencesWindow = findReferencesWindow; + _globalOptions = globalOptions; TableControl = (IWpfTableControl2)findReferencesWindow.TableControl; TableControl.GroupingsChanged += OnTableControlGroupingsChanged; @@ -157,6 +158,9 @@ protected AbstractTableDataSourceFindUsagesContext( CancellationTokenSource.Token); } + public override ValueTask GetOptionsAsync(string language, CancellationToken cancellationToken) + => ValueTaskFactory.FromResult(_globalOptions.GetFindUsagesOptions(language)); + private static ImmutableArray SelectCustomColumnsToInclude(ImmutableArray customColumns, bool includeContainingTypeAndMemberColumns, bool includeKindColumn) { var customColumnsToInclude = ArrayBuilder.GetInstance();