提交 838d8ed2 编写于 作者: C CyrusNajmabadi

Merge branch 'commonPathElements' into findRefLanguageModel

// 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.Immutable;
using System.Linq;
using Microsoft.CodeAnalysis;
using Microsoft.CodeAnalysis.Editor.Implementation.FindReferences;
using Microsoft.CodeAnalysis.FindSymbols;
using Microsoft.CodeAnalysis.Navigation;
using Microsoft.CodeAnalysis.Shared.Extensions;
using Microsoft.CodeAnalysis.Text;
using Microsoft.VisualStudio.LanguageServices.Implementation.Utilities;
using Roslyn.Utilities;
......@@ -29,14 +24,22 @@ public void PresentDefinitionsAndReferences(DefinitionsAndReferences definitions
internal IList<AbstractTreeItem> CreateFindReferencesItems(
DefinitionsAndReferences definitionsAndReferences)
{
var definitionItems = definitionsAndReferences.Definitions;
return definitionItems.SelectMany(d => CreateDefinitionItems(d, definitionsAndReferences))
.ToList();
var documents = definitionsAndReferences.References.Select(r => r.Location.Document)
.WhereNotNull()
.ToSet();
var commonPathElements = CountCommonPathElements(documents);
var query = from d in definitionsAndReferences.Definitions
from i in CreateDefinitionItems(d, definitionsAndReferences, commonPathElements)
select i;
return query.ToList();
}
private IEnumerable<AbstractTreeItem> CreateDefinitionItems(
DefinitionItem definitionItem,
DefinitionsAndReferences definitionsAndReferences)
DefinitionsAndReferences definitionsAndReferences,
int commonPathElements)
{
// Each definition item may end up as several top nodes (because of partials).
// Add the references to the last item actually in the list.
......@@ -44,24 +47,14 @@ public void PresentDefinitionsAndReferences(DefinitionsAndReferences definitions
if (!definitionTreeItems.IsEmpty)
{
var lastTreeItem = definitionTreeItems.Last();
var referenceItems = CreateReferenceItems(definitionItem, definitionsAndReferences);
var referenceItems = CreateReferenceItems(
definitionItem, definitionsAndReferences, commonPathElements);
lastTreeItem.Children.AddRange(referenceItems);
lastTreeItem.SetReferenceCount(referenceItems.Count);
}
return definitionTreeItems;
// // Add on any definition locations from third party language services
// string filePath;
// int lineNumber, charOffset;
// if (symbolNavigationService.WouldNavigateToSymbol(definition, solution, out filePath, out lineNumber, out charOffset))
// {
// definitions.Add(new ExternalLanguageDefinitionTreeItem(filePath, lineNumber, charOffset, definition.Name, definition.GetGlyph().GetGlyphIndex(), this.ServiceProvider));
// }
//}
//return definitions;
}
private ImmutableArray<AbstractTreeItem> ConvertToDefinitionTreeItems(
......@@ -75,30 +68,12 @@ public void PresentDefinitionsAndReferences(DefinitionsAndReferences definitions
}
return result.ToImmutable();
//if (!location.IsInSource)
//{
// return referencedSymbol.Locations.Any()
// ? new MetadataDefinitionTreeItem(
// solution.Workspace,
// referencedSymbol.Definition,
// referencedSymbol.Locations.First().Document.Project.Id,
// glyph.GetGlyphIndex())
// : null;
//}
//var document = solution.GetDocument(location.SourceTree);
//var sourceSpan = location.SourceSpan;
//if (!IsValidSourceLocation(document, sourceSpan))
//{
// return null;
//}
//return new SourceDefinitionTreeItem(document, sourceSpan, referencedSymbol.Definition, glyph.GetGlyphIndex());
}
private IList<SourceReferenceTreeItem> CreateReferenceItems(
DefinitionItem definitionItem,
DefinitionsAndReferences definitionsAndReferences)
DefinitionsAndReferences definitionsAndReferences,
int commonPathElements)
{
var result = new List<SourceReferenceTreeItem>();
......@@ -107,9 +82,10 @@ public void PresentDefinitionsAndReferences(DefinitionsAndReferences definitions
{
var documentLocation = referenceItem.Location;
result.Add(new SourceReferenceTreeItem(
documentLocation.Document,
documentLocation.Document,
documentLocation.SourceSpan,
Glyph.Reference.GetGlyphIndex()));
Glyph.Reference.GetGlyphIndex(),
commonPathElements));
}
var linkedReferences = result.GroupBy(r => r.DisplayText.ToLowerInvariant()).Where(g => g.Count() > 1).SelectMany(g => g);
......@@ -122,4 +98,4 @@ public void PresentDefinitionsAndReferences(DefinitionsAndReferences definitions
return result;
}
}
}
}
\ No newline at end of file
// 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.Diagnostics;
using System.Linq;
......@@ -32,10 +31,15 @@ private IList<AbstractTreeItem> CreateNavigableItemTreeItems(IEnumerable<INaviga
return CreateNavigableItemTreeItems(itemsList, commonPathElements);
}
private int CountCommonPathElements(HashSet<Document> documents)
private int CountCommonPathElements(ISet<Document> documents)
{
Debug.Assert(documents.Count > 0);
if (documents.Count == 0)
{
return 0;
}
var commonPathElements = 0;
for (var index = 0; ; index++)
{
var pathPortion = GetPathPortion(documents.First(), index);
......
......@@ -29,7 +29,8 @@ internal class SourceReferenceTreeItem : AbstractTreeItem, IComparable<SourceRef
private static readonly ObjectPool<StringBuilder> s_filePathBuilderPool = new ObjectPool<StringBuilder>(() => new StringBuilder());
private SourceReferenceTreeItem(Document document, TextSpan sourceSpan, ushort glyphIndex, int commonPathElements = 0)
private SourceReferenceTreeItem(
Document document, TextSpan sourceSpan, ushort glyphIndex, int commonPathElements)
: base(glyphIndex)
{
// We store the document ID, line and offset for navigation so that we
......@@ -58,7 +59,9 @@ private SourceReferenceTreeItem(Document document, TextSpan sourceSpan, ushort g
_mappedOffset = succeeded ? spanInPrimaryBuffer.iStartIndex : _offset;
}
public SourceReferenceTreeItem(Document document, TextSpan sourceSpan, ushort glyphIndex, int commonPathElements = 0, string displayText = null, bool includeFileLocation = false)
public SourceReferenceTreeItem(
Document document, TextSpan sourceSpan, ushort glyphIndex,
int commonPathElements, string displayText = null, bool includeFileLocation = false)
: this(document, sourceSpan, glyphIndex, commonPathElements)
{
if (displayText != null && !includeFileLocation)
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册