From e5a0aa900539a690f63c3e3c7e417408e4aca02a Mon Sep 17 00:00:00 2001 From: Sam Harwell Date: Tue, 2 Apr 2019 08:57:09 -0500 Subject: [PATCH] Make IStreamingFindUsagesPresenters a required export --- .../GoToDefinition/CSharpGoToDefinitionService.cs | 6 ++---- .../GoToDefinition/AbstractGoToDefinitionService.cs | 10 ++++------ .../Core/GoToDefinition/GoToDefinitionHelpers.cs | 5 ++--- .../GoToDefinitionCommandHandlerTests.vb | 2 +- .../Test2/GoToDefinition/GoToDefinitionTests.vb | 6 +++--- .../VisualBasicGoToDefinitionService.vb | 4 ++-- .../AbstractObjectBrowserLibraryManager.cs | 11 ++++------- .../Core/Impl/RoslynVisualStudioWorkspace.cs | 6 +++++- .../Test/GoToDefinition/GoToDefinitionApiTests.vb | 3 +-- 9 files changed, 24 insertions(+), 29 deletions(-) diff --git a/src/EditorFeatures/CSharp/GoToDefinition/CSharpGoToDefinitionService.cs b/src/EditorFeatures/CSharp/GoToDefinition/CSharpGoToDefinitionService.cs index 23667a105de..72a903312cb 100644 --- a/src/EditorFeatures/CSharp/GoToDefinition/CSharpGoToDefinitionService.cs +++ b/src/EditorFeatures/CSharp/GoToDefinition/CSharpGoToDefinitionService.cs @@ -1,6 +1,5 @@ // Copyright (c) Microsoft. All Rights Reserved. Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information. -using System; using System.Composition; using Microsoft.CodeAnalysis.Editor.GoToDefinition; using Microsoft.CodeAnalysis.Editor.Host; @@ -12,9 +11,8 @@ namespace Microsoft.CodeAnalysis.Editor.CSharp.GoToDefinition internal class CSharpGoToDefinitionService : AbstractGoToDefinitionService { [ImportingConstructor] - public CSharpGoToDefinitionService( - [Import(AllowDefault = true)] Lazy streamingPresenterOpt) - : base(streamingPresenterOpt) + public CSharpGoToDefinitionService(IStreamingFindUsagesPresenter streamingPresenter) + : base(streamingPresenter) { } } diff --git a/src/EditorFeatures/Core/GoToDefinition/AbstractGoToDefinitionService.cs b/src/EditorFeatures/Core/GoToDefinition/AbstractGoToDefinitionService.cs index e7328327a93..c338b8c6835 100644 --- a/src/EditorFeatures/Core/GoToDefinition/AbstractGoToDefinitionService.cs +++ b/src/EditorFeatures/Core/GoToDefinition/AbstractGoToDefinitionService.cs @@ -1,6 +1,5 @@ // Copyright (c) Microsoft. All Rights Reserved. Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information. -using System; using System.Collections.Generic; using System.Linq; using System.Threading; @@ -16,12 +15,11 @@ namespace Microsoft.CodeAnalysis.Editor.GoToDefinition // GoToDefinition internal abstract class AbstractGoToDefinitionService : IGoToDefinitionService { - private readonly Lazy _streamingPresenterOpt; + private readonly IStreamingFindUsagesPresenter _streamingPresenter; - protected AbstractGoToDefinitionService( - Lazy streamingPresenterOpt) + protected AbstractGoToDefinitionService(IStreamingFindUsagesPresenter streamingPresenter) { - _streamingPresenterOpt = streamingPresenterOpt; + _streamingPresenter = streamingPresenter; } public async Task> FindDefinitionsAsync( @@ -54,7 +52,7 @@ public bool TryGoToDefinition(Document document, int position, CancellationToken return GoToDefinitionHelpers.TryGoToDefinition(symbol, document.Project, - _streamingPresenterOpt, + _streamingPresenter, thirdPartyNavigationAllowed: isThirdPartyNavigationAllowed, throwOnHiddenDefinition: true, cancellationToken: cancellationToken); diff --git a/src/EditorFeatures/Core/GoToDefinition/GoToDefinitionHelpers.cs b/src/EditorFeatures/Core/GoToDefinition/GoToDefinitionHelpers.cs index 76e8cb11df7..e942badf992 100644 --- a/src/EditorFeatures/Core/GoToDefinition/GoToDefinitionHelpers.cs +++ b/src/EditorFeatures/Core/GoToDefinition/GoToDefinitionHelpers.cs @@ -95,18 +95,17 @@ internal static class GoToDefinitionHelpers public static bool TryGoToDefinition( ISymbol symbol, Project project, - Lazy streamingPresenterOpt, + IStreamingFindUsagesPresenter streamingPresenter, CancellationToken cancellationToken, bool thirdPartyNavigationAllowed = true, bool throwOnHiddenDefinition = false) { var definitions = GetDefinitions(symbol, project, thirdPartyNavigationAllowed, cancellationToken); - var presenter = streamingPresenterOpt?.Value; var title = string.Format(EditorFeaturesResources._0_declarations, FindUsagesHelpers.GetDisplayName(symbol)); - return presenter.TryNavigateToOrPresentItemsAsync( + return streamingPresenter.TryNavigateToOrPresentItemsAsync( project.Solution.Workspace, title, definitions).WaitAndGetResult(cancellationToken); } diff --git a/src/EditorFeatures/Test2/GoToDefinition/GoToDefinitionCommandHandlerTests.vb b/src/EditorFeatures/Test2/GoToDefinition/GoToDefinitionCommandHandlerTests.vb index b0bab109333..3c55249b6b5 100644 --- a/src/EditorFeatures/Test2/GoToDefinition/GoToDefinitionCommandHandlerTests.vb +++ b/src/EditorFeatures/Test2/GoToDefinition/GoToDefinitionCommandHandlerTests.vb @@ -100,7 +100,7 @@ class C Dim cursorBuffer = cursorDocument.TextBuffer Dim document = workspace.CurrentSolution.GetDocument(cursorDocument.Id) - Dim goToDefService = New CSharpGoToDefinitionService(New Lazy(Of IStreamingFindUsagesPresenter)(Function() presenter)) + Dim goToDefService = New CSharpGoToDefinitionService(presenter) Dim waitContext = New TestUIThreadOperationContext(updatesBeforeCancel) Dim commandHandler = New GoToDefinitionCommandHandler() diff --git a/src/EditorFeatures/Test2/GoToDefinition/GoToDefinitionTests.vb b/src/EditorFeatures/Test2/GoToDefinition/GoToDefinitionTests.vb index 7d611591fac..3a69ecdb96a 100644 --- a/src/EditorFeatures/Test2/GoToDefinition/GoToDefinitionTests.vb +++ b/src/EditorFeatures/Test2/GoToDefinition/GoToDefinitionTests.vb @@ -14,7 +14,7 @@ Namespace Microsoft.CodeAnalysis.Editor.UnitTests.GoToDefinition Public Class GoToDefinitionTests Friend Sub Test(workspaceDefinition As XElement, expectedResult As Boolean, - executeOnDocument As Func(Of Document, Integer, Lazy(Of IStreamingFindUsagesPresenter), Boolean)) + executeOnDocument As Func(Of Document, Integer, IStreamingFindUsagesPresenter, Boolean)) Using workspace = TestWorkspace.Create( workspaceDefinition, exportProvider:=GoToTestHelpers.ExportProviderFactory.CreateExportProvider()) Dim solution = workspace.CurrentSolution @@ -38,7 +38,7 @@ Namespace Microsoft.CodeAnalysis.Editor.UnitTests.GoToDefinition Dim presenterCalled As Boolean = False Dim presenter = New MockStreamingFindUsagesPresenter(Sub() presenterCalled = True) - Dim actualResult = executeOnDocument(document, cursorPosition, New Lazy(Of IStreamingFindUsagesPresenter)(Function() presenter)) + Dim actualResult = executeOnDocument(document, cursorPosition, presenter) Assert.Equal(expectedResult, actualResult) @@ -103,7 +103,7 @@ Namespace Microsoft.CodeAnalysis.Editor.UnitTests.GoToDefinition Private Sub Test(workspaceDefinition As XElement, Optional expectedResult As Boolean = True) Test(workspaceDefinition, expectedResult, - Function(document As Document, cursorPosition As Integer, presenter As Lazy(Of IStreamingFindUsagesPresenter)) + Function(document As Document, cursorPosition As Integer, presenter As IStreamingFindUsagesPresenter) Dim goToDefService = If(document.Project.Language = LanguageNames.CSharp, DirectCast(New CSharpGoToDefinitionService(presenter), IGoToDefinitionService), New VisualBasicGoToDefinitionService(presenter)) diff --git a/src/EditorFeatures/VisualBasic/GoToDefinition/VisualBasicGoToDefinitionService.vb b/src/EditorFeatures/VisualBasic/GoToDefinition/VisualBasicGoToDefinitionService.vb index c9a3ad98adc..3e501edff56 100644 --- a/src/EditorFeatures/VisualBasic/GoToDefinition/VisualBasicGoToDefinitionService.vb +++ b/src/EditorFeatures/VisualBasic/GoToDefinition/VisualBasicGoToDefinitionService.vb @@ -11,8 +11,8 @@ Namespace Microsoft.CodeAnalysis.Editor.VisualBasic.GoToDefinition Inherits AbstractGoToDefinitionService - Public Sub New( streamingPresenterOpt As Lazy(Of IStreamingFindUsagesPresenter)) - MyBase.New(streamingPresenterOpt) + Public Sub New(streamingPresenter As IStreamingFindUsagesPresenter) + MyBase.New(streamingPresenter) End Sub End Class End Namespace diff --git a/src/VisualStudio/Core/Def/Implementation/Library/ObjectBrowser/AbstractObjectBrowserLibraryManager.cs b/src/VisualStudio/Core/Def/Implementation/Library/ObjectBrowser/AbstractObjectBrowserLibraryManager.cs index 3032bd8fe4f..cea2041f651 100644 --- a/src/VisualStudio/Core/Def/Implementation/Library/ObjectBrowser/AbstractObjectBrowserLibraryManager.cs +++ b/src/VisualStudio/Core/Def/Implementation/Library/ObjectBrowser/AbstractObjectBrowserLibraryManager.cs @@ -2,7 +2,6 @@ using System; using System.Diagnostics; -using System.Linq; using System.Text; using System.Threading; using Microsoft.CodeAnalysis; @@ -41,7 +40,7 @@ internal abstract partial class AbstractObjectBrowserLibraryManager : AbstractLi private AbstractListItemFactory _listItemFactory; private object _classMemberGate = new object(); - private readonly Lazy _streamingPresenterOpt; + private readonly IStreamingFindUsagesPresenter _streamingPresenter; private readonly IThreadingContext _threadingContext; protected AbstractObjectBrowserLibraryManager( @@ -61,7 +60,7 @@ internal abstract partial class AbstractObjectBrowserLibraryManager : AbstractLi Workspace.WorkspaceChanged += OnWorkspaceChanged; _libraryService = new Lazy(() => Workspace.Services.GetLanguageServices(_languageName).GetService()); - _streamingPresenterOpt = componentModel.DefaultExportProvider.GetExports().SingleOrDefault(); + _streamingPresenter = componentModel.DefaultExportProvider.GetExportedValue(); _threadingContext = componentModel.DefaultExportProvider.GetExportedValue(); } @@ -501,10 +500,8 @@ protected override bool TryExec(Guid commandGroup, uint commandId) switch (commandId) { case (uint)VSConstants.VSStd97CmdID.FindReferences: - var streamingPresenter = _streamingPresenterOpt?.Value; var symbolListItem = _activeListItem as SymbolListItem; - - if (streamingPresenter != null && symbolListItem?.ProjectId != null) + if (symbolListItem?.ProjectId != null) { var project = this.Workspace.CurrentSolution.GetProject(symbolListItem.ProjectId); if (project != null) @@ -514,7 +511,7 @@ protected override bool TryExec(Guid commandGroup, uint commandId) // and the references will be asynchronously added to the FindReferences // window as they are computed. The user also knows something is happening // as the window, with the progress-banner will pop up immediately. - var task = FindReferencesAsync(streamingPresenter, symbolListItem, project); + var task = FindReferencesAsync(_streamingPresenter, symbolListItem, project); return true; } } diff --git a/src/VisualStudio/Core/Impl/RoslynVisualStudioWorkspace.cs b/src/VisualStudio/Core/Impl/RoslynVisualStudioWorkspace.cs index d07334bf8f5..940028041f1 100644 --- a/src/VisualStudio/Core/Impl/RoslynVisualStudioWorkspace.cs +++ b/src/VisualStudio/Core/Impl/RoslynVisualStudioWorkspace.cs @@ -28,6 +28,10 @@ namespace Microsoft.VisualStudio.LanguageServices [Export(typeof(VisualStudioWorkspaceImpl))] internal class RoslynVisualStudioWorkspace : VisualStudioWorkspaceImpl { + /// + /// Must be lazily constructed since the implementation imports a + /// backreference to . + /// private readonly Lazy _streamingPresenter; [ImportingConstructor] @@ -116,7 +120,7 @@ private static bool TryResolveSymbol(ISymbol symbol, Project project, Cancellati return GoToDefinitionHelpers.TryGoToDefinition( searchSymbol, searchProject, - _streamingPresenter, cancellationToken); + _streamingPresenter.Value, cancellationToken); } public override bool TryFindAllReferences(ISymbol symbol, Project project, CancellationToken cancellationToken) diff --git a/src/VisualStudio/Core/Test/GoToDefinition/GoToDefinitionApiTests.vb b/src/VisualStudio/Core/Test/GoToDefinition/GoToDefinitionApiTests.vb index bbbabbbb24e..1a408b330fe 100644 --- a/src/VisualStudio/Core/Test/GoToDefinition/GoToDefinitionApiTests.vb +++ b/src/VisualStudio/Core/Test/GoToDefinition/GoToDefinitionApiTests.vb @@ -3,7 +3,6 @@ Imports System.Threading Imports Microsoft.CodeAnalysis Imports Microsoft.CodeAnalysis.Editor.GoToDefinition -Imports Microsoft.CodeAnalysis.Editor.Host Imports Microsoft.CodeAnalysis.Editor.UnitTests.Utilities.GoToHelpers Imports Microsoft.CodeAnalysis.Editor.UnitTests.Workspaces Imports Microsoft.CodeAnalysis.Test.Utilities @@ -42,7 +41,7 @@ Namespace Microsoft.VisualStudio.LanguageServices.UnitTests.GoToDefinition WpfTestRunner.RequireWpfFact($"{NameOf(GoToDefinitionHelpers)}.{NameOf(GoToDefinitionHelpers.TryGoToDefinition)} assumes it's on the UI thread with a {NameOf(TaskExtensions.WaitAndGetResult)} call") Dim success = GoToDefinitionHelpers.TryGoToDefinition( symbolInfo.Symbol, document.Project, - New Lazy(Of IStreamingFindUsagesPresenter)(Function() presenter), + presenter, thirdPartyNavigationAllowed:=True, throwOnHiddenDefinition:=False, cancellationToken:=CancellationToken.None) -- GitLab