提交 e5a0aa90 编写于 作者: S Sam Harwell

Make IStreamingFindUsagesPresenters a required export

上级 065f3579
// Copyright (c) Microsoft. All Rights Reserved. Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information. // 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 System.Composition;
using Microsoft.CodeAnalysis.Editor.GoToDefinition; using Microsoft.CodeAnalysis.Editor.GoToDefinition;
using Microsoft.CodeAnalysis.Editor.Host; using Microsoft.CodeAnalysis.Editor.Host;
...@@ -12,9 +11,8 @@ namespace Microsoft.CodeAnalysis.Editor.CSharp.GoToDefinition ...@@ -12,9 +11,8 @@ namespace Microsoft.CodeAnalysis.Editor.CSharp.GoToDefinition
internal class CSharpGoToDefinitionService : AbstractGoToDefinitionService internal class CSharpGoToDefinitionService : AbstractGoToDefinitionService
{ {
[ImportingConstructor] [ImportingConstructor]
public CSharpGoToDefinitionService( public CSharpGoToDefinitionService(IStreamingFindUsagesPresenter streamingPresenter)
[Import(AllowDefault = true)] Lazy<IStreamingFindUsagesPresenter> streamingPresenterOpt) : base(streamingPresenter)
: base(streamingPresenterOpt)
{ {
} }
} }
......
// Copyright (c) Microsoft. All Rights Reserved. Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information. // 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.Collections.Generic;
using System.Linq; using System.Linq;
using System.Threading; using System.Threading;
...@@ -16,12 +15,11 @@ namespace Microsoft.CodeAnalysis.Editor.GoToDefinition ...@@ -16,12 +15,11 @@ namespace Microsoft.CodeAnalysis.Editor.GoToDefinition
// GoToDefinition // GoToDefinition
internal abstract class AbstractGoToDefinitionService : IGoToDefinitionService internal abstract class AbstractGoToDefinitionService : IGoToDefinitionService
{ {
private readonly Lazy<IStreamingFindUsagesPresenter> _streamingPresenterOpt; private readonly IStreamingFindUsagesPresenter _streamingPresenter;
protected AbstractGoToDefinitionService( protected AbstractGoToDefinitionService(IStreamingFindUsagesPresenter streamingPresenter)
Lazy<IStreamingFindUsagesPresenter> streamingPresenterOpt)
{ {
_streamingPresenterOpt = streamingPresenterOpt; _streamingPresenter = streamingPresenter;
} }
public async Task<IEnumerable<INavigableItem>> FindDefinitionsAsync( public async Task<IEnumerable<INavigableItem>> FindDefinitionsAsync(
...@@ -54,7 +52,7 @@ public bool TryGoToDefinition(Document document, int position, CancellationToken ...@@ -54,7 +52,7 @@ public bool TryGoToDefinition(Document document, int position, CancellationToken
return GoToDefinitionHelpers.TryGoToDefinition(symbol, return GoToDefinitionHelpers.TryGoToDefinition(symbol,
document.Project, document.Project,
_streamingPresenterOpt, _streamingPresenter,
thirdPartyNavigationAllowed: isThirdPartyNavigationAllowed, thirdPartyNavigationAllowed: isThirdPartyNavigationAllowed,
throwOnHiddenDefinition: true, throwOnHiddenDefinition: true,
cancellationToken: cancellationToken); cancellationToken: cancellationToken);
......
...@@ -95,18 +95,17 @@ internal static class GoToDefinitionHelpers ...@@ -95,18 +95,17 @@ internal static class GoToDefinitionHelpers
public static bool TryGoToDefinition( public static bool TryGoToDefinition(
ISymbol symbol, ISymbol symbol,
Project project, Project project,
Lazy<IStreamingFindUsagesPresenter> streamingPresenterOpt, IStreamingFindUsagesPresenter streamingPresenter,
CancellationToken cancellationToken, CancellationToken cancellationToken,
bool thirdPartyNavigationAllowed = true, bool thirdPartyNavigationAllowed = true,
bool throwOnHiddenDefinition = false) bool throwOnHiddenDefinition = false)
{ {
var definitions = GetDefinitions(symbol, project, thirdPartyNavigationAllowed, cancellationToken); var definitions = GetDefinitions(symbol, project, thirdPartyNavigationAllowed, cancellationToken);
var presenter = streamingPresenterOpt?.Value;
var title = string.Format(EditorFeaturesResources._0_declarations, var title = string.Format(EditorFeaturesResources._0_declarations,
FindUsagesHelpers.GetDisplayName(symbol)); FindUsagesHelpers.GetDisplayName(symbol));
return presenter.TryNavigateToOrPresentItemsAsync( return streamingPresenter.TryNavigateToOrPresentItemsAsync(
project.Solution.Workspace, title, definitions).WaitAndGetResult(cancellationToken); project.Solution.Workspace, title, definitions).WaitAndGetResult(cancellationToken);
} }
......
...@@ -100,7 +100,7 @@ class C ...@@ -100,7 +100,7 @@ class C
Dim cursorBuffer = cursorDocument.TextBuffer Dim cursorBuffer = cursorDocument.TextBuffer
Dim document = workspace.CurrentSolution.GetDocument(cursorDocument.Id) 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 waitContext = New TestUIThreadOperationContext(updatesBeforeCancel)
Dim commandHandler = New GoToDefinitionCommandHandler() Dim commandHandler = New GoToDefinitionCommandHandler()
......
...@@ -14,7 +14,7 @@ Namespace Microsoft.CodeAnalysis.Editor.UnitTests.GoToDefinition ...@@ -14,7 +14,7 @@ Namespace Microsoft.CodeAnalysis.Editor.UnitTests.GoToDefinition
Public Class GoToDefinitionTests Public Class GoToDefinitionTests
Friend Sub Test(workspaceDefinition As XElement, Friend Sub Test(workspaceDefinition As XElement,
expectedResult As Boolean, 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( Using workspace = TestWorkspace.Create(
workspaceDefinition, exportProvider:=GoToTestHelpers.ExportProviderFactory.CreateExportProvider()) workspaceDefinition, exportProvider:=GoToTestHelpers.ExportProviderFactory.CreateExportProvider())
Dim solution = workspace.CurrentSolution Dim solution = workspace.CurrentSolution
...@@ -38,7 +38,7 @@ Namespace Microsoft.CodeAnalysis.Editor.UnitTests.GoToDefinition ...@@ -38,7 +38,7 @@ Namespace Microsoft.CodeAnalysis.Editor.UnitTests.GoToDefinition
Dim presenterCalled As Boolean = False Dim presenterCalled As Boolean = False
Dim presenter = New MockStreamingFindUsagesPresenter(Sub() presenterCalled = True) 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) Assert.Equal(expectedResult, actualResult)
...@@ -103,7 +103,7 @@ Namespace Microsoft.CodeAnalysis.Editor.UnitTests.GoToDefinition ...@@ -103,7 +103,7 @@ Namespace Microsoft.CodeAnalysis.Editor.UnitTests.GoToDefinition
Private Sub Test(workspaceDefinition As XElement, Optional expectedResult As Boolean = True) Private Sub Test(workspaceDefinition As XElement, Optional expectedResult As Boolean = True)
Test(workspaceDefinition, expectedResult, 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, Dim goToDefService = If(document.Project.Language = LanguageNames.CSharp,
DirectCast(New CSharpGoToDefinitionService(presenter), IGoToDefinitionService), DirectCast(New CSharpGoToDefinitionService(presenter), IGoToDefinitionService),
New VisualBasicGoToDefinitionService(presenter)) New VisualBasicGoToDefinitionService(presenter))
......
...@@ -11,8 +11,8 @@ Namespace Microsoft.CodeAnalysis.Editor.VisualBasic.GoToDefinition ...@@ -11,8 +11,8 @@ Namespace Microsoft.CodeAnalysis.Editor.VisualBasic.GoToDefinition
Inherits AbstractGoToDefinitionService Inherits AbstractGoToDefinitionService
<ImportingConstructor> <ImportingConstructor>
Public Sub New(<Import(AllowDefault:=True)> streamingPresenterOpt As Lazy(Of IStreamingFindUsagesPresenter)) Public Sub New(streamingPresenter As IStreamingFindUsagesPresenter)
MyBase.New(streamingPresenterOpt) MyBase.New(streamingPresenter)
End Sub End Sub
End Class End Class
End Namespace End Namespace
...@@ -2,7 +2,6 @@ ...@@ -2,7 +2,6 @@
using System; using System;
using System.Diagnostics; using System.Diagnostics;
using System.Linq;
using System.Text; using System.Text;
using System.Threading; using System.Threading;
using Microsoft.CodeAnalysis; using Microsoft.CodeAnalysis;
...@@ -41,7 +40,7 @@ internal abstract partial class AbstractObjectBrowserLibraryManager : AbstractLi ...@@ -41,7 +40,7 @@ internal abstract partial class AbstractObjectBrowserLibraryManager : AbstractLi
private AbstractListItemFactory _listItemFactory; private AbstractListItemFactory _listItemFactory;
private object _classMemberGate = new object(); private object _classMemberGate = new object();
private readonly Lazy<IStreamingFindUsagesPresenter> _streamingPresenterOpt; private readonly IStreamingFindUsagesPresenter _streamingPresenter;
private readonly IThreadingContext _threadingContext; private readonly IThreadingContext _threadingContext;
protected AbstractObjectBrowserLibraryManager( protected AbstractObjectBrowserLibraryManager(
...@@ -61,7 +60,7 @@ internal abstract partial class AbstractObjectBrowserLibraryManager : AbstractLi ...@@ -61,7 +60,7 @@ internal abstract partial class AbstractObjectBrowserLibraryManager : AbstractLi
Workspace.WorkspaceChanged += OnWorkspaceChanged; Workspace.WorkspaceChanged += OnWorkspaceChanged;
_libraryService = new Lazy<ILibraryService>(() => Workspace.Services.GetLanguageServices(_languageName).GetService<ILibraryService>()); _libraryService = new Lazy<ILibraryService>(() => Workspace.Services.GetLanguageServices(_languageName).GetService<ILibraryService>());
_streamingPresenterOpt = componentModel.DefaultExportProvider.GetExports<IStreamingFindUsagesPresenter>().SingleOrDefault(); _streamingPresenter = componentModel.DefaultExportProvider.GetExportedValue<IStreamingFindUsagesPresenter>();
_threadingContext = componentModel.DefaultExportProvider.GetExportedValue<IThreadingContext>(); _threadingContext = componentModel.DefaultExportProvider.GetExportedValue<IThreadingContext>();
} }
...@@ -501,10 +500,8 @@ protected override bool TryExec(Guid commandGroup, uint commandId) ...@@ -501,10 +500,8 @@ protected override bool TryExec(Guid commandGroup, uint commandId)
switch (commandId) switch (commandId)
{ {
case (uint)VSConstants.VSStd97CmdID.FindReferences: case (uint)VSConstants.VSStd97CmdID.FindReferences:
var streamingPresenter = _streamingPresenterOpt?.Value;
var symbolListItem = _activeListItem as SymbolListItem; var symbolListItem = _activeListItem as SymbolListItem;
if (symbolListItem?.ProjectId != null)
if (streamingPresenter != null && symbolListItem?.ProjectId != null)
{ {
var project = this.Workspace.CurrentSolution.GetProject(symbolListItem.ProjectId); var project = this.Workspace.CurrentSolution.GetProject(symbolListItem.ProjectId);
if (project != null) if (project != null)
...@@ -514,7 +511,7 @@ protected override bool TryExec(Guid commandGroup, uint commandId) ...@@ -514,7 +511,7 @@ protected override bool TryExec(Guid commandGroup, uint commandId)
// and the references will be asynchronously added to the FindReferences // and the references will be asynchronously added to the FindReferences
// window as they are computed. The user also knows something is happening // window as they are computed. The user also knows something is happening
// as the window, with the progress-banner will pop up immediately. // 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; return true;
} }
} }
......
...@@ -28,6 +28,10 @@ namespace Microsoft.VisualStudio.LanguageServices ...@@ -28,6 +28,10 @@ namespace Microsoft.VisualStudio.LanguageServices
[Export(typeof(VisualStudioWorkspaceImpl))] [Export(typeof(VisualStudioWorkspaceImpl))]
internal class RoslynVisualStudioWorkspace : VisualStudioWorkspaceImpl internal class RoslynVisualStudioWorkspace : VisualStudioWorkspaceImpl
{ {
/// <remarks>
/// Must be lazily constructed since the <see cref="IStreamingFindUsagesPresenter"/> implementation imports a
/// backreference to <see cref="VisualStudioWorkspace"/>.
/// </remarks>
private readonly Lazy<IStreamingFindUsagesPresenter> _streamingPresenter; private readonly Lazy<IStreamingFindUsagesPresenter> _streamingPresenter;
[ImportingConstructor] [ImportingConstructor]
...@@ -116,7 +120,7 @@ private static bool TryResolveSymbol(ISymbol symbol, Project project, Cancellati ...@@ -116,7 +120,7 @@ private static bool TryResolveSymbol(ISymbol symbol, Project project, Cancellati
return GoToDefinitionHelpers.TryGoToDefinition( return GoToDefinitionHelpers.TryGoToDefinition(
searchSymbol, searchProject, searchSymbol, searchProject,
_streamingPresenter, cancellationToken); _streamingPresenter.Value, cancellationToken);
} }
public override bool TryFindAllReferences(ISymbol symbol, Project project, CancellationToken cancellationToken) public override bool TryFindAllReferences(ISymbol symbol, Project project, CancellationToken cancellationToken)
......
...@@ -3,7 +3,6 @@ ...@@ -3,7 +3,6 @@
Imports System.Threading Imports System.Threading
Imports Microsoft.CodeAnalysis Imports Microsoft.CodeAnalysis
Imports Microsoft.CodeAnalysis.Editor.GoToDefinition Imports Microsoft.CodeAnalysis.Editor.GoToDefinition
Imports Microsoft.CodeAnalysis.Editor.Host
Imports Microsoft.CodeAnalysis.Editor.UnitTests.Utilities.GoToHelpers Imports Microsoft.CodeAnalysis.Editor.UnitTests.Utilities.GoToHelpers
Imports Microsoft.CodeAnalysis.Editor.UnitTests.Workspaces Imports Microsoft.CodeAnalysis.Editor.UnitTests.Workspaces
Imports Microsoft.CodeAnalysis.Test.Utilities Imports Microsoft.CodeAnalysis.Test.Utilities
...@@ -42,7 +41,7 @@ Namespace Microsoft.VisualStudio.LanguageServices.UnitTests.GoToDefinition ...@@ -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") WpfTestRunner.RequireWpfFact($"{NameOf(GoToDefinitionHelpers)}.{NameOf(GoToDefinitionHelpers.TryGoToDefinition)} assumes it's on the UI thread with a {NameOf(TaskExtensions.WaitAndGetResult)} call")
Dim success = GoToDefinitionHelpers.TryGoToDefinition( Dim success = GoToDefinitionHelpers.TryGoToDefinition(
symbolInfo.Symbol, document.Project, symbolInfo.Symbol, document.Project,
New Lazy(Of IStreamingFindUsagesPresenter)(Function() presenter), presenter,
thirdPartyNavigationAllowed:=True, throwOnHiddenDefinition:=False, thirdPartyNavigationAllowed:=True, throwOnHiddenDefinition:=False,
cancellationToken:=CancellationToken.None) cancellationToken:=CancellationToken.None)
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册