1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160
|
// Copyright (c) 2013 AlphaSierraPapa for the SharpDevelop Team
//
// Permission is hereby granted, free of charge, to any person obtaining a copy of this
// software and associated documentation files (the "Software"), to deal in the Software
// without restriction, including without limitation the rights to use, copy, modify, merge,
// publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons
// to whom the Software is furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in all copies or
// substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED,
// INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR
// PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE
// FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
// OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
// DEALINGS IN THE SOFTWARE.
using System;
using System.Collections.Generic;
using ICSharpCode.NRefactory.TypeSystem;
using System.Linq;
namespace ICSharpCode.NRefactory.Analysis
{
/// <summary>
/// The symbol collector collects related symbols that form a group of symbols that should be renamed
/// when a name of one symbol changes. For example if a type definition name should be changed
/// the constructors and destructor names should change as well.
/// </summary>
public class SymbolCollector
{
/// <summary>
/// Gets or sets a value indicating whether this <see cref="ICSharpCode.NRefactory.Analysis.SymbolCollector"/> should include overloads.
/// </summary>
/// <value><c>true</c> if overloads should be included; otherwise, <c>false</c>.</value>
public bool IncludeOverloads {
get;
set;
}
public bool GroupForRenaming {
get;
set;
}
static IEnumerable<ISymbol> CollectTypeRelatedMembers (ITypeDefinition type)
{
yield return type;
foreach (var c in type.GetDefinition ().GetMembers (m => !m.IsSynthetic && (m.SymbolKind == SymbolKind.Constructor || m.SymbolKind == SymbolKind.Destructor), GetMemberOptions.IgnoreInheritedMembers)) {
yield return c;
}
}
static IEnumerable<ISymbol> CollectOverloads (TypeGraph g, IMethod method)
{
return method.DeclaringType
.GetMethods (m => m.Name == method.Name)
.Where (m => m != method);
}
static IMember SearchMember (ITypeDefinition derivedType, IMember method)
{
foreach (var m in derivedType.Members) {
if (m.ImplementedInterfaceMembers.Contains (method))
return m;
}
return null;
}
static IEnumerable<ISymbol> MakeUnique (List<ISymbol> symbols)
{
HashSet<ISymbol> taken = new HashSet<ISymbol> ();
foreach (var sym in symbols) {
if (taken.Contains (sym))
continue;
taken.Add (sym);
yield return sym;
}
}
/// <summary>
/// Gets the related symbols.
/// </summary>
/// <returns>The related symbols.</returns>
/// <param name="g">The type graph.</param>
/// <param name="m">The symbol to search</param>
public IEnumerable<ISymbol> GetRelatedSymbols(TypeGraph g, ISymbol m)
{
switch (m.SymbolKind) {
case SymbolKind.TypeDefinition:
return CollectTypeRelatedMembers ((ITypeDefinition)m);
case SymbolKind.Field:
case SymbolKind.Operator:
case SymbolKind.Variable:
case SymbolKind.Parameter:
case SymbolKind.TypeParameter:
return new ISymbol[] { m };
case SymbolKind.Constructor:
if (GroupForRenaming)
return GetRelatedSymbols (g, ((IMethod)m).DeclaringTypeDefinition);
List<ISymbol> constructorSymbols = new List<ISymbol> ();
if (IncludeOverloads) {
foreach (var m3 in CollectOverloads (g, (IMethod)m)) {
constructorSymbols.Add (m3);
}
}
return constructorSymbols;
case SymbolKind.Destructor:
if (GroupForRenaming)
return GetRelatedSymbols (g, ((IMethod)m).DeclaringTypeDefinition);
return new ISymbol[] { m };
case SymbolKind.Indexer:
case SymbolKind.Event:
case SymbolKind.Property:
case SymbolKind.Method: {
var member = (IMember)m;
List<ISymbol> symbols = new List<ISymbol> ();
if (member.ImplementedInterfaceMembers.Count > 0) {
foreach (var m2 in member.ImplementedInterfaceMembers) {
symbols.AddRange (GetRelatedSymbols (g, m2));
}
} else {
symbols.Add (member);
}
if (member.DeclaringType.Kind == TypeKind.Interface) {
foreach (var derivedType in g.GetNode (member.DeclaringTypeDefinition).DerivedTypes) {
var mem = SearchMember (derivedType.TypeDefinition, member);
if (mem != null)
symbols.Add (mem);
}
}
if (IncludeOverloads) {
IncludeOverloads = false;
if (member is IMethod) {
foreach (var m3 in CollectOverloads (g, (IMethod)member)) {
symbols.AddRange (GetRelatedSymbols (g, m3));
}
} else if (member.SymbolKind == SymbolKind.Indexer) {
symbols.AddRange (member.DeclaringTypeDefinition.GetProperties (p => p.IsIndexer));
}
}
return MakeUnique (symbols);
}
case SymbolKind.Namespace:
// TODO?
return new ISymbol[] { m };
default:
throw new ArgumentOutOfRangeException ("symbol:"+m.SymbolKind);
}
}
}
}
|