提交 c267169e 编写于 作者: C CyrusNajmabadi

Simplify find refs filtering.

上级 97e6eb79
......@@ -98,7 +98,7 @@ private bool ShouldConsiderSymbol(ISymbol symbol)
private async Task<IEnumerable<DocumentHighlights>> FilterAndCreateSpansAsync(
IEnumerable<ReferencedSymbol> references, Solution solution, IImmutableSet<Document> documentsToSearch, ISymbol symbol, CancellationToken cancellationToken)
{
references = references.FilterUnreferencedSyntheticDefinitions();
references = references.FilterToItemsToShow();
references = references.FilterNonMatchingMethodNames(solution, symbol);
references = references.FilterToAliasMatches(symbol as IAliasSymbol);
......
......@@ -17,9 +17,9 @@ Imports System.Linq
Module Program
Function Main(args As String())
Dim q = Function(e As Integer)
Dim q = {|Definition:Function(e As Integer)
Return True
End Function.$$[|Invoke|](42)
End Function|}.$$[|Invoke|](42)
Dim r = Function(e2 As Integer)
Return True
......@@ -32,4 +32,4 @@ End Module
Await TestAsync(input)
End Function
End Class
End Namespace
End Namespace
\ No newline at end of file
......@@ -11,6 +11,8 @@ Imports Xunit.Abstractions
Namespace Microsoft.CodeAnalysis.Editor.UnitTests.FindReferences
Partial Public Class FindReferencesTests
Private Const DefinitionKey As String = "Definition"
Private ReadOnly _outputHelper As ITestOutputHelper
Public Sub New(outputHelper As ITestOutputHelper)
......@@ -38,14 +40,8 @@ Namespace Microsoft.CodeAnalysis.Editor.UnitTests.FindReferences
End If
Dim actualDefinitions =
result.FilterUnreferencedSyntheticDefinitions().
Where(Function(r)
Return Not r.Definition.IsImplicitlyDeclared OrElse
(r.Definition.Kind = SymbolKind.Property AndAlso
r.Definition.ContainingSymbol IsNot Nothing AndAlso
r.Definition.ContainingSymbol.Kind = SymbolKind.NamedType AndAlso
DirectCast(r.Definition.ContainingSymbol, INamedTypeSymbol).IsAnonymousType)
End Function).
result.FilterToItemsToShow().
Where(Function(s) Not IsImplicitNamespace(s)).
SelectMany(Function(r) r.Definition.Locations).
Where(Function(loc) IsInSource(workspace, loc, uiVisibleOnly)).
GroupBy(Function(loc) loc.SourceTree).
......@@ -56,11 +52,11 @@ Namespace Microsoft.CodeAnalysis.Editor.UnitTests.FindReferences
Dim documentsWithAnnotatedSpans = workspace.Documents.Where(Function(d) d.AnnotatedSpans.Any())
Assert.Equal(Of String)(documentsWithAnnotatedSpans.Select(Function(d) GetFilePathAndProjectLabel(workspace, d)).Order(), actualDefinitions.Keys.Order())
For Each doc In documentsWithAnnotatedSpans
Assert.Equal(Of Text.TextSpan)(doc.AnnotatedSpans("Definition").Order(), actualDefinitions(GetFilePathAndProjectLabel(workspace, doc)).Order())
Assert.Equal(Of Text.TextSpan)(doc.AnnotatedSpans(DefinitionKey).Order(), actualDefinitions(GetFilePathAndProjectLabel(workspace, doc)).Order())
Next
Dim actualReferences =
result.FilterUnreferencedSyntheticDefinitions().
result.FilterToItemsToShow().
SelectMany(Function(r) r.Locations.Select(Function(loc) loc.Location)).
Where(Function(loc) IsInSource(workspace, loc, uiVisibleOnly)).
Distinct().
......@@ -82,6 +78,11 @@ Namespace Microsoft.CodeAnalysis.Editor.UnitTests.FindReferences
End Using
End Function
Private Function IsImplicitNamespace(referencedSymbol As ReferencedSymbol) As Boolean
Return referencedSymbol.Definition.IsImplicitlyDeclared AndAlso
referencedSymbol.Definition.Kind = SymbolKind.Namespace
End Function
Private Shared Function IsInSource(workspace As Workspace, loc As Location, uiVisibleOnly As Boolean) As Boolean
If uiVisibleOnly Then
Return loc.IsInSource AndAlso Not loc.SourceTree.IsHiddenPosition(loc.SourceSpan.Start)
......
......@@ -42,15 +42,10 @@ internal IList<AbstractTreeItem> CreateFindReferencesItems(Solution solution, IE
var uniqueLocations = new HashSet<ValueTuple<Document, TextSpan>>();
var symbolNavigationService = solution.Workspace.Services.GetService<ISymbolNavigationService>();
referencedSymbols = referencedSymbols.FilterUnreferencedSyntheticDefinitions().ToList();
referencedSymbols = referencedSymbols.FilterToItemsToShow().ToList();
foreach (var referencedSymbol in referencedSymbols.OrderBy(GetDefinitionPrecedence))
{
if (!IncludeDefinition(referencedSymbol))
{
continue;
}
var definition = referencedSymbol.Definition;
var locations = definition.Locations;
......@@ -75,6 +70,7 @@ internal IList<AbstractTreeItem> CreateFindReferencesItems(Solution solution, IE
if (definitionItem != null)
{
definitions.Add(definitionItem);
var referenceItems = CreateReferenceItems(solution, uniqueLocations, referencedSymbol.Locations.Select(loc => loc.Location));
definitionItem.Children.AddRange(referenceItems);
definitionItem.SetReferenceCount(referenceItems.Count);
......
......@@ -11,29 +11,40 @@ namespace Microsoft.CodeAnalysis.Shared.Extensions
{
internal static partial class IFindReferencesResultExtensions
{
public static IEnumerable<ReferencedSymbol> FilterUnreferencedSyntheticDefinitions(
public static IEnumerable<ReferencedSymbol> FilterToItemsToShow(
this IEnumerable<ReferencedSymbol> result)
{
return result.Where(ShouldKeep);
return result.Where(ShouldShow);
}
private static bool ShouldKeep(ReferencedSymbol r)
private static bool ShouldShow(ReferencedSymbol referencedSymbol)
{
if (r.Locations.Any())
// If the reference has any locations then we will present it.
if (referencedSymbol.Locations.Any())
{
return true;
}
if (r.Definition.IsImplicitlyDeclared)
return referencedSymbol.Definition.ShouldShowWithNoReferenceLocations();
}
public static bool ShouldShowWithNoReferenceLocations(this ISymbol definition)
{
// If the definition is implicit and we have no references, then we don't want to
// clutter the UI with it.
if (definition.IsImplicitlyDeclared)
{
return false;
}
if (r.Definition.IsPropertyAccessor())
// We don't want to clutter the UI with property accessors if there are no direct
// references to them.
if (definition.IsPropertyAccessor())
{
return false;
}
// Otherwise we still show the item even if there are no references to it.
return true;
}
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册