提交 12769173 编写于 作者: C CyrusNajmabadi

Extract out helper code to a common location.

上级 6c305478
......@@ -109,6 +109,7 @@
</ItemGroup>
<ItemGroup>
<Compile Include="FindUsages\FindUsagesContext.cs" />
<Compile Include="FindUsages\FindUsagesHelpers.cs" />
<Compile Include="FindUsages\IFindUsagesContext.cs" />
<Compile Include="Implementation\NavigateTo\AbstractNavigateToItemDisplay.cs" />
<Compile Include="CommandArgs.cs" />
......
......@@ -3,13 +3,12 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading;
using System.Threading.Tasks;
using Microsoft.CodeAnalysis.Editor.FindUsages;
using Microsoft.CodeAnalysis.Editor.Host;
using Microsoft.CodeAnalysis.Editor.Shared.Utilities;
using Microsoft.CodeAnalysis.Editor.SymbolMapping;
using Microsoft.CodeAnalysis.FindUsages;
using Microsoft.CodeAnalysis.FindSymbols;
using Microsoft.CodeAnalysis.FindUsages;
using Microsoft.CodeAnalysis.Navigation;
using Microsoft.CodeAnalysis.Shared.Extensions;
using Roslyn.Utilities;
......@@ -30,54 +29,20 @@ internal abstract partial class AbstractFindReferencesService :
_navigableItemPresenters = navigableItemPresenters;
}
/// <summary>
/// Common helper for both the synchronous and streaming versions of FAR.
/// It returns the symbol we want to search for and the solution we should
/// be searching.
///
/// Note that the <see cref="Solution"/> returned may absolutely *not* be
/// the same as <code>document.Project.Solution</code>. This is because
/// there may be symbol mapping involved (for example in Metadata-As-Source
/// scenarios).
/// </summary>
private async Task<Tuple<ISymbol, Project>> GetRelevantSymbolAndProjectAtPositionAsync(
Document document, int position, CancellationToken cancellationToken)
{
cancellationToken.ThrowIfCancellationRequested();
var symbol = await SymbolFinder.FindSymbolAtPositionAsync(document, position, cancellationToken: cancellationToken).ConfigureAwait(false);
if (symbol == null)
{
return null;
}
// If this document is not in the primary workspace, we may want to search for results
// in a solution different from the one we started in. Use the starting workspace's
// ISymbolMappingService to get a context for searching in the proper solution.
var mappingService = document.Project.Solution.Workspace.Services.GetService<ISymbolMappingService>();
var mapping = await mappingService.MapSymbolAsync(document, symbol, cancellationToken).ConfigureAwait(false);
if (mapping == null)
{
return null;
}
return Tuple.Create(mapping.Symbol, mapping.Project);
}
private async Task<Tuple<IEnumerable<ReferencedSymbol>, Solution>> FindReferencedSymbolsAsync(
Document document, int position, IWaitContext waitContext)
{
var cancellationToken = waitContext.CancellationToken;
var symbolAndProject = await GetRelevantSymbolAndProjectAtPositionAsync(document, position, cancellationToken).ConfigureAwait(false);
var symbolAndProject = await FindUsagesHelpers.GetRelevantSymbolAndProjectAtPositionAsync(
document, position, cancellationToken).ConfigureAwait(false);
if (symbolAndProject == null)
{
return null;
}
var symbol = symbolAndProject.Item1;
var project = symbolAndProject.Item2;
var symbol = symbolAndProject?.symbol;
var project = symbolAndProject?.project;
var displayName = GetDisplayName(symbol);
......@@ -164,15 +129,15 @@ private bool TryDisplayReferences(Tuple<IEnumerable<ReferencedSymbol>, Solution>
cancellationToken.ThrowIfCancellationRequested();
// Find the symbol we want to search and the solution we want to search in.
var symbolAndProject = await GetRelevantSymbolAndProjectAtPositionAsync(
var symbolAndProject = await FindUsagesHelpers.GetRelevantSymbolAndProjectAtPositionAsync(
document, position, cancellationToken).ConfigureAwait(false);
if (symbolAndProject == null)
{
return null;
}
var symbol = symbolAndProject.Item1;
var project = symbolAndProject.Item2;
var symbol = symbolAndProject?.symbol;
var project = symbolAndProject?.project;
var displayName = GetDisplayName(symbol);
context.SetSearchLabel(displayName);
......
// 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.Threading;
using System.Threading.Tasks;
using Microsoft.CodeAnalysis.Editor.SymbolMapping;
using Microsoft.CodeAnalysis.FindSymbols;
namespace Microsoft.CodeAnalysis.Editor.FindUsages
{
internal static class FindUsagesHelpers
{
/// <summary>
/// Common helper for both the synchronous and streaming versions of FAR.
/// It returns the symbol we want to search for and the solution we should
/// be searching.
///
/// Note that the <see cref="Solution"/> returned may absolutely *not* be
/// the same as <code>document.Project.Solution</code>. This is because
/// there may be symbol mapping involved (for example in Metadata-As-Source
/// scenarios).
/// </summary>
public static async Task<(ISymbol symbol, Project project)?> GetRelevantSymbolAndProjectAtPositionAsync(
Document document, int position, CancellationToken cancellationToken)
{
cancellationToken.ThrowIfCancellationRequested();
var symbol = await SymbolFinder.FindSymbolAtPositionAsync(document, position, cancellationToken: cancellationToken).ConfigureAwait(false);
if (symbol == null)
{
return null;
}
// If this document is not in the primary workspace, we may want to search for results
// in a solution different from the one we started in. Use the starting workspace's
// ISymbolMappingService to get a context for searching in the proper solution.
var mappingService = document.Project.Solution.Workspace.Services.GetService<ISymbolMappingService>();
var mapping = await mappingService.MapSymbolAsync(document, symbol, cancellationToken).ConfigureAwait(false);
if (mapping == null)
{
return null;
}
return (mapping.Symbol, mapping.Project);
}
}
}
\ No newline at end of file
......@@ -9,15 +9,20 @@ internal partial class FeatureAttribute
public const string BraceHighlighting = nameof(BraceHighlighting);
public const string CallHierarchy = nameof(CallHierarchy);
public const string Classification = nameof(Classification);
public const string CodeModel = nameof(CodeModel);
public const string CompletionSet = nameof(CompletionSet);
public const string DesignerAttribute = nameof(DesignerAttribute);
public const string ErrorSquiggles = nameof(ErrorSquiggles);
public const string DiagnosticService = nameof(DiagnosticService);
public const string ErrorList = nameof(ErrorList);
public const string FindReferences = nameof(FindReferences);
public const string TodoCommentList = nameof(TodoCommentList);
public const string ErrorSquiggles = nameof(ErrorSquiggles);
public const string EventHookup = nameof(EventHookup);
public const string FindReferences = nameof(FindReferences);
public const string GlobalOperation = nameof(GlobalOperation);
public const string GoToImplementation = nameof(GoToImplementation);
public const string GraphProvider = nameof(GraphProvider);
public const string InfoBar = nameof(InfoBar);
public const string KeywordHighlighting = nameof(KeywordHighlighting);
public const string LightBulb = nameof(LightBulb);
public const string LineSeparators = nameof(LineSeparators);
public const string NavigateTo = nameof(NavigateTo);
public const string NavigationBar = nameof(NavigationBar);
......@@ -26,15 +31,11 @@ internal partial class FeatureAttribute
public const string ReferenceHighlighting = nameof(ReferenceHighlighting);
public const string Rename = nameof(Rename);
public const string RenameTracking = nameof(RenameTracking);
public const string RuleSetEditor = nameof(RuleSetEditor);
public const string SignatureHelp = nameof(SignatureHelp);
public const string Snippets = nameof(Snippets);
public const string SolutionCrawler = nameof(SolutionCrawler);
public const string TodoCommentList = nameof(TodoCommentList);
public const string Workspace = nameof(Workspace);
public const string LightBulb = nameof(LightBulb);
public const string CodeModel = nameof(CodeModel);
public const string GlobalOperation = nameof(GlobalOperation);
public const string DiagnosticService = nameof(DiagnosticService);
public const string RuleSetEditor = nameof(RuleSetEditor);
public const string InfoBar = nameof(InfoBar);
}
}
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册