Skip to content

Commit

Permalink
Remove dependency on IFindUsageContext from DefinitionItemExtensions (#…
Browse files Browse the repository at this point in the history
  • Loading branch information
tmat authored Jan 25, 2024
1 parent 5af1445 commit 52a1271
Show file tree
Hide file tree
Showing 56 changed files with 282 additions and 360 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
using Microsoft.CodeAnalysis.Host.Mef;
using Microsoft.CodeAnalysis.Shared.Utilities;
using Roslyn.Utilities;
using Microsoft.CodeAnalysis.Classification;

namespace Microsoft.CodeAnalysis.ExternalAccess.VSTypeScript
{
Expand All @@ -22,10 +23,10 @@ internal sealed class VSTypeScriptFindUsagesService(IVSTypeScriptFindUsagesServi
{
private readonly IVSTypeScriptFindUsagesService _underlyingService = underlyingService;

public Task FindReferencesAsync(IFindUsagesContext context, Document document, int position, CancellationToken cancellationToken)
public Task FindReferencesAsync(IFindUsagesContext context, Document document, int position, OptionsProvider<ClassificationOptions> classificationOptions, CancellationToken cancellationToken)
=> _underlyingService.FindReferencesAsync(document, position, new Context(context), cancellationToken);

public Task FindImplementationsAsync(IFindUsagesContext context, Document document, int position, CancellationToken cancellationToken)
public Task FindImplementationsAsync(IFindUsagesContext context, Document document, int position, OptionsProvider<ClassificationOptions> classificationOptions, CancellationToken cancellationToken)
=> _underlyingService.FindImplementationsAsync(document, position, new Context(context), cancellationToken);

private sealed class Context(IFindUsagesContext context) : IVSTypeScriptFindUsagesContext
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
using System.ComponentModel.Composition;
using System.Diagnostics.CodeAnalysis;
using System.Threading.Tasks;
using Microsoft.CodeAnalysis.Classification;
using Microsoft.CodeAnalysis.Editor;
using Microsoft.CodeAnalysis.Editor.Host;
using Microsoft.CodeAnalysis.Editor.Shared.Extensions;
Expand Down Expand Up @@ -120,6 +121,7 @@ private async Task StreamingFindReferencesAsync(
try
{
using var token = _asyncListener.BeginAsyncOperation(nameof(StreamingFindReferencesAsync));
var classificationOptions = _globalOptions.GetClassificationOptionsProvider();

// Let the presented know we're starting a search. It will give us back the context object that the FAR
// service will push results into. This operation is not externally cancellable. Instead, the find refs
Expand All @@ -137,7 +139,7 @@ private async Task StreamingFindReferencesAsync(
{
try
{
await findUsagesService.FindReferencesAsync(context, document, caretPosition, cancellationToken).ConfigureAwait(false);
await findUsagesService.FindReferencesAsync(context, document, caretPosition, classificationOptions, cancellationToken).ConfigureAwait(false);
}
finally
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@
using System.Diagnostics.CodeAnalysis;
using System.Threading;
using System.Threading.Tasks;
using Microsoft.CodeAnalysis.Classification;
using Microsoft.CodeAnalysis.Host;
using Microsoft.CodeAnalysis.Options;
using Microsoft.CodeAnalysis.Shared.Utilities;
using Roslyn.Utilities;
Expand All @@ -17,7 +19,7 @@ namespace Microsoft.CodeAnalysis.FindUsages;
/// user immediately if the find command completes quickly, or which will be pushed into the streaming presenter
/// if the search is taking too long.
/// </summary>
internal sealed class BufferedFindUsagesContext(IGlobalOptionService globalOptions) : IFindUsagesContext, IStreamingProgressTracker
internal sealed class BufferedFindUsagesContext : IFindUsagesContext, IStreamingProgressTracker
{
private class State
{
Expand All @@ -29,8 +31,6 @@ private class State
public ImmutableArray<DefinitionItem>.Builder Definitions = ImmutableArray.CreateBuilder<DefinitionItem>();
}

private readonly IGlobalOptionService _globalOptions = globalOptions;

/// <summary>
/// Lock which controls access to all members below.
/// </summary>
Expand Down Expand Up @@ -148,9 +148,6 @@ async ValueTask IStreamingProgressTracker.ItemsCompletedAsync(int count, Cancell

#region IFindUsagesContext

ValueTask<FindUsagesOptions> IFindUsagesContext.GetOptionsAsync(string language, CancellationToken cancellationToken)
=> ValueTaskFactory.FromResult(_globalOptions.GetFindUsagesOptions(language));

async ValueTask IFindUsagesContext.ReportMessageAsync(string message, CancellationToken cancellationToken)
{
using var _ = await _gate.DisposableWaitAsync(cancellationToken).ConfigureAwait(false);
Expand Down
2 changes: 1 addition & 1 deletion src/EditorFeatures/Core/GoToBase/GoToBaseCommandHandler.cs
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,6 @@ internal sealed class GoToBaseCommandHandler(

protected override Task FindActionAsync(IFindUsagesContext context, Document document, int caretPosition, CancellationToken cancellationToken)
=> document.GetRequiredLanguageService<IGoToBaseService>()
.FindBasesAsync(context, document, caretPosition, cancellationToken);
.FindBasesAsync(context, document, caretPosition, ClassificationOptionsProvider, cancellationToken);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@
using System;
using System.Threading;
using System.Threading.Tasks;
using Microsoft.CodeAnalysis.Classification;
using Microsoft.CodeAnalysis.CodeActions;
using Microsoft.CodeAnalysis.Editor.Host;
using Microsoft.CodeAnalysis.Editor.Shared.Extensions;
using Microsoft.CodeAnalysis.Editor.Shared.Tagging;
Expand Down Expand Up @@ -41,7 +43,8 @@ internal abstract class AbstractGoToCommandHandler<TLanguageService, TCommandArg
private readonly IStreamingFindUsagesPresenter _streamingPresenter = streamingPresenter;
private readonly IUIThreadOperationExecutor _uiThreadOperationExecutor = uiThreadOperationExecutor;
private readonly IAsynchronousOperationListener _listener = listener;
private readonly IGlobalOptionService _globalOptions = globalOptions;

public readonly OptionsProvider<ClassificationOptions> ClassificationOptionsProvider = globalOptions.GetClassificationOptionsProvider();

/// <summary>
/// The current go-to command that is in progress. Tracked so that if we issue multiple find-impl commands that
Expand Down Expand Up @@ -165,7 +168,7 @@ private async Task ExecuteCommandWorkerAsync(
// TLanguageService. Once we get the results back we'll then decide what to do with them. If we get only a
// single result back, then we'll just go directly to it. Otherwise, we'll present the results in the
// IStreamingFindUsagesPresenter.
var findContext = new BufferedFindUsagesContext(_globalOptions);
var findContext = new BufferedFindUsagesContext();

var cancellationToken = cancellationTokenSource.Token;
var delayTask = DelayAsync(cancellationToken);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,6 @@ internal sealed class GoToImplementationCommandHandler(

protected override Task FindActionAsync(IFindUsagesContext context, Document document, int caretPosition, CancellationToken cancellationToken)
=> document.GetRequiredLanguageService<IFindUsagesService>()
.FindImplementationsAsync(context, document, caretPosition, cancellationToken);
.FindImplementationsAsync(context, document, caretPosition, ClassificationOptionsProvider, cancellationToken);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -31,13 +31,6 @@ private class MockFindUsagesContext : FindUsagesContext
{
public readonly List<DefinitionItem> Result = new();

public MockFindUsagesContext()
{
}

public override ValueTask<FindUsagesOptions> GetOptionsAsync(string language, CancellationToken cancellationToken)
=> ValueTaskFactory.FromResult(FindUsagesOptions.Default);

public override ValueTask OnDefinitionFoundAsync(DefinitionItem definition, CancellationToken cancellationToken)
{
lock (Result)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
Imports System.Collections.Immutable
Imports System.Threading
Imports Microsoft.CodeAnalysis
Imports Microsoft.CodeAnalysis.Classification
Imports Microsoft.CodeAnalysis.CSharp.Syntax
Imports Microsoft.CodeAnalysis.Editor.UnitTests.Workspaces
Imports Microsoft.CodeAnalysis.FindSymbols
Expand Down Expand Up @@ -74,9 +75,10 @@ Namespace Microsoft.CodeAnalysis.Editor.UnitTests.FindReferences
Await workspace.CurrentSolution.GetSourceGeneratedDocumentAsync(cursorDocument.Id, CancellationToken.None))
Assert.NotNull(startDocument)

Dim classificationOptions = workspace.GlobalOptions.GetClassificationOptionsProvider()
Dim findRefsService = startDocument.GetLanguageService(Of IFindUsagesService)
Dim context = New TestContext()
Await findRefsService.FindReferencesAsync(context, startDocument, cursorPosition, CancellationToken.None)
Await findRefsService.FindReferencesAsync(context, startDocument, cursorPosition, classificationOptions, CancellationToken.None)

Dim expectedDefinitions =
workspace.Documents.Where(Function(d) d.AnnotatedSpans.ContainsKey(DefinitionKey) AndAlso d.AnnotatedSpans(DefinitionKey).Any()).
Expand Down Expand Up @@ -230,10 +232,6 @@ Namespace Microsoft.CodeAnalysis.Editor.UnitTests.FindReferences
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
Expand Down
4 changes: 3 additions & 1 deletion src/EditorFeatures/Test2/GoToBase/GoToBaseTestsBase.vb
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
' See the LICENSE file in the project root for more information.

Imports System.Threading
Imports Microsoft.CodeAnalysis.Classification
Imports Microsoft.CodeAnalysis.FindUsages
Imports Microsoft.CodeAnalysis.GoToBase
Imports Microsoft.CodeAnalysis.Remote.Testing
Expand All @@ -16,7 +17,8 @@ Namespace Microsoft.CodeAnalysis.Editor.UnitTests.GoToBase
testHost:=TestHost.InProcess,
Async Function(document As Document, position As Integer, context As SimpleFindUsagesContext)
Dim gotoBaseService = document.GetLanguageService(Of IGoToBaseService)
Await gotoBaseService.FindBasesAsync(context, document, position, CancellationToken.None)
Dim options = New TestOptionsProvider(Of ClassificationOptions)(ClassificationOptions.Default)
Await gotoBaseService.FindBasesAsync(context, document, position, options, CancellationToken.None)
End Function,
shouldSucceed, metadataDefinitions)
End Function
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ Namespace Microsoft.CodeAnalysis.Editor.UnitTests.GoToDefinition

Dim presenterCalled As Boolean = False
Dim threadingContext = workspace.ExportProvider.GetExportedValue(Of IThreadingContext)()
Dim presenter = New MockStreamingFindUsagesPresenter(workspace.GlobalOptions, Sub() presenterCalled = True)
Dim presenter = New MockStreamingFindUsagesPresenter(Sub() presenterCalled = True)

Dim goToDefService = If(document.Project.Language = LanguageNames.CSharp,
DirectCast(New CSharpDefinitionLocationService(threadingContext, presenter), IDefinitionLocationService),
Expand Down
2 changes: 1 addition & 1 deletion src/EditorFeatures/Test2/GoToHelpers/GoToHelpers.vb
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ Friend Class GoToHelpers
Dim solution = workspace.CurrentSolution
Dim document = Await solution.GetRequiredDocumentAsync(documentWithCursor.Id, includeSourceGenerated:=True)

Dim context = New SimpleFindUsagesContext(workspace.GlobalOptions)
Dim context = New SimpleFindUsagesContext()
Await testingMethod(document, position, context)

If Not shouldSucceed Then
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
Imports Microsoft.CodeAnalysis.Remote.Testing
Imports Microsoft.CodeAnalysis.FindUsages
Imports System.Threading
Imports Microsoft.CodeAnalysis.Classification

Namespace Microsoft.CodeAnalysis.Editor.UnitTests.GoToImplementation
<[UseExportProvider]>
Expand All @@ -17,7 +18,8 @@ Namespace Microsoft.CodeAnalysis.Editor.UnitTests.GoToImplementation
host,
Async Function(document As Document, position As Integer, context As SimpleFindUsagesContext) As Task
Dim findUsagesService = document.GetLanguageService(Of IFindUsagesService)
Await findUsagesService.FindImplementationsAsync(context, document, position, CancellationToken.None).ConfigureAwait(False)
Dim options = New TestOptionsProvider(Of ClassificationOptions)(ClassificationOptions.Default)
Await findUsagesService.FindImplementationsAsync(context, document, position, options, CancellationToken.None).ConfigureAwait(False)
End Function,
shouldSucceed,
metadataDefinitions)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@
Imports System.Threading
Imports Microsoft.CodeAnalysis.Editor.Host
Imports Microsoft.CodeAnalysis.FindUsages
Imports Microsoft.CodeAnalysis.Options

Namespace Microsoft.CodeAnalysis.Editor.UnitTests.Utilities.GoToHelpers
Friend Class MockStreamingFindUsagesPresenter
Expand All @@ -14,9 +13,9 @@ Namespace Microsoft.CodeAnalysis.Editor.UnitTests.Utilities.GoToHelpers
Public ReadOnly Context As SimpleFindUsagesContext
Private ReadOnly _action As Action

Public Sub New(globalOptions As IGlobalOptionService, action As Action)
Public Sub New(action As Action)
_action = action
Context = New SimpleFindUsagesContext(globalOptions)
Context = New SimpleFindUsagesContext()
End Sub

Public Sub ClearAll() Implements IStreamingFindUsagesPresenter.ClearAll
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,10 +19,8 @@ namespace Microsoft.CodeAnalysis.ConvertTupleToStruct
{
internal interface IRemoteConvertTupleToStructCodeRefactoringService
{
// TODO https://github.com/microsoft/vs-streamjsonrpc/issues/789
internal interface ICallback // : IRemoteOptionsCallback<CodeCleanupOptions>
internal interface ICallback : IRemoteOptionsCallback<CleanCodeGenerationOptions>
{
ValueTask<CleanCodeGenerationOptions> GetOptionsAsync(RemoteServiceCallbackId callbackId, string language, CancellationToken cancellationToken);
}

ValueTask<SerializableConvertTupleToStructResult> ConvertToStructAsync(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@
using System.Composition;
using System.Threading;
using System.Threading.Tasks;
using Microsoft.CodeAnalysis.CodeCleanup;
using Microsoft.CodeAnalysis.Host.Mef;
using Microsoft.CodeAnalysis.CodeGeneration;
using Microsoft.CodeAnalysis.Remote;
Expand All @@ -19,10 +18,8 @@ namespace Microsoft.CodeAnalysis.EncapsulateField
{
internal interface IRemoteEncapsulateFieldService
{
// TODO https://github.com/microsoft/vs-streamjsonrpc/issues/789
internal interface ICallback // : IRemoteOptionsCallback<CleanCodeGenerationOptions>
internal interface ICallback : IRemoteOptionsCallback<CleanCodeGenerationOptions>
{
ValueTask<CleanCodeGenerationOptions> GetOptionsAsync(RemoteServiceCallbackId callbackId, string language, CancellationToken cancellationToken);
}

ValueTask<ImmutableArray<(DocumentId, ImmutableArray<TextChange>)>> EncapsulateFieldsAsync(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,9 @@
using System.Collections.Immutable;
using System.Threading;
using System.Threading.Tasks;
using Microsoft.CodeAnalysis.Classification;
using Microsoft.CodeAnalysis.FindUsages;
using Microsoft.CodeAnalysis.Host;
using Microsoft.CodeAnalysis.Shared.Utilities;

namespace Microsoft.CodeAnalysis.FindUsages
Expand All @@ -27,9 +29,6 @@ private sealed class DefinitionTrackingContext(IFindUsagesContext underlyingCont
private readonly object _gate = new();
private readonly List<DefinitionItem> _definitions = new();

public ValueTask<FindUsagesOptions> GetOptionsAsync(string language, CancellationToken cancellationToken)
=> _underlyingContext.GetOptionsAsync(language, cancellationToken);

public IStreamingProgressTracker ProgressTracker
=> _underlyingContext.ProgressTracker;

Expand Down
Loading

0 comments on commit 52a1271

Please sign in to comment.