提交 bbe1d6b2 编写于 作者: C Cyrus Najmabadi

Siplify code

上级 05b2ed1b
......@@ -102,8 +102,7 @@ private async Task<DefinitionItem> GetDefinitionItemAsync(ISymbol definition)
if (!_definitionToItem.TryGetValue(definition, out var definitionItem))
{
definitionItem = await definition.ToClassifiedDefinitionItemAsync(
_solution.GetOriginatingProject(definition), includeHiddenLocations: false,
_options, _context.CancellationToken).ConfigureAwait(false);
_solution, includeHiddenLocations: false, _options, _context.CancellationToken).ConfigureAwait(false);
_definitionToItem[definition] = definitionItem;
}
......
......@@ -52,8 +52,7 @@ protected AbstractFindUsagesService(IThreadingContext threadingContext)
foreach (var implementation in implementations)
{
var definitionItem = await implementation.ToClassifiedDefinitionItemAsync(
solution.GetOriginatingProject(implementation), includeHiddenLocations: false,
FindReferencesSearchOptions.Default, cancellationToken).ConfigureAwait(false);
solution, includeHiddenLocations: false, FindReferencesSearchOptions.Default, cancellationToken).ConfigureAwait(false);
await context.OnDefinitionFoundAsync(definitionItem).ConfigureAwait(false);
}
......@@ -122,16 +121,16 @@ protected AbstractFindUsagesService(IThreadingContext threadingContext)
cancellationToken.ThrowIfCancellationRequested();
// Find the symbol we want to search and the solution we want to search in.
var symbolAndProjectOpt = await FindUsagesHelpers.GetRelevantSymbolAndProjectAtPositionAsync(
var symbolAndSolutionOpt = await FindUsagesHelpers.GetRelevantSymbolAndSolutionAtPositionAsync(
document, position, cancellationToken).ConfigureAwait(false);
if (symbolAndProjectOpt == null)
if (symbolAndSolutionOpt == null)
return;
var (symbol, project) = symbolAndProjectOpt.Value;
var (symbol, solution) = symbolAndSolutionOpt.Value;
await FindSymbolReferencesAsync(
_threadingContext, context,
symbol, project,
symbol, solution,
cancellationToken).ConfigureAwait(false);
}
......@@ -141,9 +140,9 @@ protected AbstractFindUsagesService(IThreadingContext threadingContext)
/// </summary>
public static async Task FindSymbolReferencesAsync(
IThreadingContext threadingContext, IFindUsagesContext context,
ISymbol symbol, Project project, CancellationToken cancellationToken)
ISymbol symbol, Solution solution, CancellationToken cancellationToken)
{
var monikerUsagesService = project.Solution.Workspace.Services.GetRequiredService<IFindSymbolMonikerUsagesService>();
var monikerUsagesService = solution.Workspace.Services.GetRequiredService<IFindSymbolMonikerUsagesService>();
await context.SetSearchTitleAsync(string.Format(EditorFeaturesResources._0_references,
FindUsagesHelpers.GetDisplayName(symbol))).ConfigureAwait(false);
......@@ -154,20 +153,13 @@ protected AbstractFindUsagesService(IThreadingContext threadingContext)
// engine will push results into the 'progress' instance passed into it.
// We'll take those results, massage them, and forward them along to the
// FindReferencesContext instance we were given.
var progress = new FindReferencesProgressAdapter(threadingContext, solution, context, options);
var normalFindReferencesTask = SymbolFinder.FindReferencesAsync(
symbol,
project.Solution,
new FindReferencesProgressAdapter(threadingContext, project.Solution, context, options),
documents: null,
options,
cancellationToken);
symbol, solution, progress, documents: null, options, cancellationToken);
// Kick off work to search the online code index system in parallel
var codeIndexReferencesTask = FindSymbolMonikerReferencesAsync(
monikerUsagesService,
symbol,
context,
cancellationToken);
monikerUsagesService, symbol, context, cancellationToken);
await Task.WhenAll(normalFindReferencesTask, codeIndexReferencesTask).ConfigureAwait(false);
}
......
......@@ -31,7 +31,7 @@ public static string GetDisplayName(ISymbol symbol)
/// there may be symbol mapping involved (for example in Metadata-As-Source
/// scenarios).
/// </summary>
public static async Task<(ISymbol symbol, Project project)?> GetRelevantSymbolAndProjectAtPositionAsync(
public static async Task<(ISymbol symbol, Solution solution)?> GetRelevantSymbolAndSolutionAtPositionAsync(
Document document, int position, CancellationToken cancellationToken)
{
cancellationToken.ThrowIfCancellationRequested();
......@@ -49,19 +49,19 @@ public static string GetDisplayName(ISymbol symbol)
if (mapping == null)
return null;
return (mapping.Symbol, mapping.Project);
return (mapping.Symbol, mapping.Project.Solution);
}
public static async Task<(Solution solution, ISymbol symbol, ImmutableArray<ISymbol> implementations, string message)?> FindSourceImplementationsAsync(Document document, int position, CancellationToken cancellationToken)
{
var symbolAndProjectOpt = await GetRelevantSymbolAndProjectAtPositionAsync(
var symbolAndSolutionOpt = await GetRelevantSymbolAndSolutionAtPositionAsync(
document, position, cancellationToken).ConfigureAwait(false);
if (symbolAndProjectOpt == null)
if (symbolAndSolutionOpt == null)
return null;
var (symbol, project) = symbolAndProjectOpt.Value;
var (symbol, solution) = symbolAndSolutionOpt.Value;
return await FindSourceImplementationsAsync(
project.Solution, symbol, cancellationToken).ConfigureAwait(false);
solution, symbol, cancellationToken).ConfigureAwait(false);
}
private static async Task<(Solution solution, ISymbol symbol, ImmutableArray<ISymbol> implementations, string message)?> FindSourceImplementationsAsync(
......
......@@ -53,7 +53,7 @@ internal static class DefinitionItemExtensions
{
public static DefinitionItem ToNonClassifiedDefinitionItem(
this ISymbol definition,
Project project,
Solution solution,
bool includeHiddenLocations)
{
// Because we're passing in 'false' for 'includeClassifiedSpans', this won't ever have
......@@ -61,25 +61,25 @@ internal static class DefinitionItemExtensions
// to compute the classified spans for the locations of the definition. So it's totally
// fine to pass in CancellationToken.None and block on the result.
return ToDefinitionItemAsync(
definition, project, includeHiddenLocations, includeClassifiedSpans: false,
definition, solution, includeHiddenLocations, includeClassifiedSpans: false,
options: FindReferencesSearchOptions.Default, cancellationToken: CancellationToken.None).WaitAndGetResult_CanCallOnBackground(CancellationToken.None);
}
public static Task<DefinitionItem> ToClassifiedDefinitionItemAsync(
this ISymbol definition,
Project project,
Solution solution,
bool includeHiddenLocations,
FindReferencesSearchOptions options,
CancellationToken cancellationToken)
{
return ToDefinitionItemAsync(definition, project,
return ToDefinitionItemAsync(definition, solution,
includeHiddenLocations, includeClassifiedSpans: true,
options, cancellationToken);
}
private static async Task<DefinitionItem> ToDefinitionItemAsync(
this ISymbol definition,
Project project,
Solution solution,
bool includeHiddenLocations,
bool includeClassifiedSpans,
FindReferencesSearchOptions options,
......@@ -121,7 +121,7 @@ internal static class DefinitionItemExtensions
if (location.IsInMetadata)
{
return DefinitionItem.CreateMetadataDefinition(
tags, displayParts, nameDisplayParts, project,
tags, displayParts, nameDisplayParts, solution,
definition, properties, displayIfNoReferences);
}
else if (location.IsInSource)
......@@ -132,7 +132,7 @@ internal static class DefinitionItemExtensions
continue;
}
var document = project.Solution.GetDocument(location.SourceTree);
var document = solution.GetDocument(location.SourceTree);
if (document != null)
{
var documentLocation = !includeClassifiedSpans
......
......@@ -16,21 +16,20 @@ internal abstract partial class AbstractGoToBaseService : IGoToBaseService
public async Task FindBasesAsync(Document document, int position, IFindUsagesContext context)
{
var cancellationToken = context.CancellationToken;
var symbolAndProjectOpt = await FindUsagesHelpers.GetRelevantSymbolAndProjectAtPositionAsync(
var symbolAndSolutionOpt = await FindUsagesHelpers.GetRelevantSymbolAndSolutionAtPositionAsync(
document, position, cancellationToken).ConfigureAwait(false);
if (symbolAndProjectOpt == null)
if (symbolAndSolutionOpt == null)
{
await context.ReportMessageAsync(
EditorFeaturesResources.Cannot_navigate_to_the_symbol_under_the_caret).ConfigureAwait(false);
return;
}
var (symbol, project) = symbolAndProjectOpt.Value;
var solution = project.Solution;
var (symbol, solution) = symbolAndSolutionOpt.Value;
var bases = FindBaseHelpers.FindBases(
symbol, project, cancellationToken);
symbol, solution, cancellationToken);
await context.SetSearchTitleAsync(
string.Format(EditorFeaturesResources._0_bases,
......@@ -48,8 +47,7 @@ public async Task FindBasesAsync(Document document, int position, IFindUsagesCon
if (sourceDefinition != null)
{
var definitionItem = await sourceDefinition.ToClassifiedDefinitionItemAsync(
solution.GetOriginatingProject(sourceDefinition), includeHiddenLocations: false,
FindReferencesSearchOptions.Default, cancellationToken: cancellationToken).ConfigureAwait(false);
solution, includeHiddenLocations: false, FindReferencesSearchOptions.Default, cancellationToken: cancellationToken).ConfigureAwait(false);
await context.OnDefinitionFoundAsync(definitionItem).ConfigureAwait(false);
found = true;
......@@ -57,7 +55,7 @@ public async Task FindBasesAsync(Document document, int position, IFindUsagesCon
else if (baseSymbol.Locations.Any(l => l.IsInMetadata))
{
var definitionItem = baseSymbol.ToNonClassifiedDefinitionItem(
project, includeHiddenLocations: true);
solution, includeHiddenLocations: true);
await context.OnDefinitionFoundAsync(definitionItem).ConfigureAwait(false);
found = true;
}
......
......@@ -11,7 +11,7 @@ namespace Microsoft.CodeAnalysis.Editor.GoToBase
internal static class FindBaseHelpers
{
public static ImmutableArray<ISymbol> FindBases(
ISymbol symbol, Project project, CancellationToken cancellationToken)
ISymbol symbol, Solution solution, CancellationToken cancellationToken)
{
if (symbol is INamedTypeSymbol namedTypeSymbol &&
(namedTypeSymbol.TypeKind == TypeKind.Class ||
......@@ -24,8 +24,7 @@ internal static class FindBaseHelpers
symbol.Kind == SymbolKind.Method ||
symbol.Kind == SymbolKind.Event)
{
return BaseTypeFinder.FindOverriddenAndImplementedMembers(
symbol, project, cancellationToken);
return BaseTypeFinder.FindOverriddenAndImplementedMembers(symbol, solution, cancellationToken);
}
else
{
......
......@@ -63,7 +63,7 @@ public bool TryGoToDefinition(Document document, int position, CancellationToken
var isThirdPartyNavigationAllowed = IsThirdPartyNavigationAllowed(symbol, position, document, cancellationToken);
return GoToDefinitionHelpers.TryGoToDefinition(symbol,
document.Project,
document.Project.Solution,
_streamingPresenter.Value,
thirdPartyNavigationAllowed: isThirdPartyNavigationAllowed,
cancellationToken: cancellationToken);
......@@ -100,7 +100,7 @@ public bool TryGoToDefinition(Document document, int position, CancellationToken
var definitions = interfaceImpls.SelectMany(
i => GoToDefinitionHelpers.GetDefinitions(
i, project, thirdPartyNavigationAllowed: false, cancellationToken)).ToImmutableArray();
i, solution, thirdPartyNavigationAllowed: false, cancellationToken)).ToImmutableArray();
var title = string.Format(EditorFeaturesResources._0_implemented_members,
FindUsagesHelpers.GetDisplayName(symbol));
......
......@@ -40,8 +40,9 @@ public async Task GetSymbolsAsync(GoToSymbolContext context)
await ThreadingContext.JoinableTaskFactory.SwitchToMainThreadAsync(alwaysYield: true, cancellationToken);
cancellationToken.ThrowIfCancellationRequested();
var definitions = GoToDefinitionHelpers.GetDefinitions(symbol, document.Project, thirdPartyNavigationAllowed: true, cancellationToken)
.WhereAsArray(d => d.CanNavigateTo(document.Project.Solution.Workspace));
var solution = document.Project.Solution;
var definitions = GoToDefinitionHelpers.GetDefinitions(symbol, solution, thirdPartyNavigationAllowed: true, cancellationToken)
.WhereAsArray(d => d.CanNavigateTo(solution.Workspace));
await TaskScheduler.Default;
......
......@@ -20,7 +20,7 @@ internal static class GoToDefinitionHelpers
{
public static ImmutableArray<DefinitionItem> GetDefinitions(
ISymbol symbol,
Project project,
Solution solution,
bool thirdPartyNavigationAllowed,
CancellationToken cancellationToken)
{
......@@ -36,13 +36,12 @@ internal static class GoToDefinitionHelpers
// VB global import aliases have a synthesized SyntaxTree.
// We can't go to the definition of the alias, so use the target type.
var solution = project.Solution;
if (alias != null)
{
var sourceLocations = NavigableItemFactory.GetPreferredSourceLocations(
solution, symbol, cancellationToken);
if (sourceLocations.All(l => project.Solution.GetDocument(l.SourceTree) == null))
if (sourceLocations.All(l => solution.GetDocument(l.SourceTree) == null))
{
symbol = alias.Target;
}
......@@ -81,7 +80,7 @@ internal static class GoToDefinitionHelpers
// So, if we only have a single location to go to, this does no unnecessary work. And,
// if we do have multiple locations to show, it will just be done in the BG, unblocking
// this command thread so it can return the user faster.
var definitionItem = symbol.ToNonClassifiedDefinitionItem(project, includeHiddenLocations: true);
var definitionItem = symbol.ToNonClassifiedDefinitionItem(solution, includeHiddenLocations: true);
if (thirdPartyNavigationAllowed)
{
......@@ -96,23 +95,23 @@ internal static class GoToDefinitionHelpers
public static bool TryGoToDefinition(
ISymbol symbol,
Project project,
Solution solution,
IStreamingFindUsagesPresenter streamingPresenter,
CancellationToken cancellationToken,
bool thirdPartyNavigationAllowed = true)
{
var definitions = GetDefinitions(symbol, project, thirdPartyNavigationAllowed, cancellationToken);
var definitions = GetDefinitions(symbol, solution, thirdPartyNavigationAllowed, cancellationToken);
var title = string.Format(EditorFeaturesResources._0_declarations,
FindUsagesHelpers.GetDisplayName(symbol));
return streamingPresenter.TryNavigateToOrPresentItemsAsync(
project.Solution.Workspace, title, definitions).WaitAndGetResult(cancellationToken);
solution.Workspace, title, definitions).WaitAndGetResult(cancellationToken);
}
public static bool TryGoToDefinition(
ImmutableArray<DefinitionItem> definitions,
Project project,
Solution solution,
string title,
IStreamingFindUsagesPresenter streamingPresenter,
CancellationToken cancellationToken)
......@@ -123,7 +122,7 @@ internal static class GoToDefinitionHelpers
}
return streamingPresenter.TryNavigateToOrPresentItemsAsync(
project.Solution.Workspace, title, definitions).WaitAndGetResult(cancellationToken);
solution.Workspace, title, definitions).WaitAndGetResult(cancellationToken);
}
}
}
......@@ -168,7 +168,7 @@ private static void NavigateToQuickInfoTarget(string navigationTarget, Document
if (resolvedSymbolKey.GetAnySymbol() is { } symbol)
{
GoToDefinitionHelpers.TryGoToDefinition(symbol, document.Project, streamingPresenter, CancellationToken.None);
GoToDefinitionHelpers.TryGoToDefinition(symbol, document.Project.Solution, streamingPresenter, CancellationToken.None);
return;
}
}
......
......@@ -62,7 +62,7 @@ public PeekableItemFactory(IMetadataAsSourceFileService metadataAsSourceFileServ
}
var symbolNavigationService = solution.Workspace.Services.GetService<ISymbolNavigationService>();
var definitionItem = symbol.ToNonClassifiedDefinitionItem(project, includeHiddenLocations: true);
var definitionItem = symbol.ToNonClassifiedDefinitionItem(solution, includeHiddenLocations: true);
if (symbolNavigationService.WouldNavigateToSymbol(
definitionItem, solution, cancellationToken,
......
......@@ -52,7 +52,7 @@ private class NavigableSymbol : INavigableSymbol
showProgress: false,
action: context => GoToDefinitionHelpers.TryGoToDefinition(
_definitions,
_document.Project,
_document.Project.Solution,
_definitions[0].NameDisplayParts.GetFullText(),
_presenter,
context.CancellationToken)
......
......@@ -213,7 +213,7 @@ internal abstract partial class DefinitionItem
ImmutableArray<string> tags,
ImmutableArray<TaggedText> displayParts,
ImmutableArray<TaggedText> nameDisplayParts,
Project project,
Solution solution,
ISymbol symbol,
ImmutableDictionary<string, string> properties = null,
bool displayIfNoReferences = true)
......@@ -222,9 +222,12 @@ internal abstract partial class DefinitionItem
var symbolKey = symbol.GetSymbolKey().ToString();
var projectId = solution.GetOriginatingProjectId(symbol);
Contract.ThrowIfNull(projectId);
properties = properties.Add(MetadataSymbolKey, symbolKey)
.Add(MetadataSymbolOriginatingProjectIdGuid, project.Id.Id.ToString())
.Add(MetadataSymbolOriginatingProjectIdDebugName, project.Id.DebugName);
.Add(MetadataSymbolOriginatingProjectIdGuid, projectId.Id.ToString())
.Add(MetadataSymbolOriginatingProjectIdDebugName, projectId.DebugName);
var originationParts = GetOriginationParts(symbol);
return new DefaultDefinitionItem(
......
......@@ -39,7 +39,8 @@ public async Task FindSymbolReferencesAsync(ISymbol symbol, Project project, Can
// the context object that the FAR service will push results into.
var context = streamingPresenter.StartSearch(EditorFeaturesResources.Find_References, supportsReferences: true);
await AbstractFindUsagesService.FindSymbolReferencesAsync(_threadingContext, context, symbol, project, cancellationToken).ConfigureAwait(false);
await AbstractFindUsagesService.FindSymbolReferencesAsync(
_threadingContext, context, symbol, project.Solution, cancellationToken).ConfigureAwait(false);
// Note: we don't need to put this in a finally. The only time we might not hit
// this is if cancellation or another error gets thrown. In the former case,
......
......@@ -553,8 +553,7 @@ private static async Task FindReferencesAsync(IThreadingContext threadingContext
if (symbol != null)
{
await AbstractFindUsagesService.FindSymbolReferencesAsync(
threadingContext,
context, symbol, project, cancellationToken).ConfigureAwait(false);
threadingContext, context, symbol, project.Solution, cancellationToken).ConfigureAwait(false);
}
}
}
......
......@@ -178,14 +178,14 @@ public bool TryNavigateToSymbol(ISymbol symbol, Project project, OptionSet optio
}
public bool TrySymbolNavigationNotify(ISymbol symbol, Project project, CancellationToken cancellationToken)
=> TryNotifyForSpecificSymbol(symbol, project, cancellationToken);
=> TryNotifyForSpecificSymbol(symbol, project.Solution, cancellationToken);
private bool TryNotifyForSpecificSymbol(
ISymbol symbol, Project project, CancellationToken cancellationToken)
ISymbol symbol, Solution solution, CancellationToken cancellationToken)
{
AssertIsForeground();
var definitionItem = symbol.ToNonClassifiedDefinitionItem(project, includeHiddenLocations: true);
var definitionItem = symbol.ToNonClassifiedDefinitionItem(solution, includeHiddenLocations: true);
definitionItem.Properties.TryGetValue(DefinitionItem.RQNameKey1, out var rqName);
if (!TryGetNavigationAPIRequiredArguments(
......
......@@ -122,7 +122,7 @@ internal override IInvisibleEditor OpenInvisibleEditor(DocumentId documentId)
}
return GoToDefinitionHelpers.TryGoToDefinition(
searchSymbol, searchProject,
searchSymbol, searchProject.Solution,
_streamingPresenter.Value, cancellationToken);
}
......
......@@ -15,9 +15,8 @@ public static ImmutableArray<ISymbol> FindBaseTypesAndInterfaces(INamedTypeSymbo
=> FindBaseTypes(type).AddRange(type.AllInterfaces).CastArray<ISymbol>();
public static ImmutableArray<ISymbol> FindOverriddenAndImplementedMembers(
ISymbol symbol, Project project, CancellationToken cancellationToken)
ISymbol symbol, Solution solution, CancellationToken cancellationToken)
{
var solution = project.Solution;
var results = ArrayBuilder<ISymbol>.GetInstance();
// This is called for all: class, struct or interface member.
......
......@@ -325,7 +325,7 @@ public System.Uri Get()
var projectIds = new HashSet<ProjectId>();
foreach (var r in references)
projectIds.Add(solution.GetExactProjectId(r.Definition));
projectIds.Add(solution.GetOriginatingProjectId(r.Definition));
Assert.True(projectIds.Contains(desktopProject.Id));
}
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册