提交 a9956355 编写于 作者: C CyrusNajmabadi 提交者: GitHub

Merge pull request #15361 from CyrusNajmabadi/simpleFindUsagesContext

	Extract context object out so it can be used by other features.
......@@ -113,6 +113,7 @@
<Compile Include="FindUsages\FindUsagesContext.cs" />
<Compile Include="FindUsages\IFindUsagesContext.cs" />
<Compile Include="FindUsages\IFindUsagesService.cs" />
<Compile Include="FindUsages\SimpleFindUsagesContext.cs" />
<Compile Include="Implementation\NavigateTo\AbstractNavigateToItemDisplay.cs" />
<Compile Include="CommandArgs.cs" />
<Compile Include="CommandHandlers\AbstractCompletionCommandHandler.cs" />
......
// 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.Collections.Immutable;
using System.Threading;
using System.Threading.Tasks;
using Microsoft.CodeAnalysis.FindUsages;
using Roslyn.Utilities;
namespace Microsoft.CodeAnalysis.Editor.FindUsages
{
/// <summary>
/// Simple implementation of a <see cref="FindUsagesContext"/> that just aggregates the results
/// for consumers that just want the data once it is finally computed.
/// </summary>
internal class SimpleFindUsagesContext : FindUsagesContext
{
private readonly object _gate = new object();
private readonly ImmutableArray<DefinitionItem>.Builder _definitionItems =
ImmutableArray.CreateBuilder<DefinitionItem>();
private readonly ImmutableArray<SourceReferenceItem>.Builder _referenceItems =
ImmutableArray.CreateBuilder<SourceReferenceItem>();
public override CancellationToken CancellationToken { get; }
public SimpleFindUsagesContext(CancellationToken cancellationToken)
{
CancellationToken = cancellationToken;
}
public string Message { get; private set; }
public override void ReportMessage(string message)
=> Message = message;
public ImmutableArray<DefinitionItem> GetDefinitions()
{
lock (_gate)
{
return _definitionItems.ToImmutable();
}
}
public ImmutableArray<SourceReferenceItem> GetReferences()
{
lock (_gate)
{
return _referenceItems.ToImmutable();
}
}
public override Task OnDefinitionFoundAsync(DefinitionItem definition)
{
lock (_gate)
{
_definitionItems.Add(definition);
}
return SpecializedTasks.EmptyTask;
}
public override Task OnReferenceFoundAsync(SourceReferenceItem reference)
{
lock (_gate)
{
_referenceItems.Add(reference);
}
return SpecializedTasks.EmptyTask;
}
}
}
\ No newline at end of file
......@@ -2,30 +2,23 @@
using System;
using System.Collections.Generic;
using System.Collections.Immutable;
using System.ComponentModel.Composition;
using System.Linq;
using System.Threading;
using System.Threading.Tasks;
using Microsoft.CodeAnalysis.Editor.Commands;
using Microsoft.CodeAnalysis.Editor.FindUsages;
using Microsoft.CodeAnalysis.Editor.Host;
using Microsoft.CodeAnalysis.Editor.Shared.Extensions;
using Microsoft.CodeAnalysis.Editor.Shared.Options;
using Microsoft.CodeAnalysis.ErrorReporting;
using Microsoft.CodeAnalysis.FindUsages;
using Microsoft.CodeAnalysis.Notification;
using Microsoft.CodeAnalysis.Shared.Extensions;
using Microsoft.CodeAnalysis.Shared.TestHooks;
using Microsoft.CodeAnalysis.Text;
using Microsoft.VisualStudio.Text;
using Roslyn.Utilities;
namespace Microsoft.CodeAnalysis.Editor.GoToImplementation
{
[ExportCommandHandler(PredefinedCommandHandlerNames.GoToImplementation,
ContentTypeNames.RoslynContentType)]
internal sealed class GoToImplementationCommandHandler : ICommandHandler<GoToImplementationCommandArgs>
internal partial class GoToImplementationCommandHandler : ICommandHandler<GoToImplementationCommandArgs>
{
private readonly IWaitIndicator _waitIndicator;
private readonly IEnumerable<Lazy<IStreamingFindUsagesPresenter>> _streamingPresenters;
......@@ -121,7 +114,7 @@ private void ExecuteCommand(Document document, int caretPosition)
// the individual IFindUsagesService. 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 goToImplContext = new GoToImplementationContext(cancellationToken);
var goToImplContext = new SimpleFindUsagesContext(cancellationToken);
findUsagesService.FindImplementationsAsync(document, caretPosition, goToImplContext).Wait(cancellationToken);
// If finding implementations reported a message, then just stop and show that
......@@ -132,10 +125,10 @@ private void ExecuteCommand(Document document, int caretPosition)
return;
}
var allItems = goToImplContext.GetDefinitionItems();
var definitionItems = goToImplContext.GetDefinitions();
streamingPresenter.NavigateToOrPresentItemsAsync(
EditorFeaturesResources.Go_To_Implementation, allItems).Wait(cancellationToken);
EditorFeaturesResources.Go_To_Implementation, definitionItems).Wait(cancellationToken);
}
private IStreamingFindUsagesPresenter GetStreamingPresenter()
......@@ -149,42 +142,5 @@ private IStreamingFindUsagesPresenter GetStreamingPresenter()
return null;
}
}
private class GoToImplementationContext : FindUsagesContext
{
private readonly object _gate = new object();
private readonly ImmutableArray<DefinitionItem>.Builder _definitionItems =
ImmutableArray.CreateBuilder<DefinitionItem>();
public override CancellationToken CancellationToken { get; }
public GoToImplementationContext(CancellationToken cancellationToken)
{
CancellationToken = cancellationToken;
}
public string Message { get; private set; }
public override void ReportMessage(string message)
=> Message = message;
public ImmutableArray<DefinitionItem> GetDefinitionItems()
{
lock (_gate)
{
return _definitionItems.ToImmutableArray();
}
}
public override Task OnDefinitionFoundAsync(DefinitionItem definition)
{
lock (_gate)
{
_definitionItems.Add(definition);
}
return SpecializedTasks.EmptyTask;
}
}
}
}
\ No newline at end of file
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册