Browse Source

Fixed FindReferences on attributes.

newNRvisualizers
Daniel Grunwald 14 years ago
parent
commit
33e0c0e9a0
  1. 42
      ICSharpCode.NRefactory.CSharp/Resolver/FindReferences.cs
  2. 2
      ICSharpCode.NRefactory.Demo/CSDemo.cs
  3. 17
      ICSharpCode.NRefactory/TypeSystem/ExtensionMethods.cs
  4. 2
      ICSharpCode.NRefactory/TypeSystem/Implementation/DefaultSolutionSnapshot.cs

42
ICSharpCode.NRefactory.CSharp/Resolver/FindReferences.cs

@ -189,7 +189,7 @@ namespace ICSharpCode.NRefactory.CSharp.Resolver @@ -189,7 +189,7 @@ namespace ICSharpCode.NRefactory.CSharp.Resolver
SearchScope additionalScope = null;
switch (entity.EntityType) {
case EntityType.TypeDefinition:
scope = FindTypeDefinitionReferences((ITypeDefinition)entity, this.FindTypeReferencesEvenIfAliased);
scope = FindTypeDefinitionReferences((ITypeDefinition)entity, this.FindTypeReferencesEvenIfAliased, out additionalScope);
break;
case EntityType.Field:
if (entity.DeclaringTypeDefinition != null && entity.DeclaringTypeDefinition.Kind == TypeKind.Enum)
@ -353,14 +353,28 @@ namespace ICSharpCode.NRefactory.CSharp.Resolver @@ -353,14 +353,28 @@ namespace ICSharpCode.NRefactory.CSharp.Resolver
#endregion
#region Find TypeDefinition References
SearchScope FindTypeDefinitionReferences(ITypeDefinition typeDefinition, bool findTypeReferencesEvenIfAliased)
SearchScope FindTypeDefinitionReferences(ITypeDefinition typeDefinition, bool findTypeReferencesEvenIfAliased, out SearchScope additionalScope)
{
string searchTerm = null;
if (!findTypeReferencesEvenIfAliased && ReflectionHelper.GetTypeCode(typeDefinition) == TypeCode.Empty) {
// not a built-in type
additionalScope = null;
if (!findTypeReferencesEvenIfAliased && KnownTypeReference.GetCSharpNameByTypeCode(typeDefinition.KnownTypeCode) == null) {
// We can optimize the search by looking only for the type references with the right identifier,
// but only if it's not a primitive type and we're not looking for indirect references (through an alias)
searchTerm = typeDefinition.Name;
if (searchTerm.Length > 9 && searchTerm.EndsWith("Attribute", StringComparison.Ordinal)) {
// The type might be an attribute, so we also need to look for the short form:
string shortForm = searchTerm.Substring(0, searchTerm.Length - 9);
additionalScope = new SearchScope(
shortForm,
delegate (ICompilation compilation) {
ITypeDefinition imported = compilation.Import(typeDefinition);
if (imported != null)
return new FindTypeDefinitionReferencesNavigator(imported, shortForm);
else
return null;
});
}
}
return new SearchScope(
searchTerm,
delegate (ICompilation compilation) {
@ -404,7 +418,15 @@ namespace ICSharpCode.NRefactory.CSharp.Resolver @@ -404,7 +418,15 @@ namespace ICSharpCode.NRefactory.CSharp.Resolver
if (searchTerm == null && node is PrimitiveType)
return true;
return node is TypeDeclaration || node is DelegateDeclaration;
TypeDeclaration typeDecl = node as TypeDeclaration;
if (typeDecl != null)
return searchTerm == null || typeDecl.Name == searchTerm;
DelegateDeclaration delegateDecl = node as DelegateDeclaration;
if (delegateDecl != null)
return searchTerm == null || delegateDecl.Name == searchTerm;
return false;
}
internal override bool IsMatch(ResolveResult rr)
@ -872,9 +894,13 @@ namespace ICSharpCode.NRefactory.CSharp.Resolver @@ -872,9 +894,13 @@ namespace ICSharpCode.NRefactory.CSharp.Resolver
{
ctor = (IMethod)ctor.MemberDefinition;
string searchTerm = null;
if (ReflectionHelper.GetTypeCode(ctor.DeclaringTypeDefinition) == TypeCode.Empty) {
if (KnownTypeReference.GetCSharpNameByTypeCode(ctor.DeclaringTypeDefinition.KnownTypeCode) == null) {
// not a built-in type
searchTerm = ctor.DeclaringTypeDefinition.Name;
if (searchTerm.Length > 9 && searchTerm.EndsWith("Attribute", StringComparison.Ordinal)) {
// we also need to look for the short form
searchTerm = null;
}
}
return new SearchScope(
searchTerm,
@ -898,7 +924,7 @@ namespace ICSharpCode.NRefactory.CSharp.Resolver @@ -898,7 +924,7 @@ namespace ICSharpCode.NRefactory.CSharp.Resolver
internal override bool CanMatch(AstNode node)
{
return node is ObjectCreateExpression || node is ConstructorDeclaration;
return node is ObjectCreateExpression || node is ConstructorDeclaration || node is Attribute;
}
internal override bool IsMatch(ResolveResult rr)

2
ICSharpCode.NRefactory.Demo/CSDemo.cs

@ -312,10 +312,12 @@ namespace ICSharpCode.NRefactory.Demo @@ -312,10 +312,12 @@ namespace ICSharpCode.NRefactory.Demo
FindReferences fr = new FindReferences();
int referenceCount = 0;
FoundReferenceCallback callback = delegate(AstNode matchNode, ResolveResult result) {
Debug.WriteLine(matchNode.StartLocation + " - " + matchNode + " - " + result);
referenceCount++;
};
var searchScopes = fr.GetSearchScopes(entity);
Debug.WriteLine("Find references to " + entity.ReflectionName);
fr.FindReferencesInFile(searchScopes, parsedFile, compilationUnit, compilation, callback, CancellationToken.None);
MessageBox.Show("Found " + referenceCount + " references to " + entity.FullName);

17
ICSharpCode.NRefactory/TypeSystem/ExtensionMethods.cs

@ -252,16 +252,33 @@ namespace ICSharpCode.NRefactory.TypeSystem @@ -252,16 +252,33 @@ namespace ICSharpCode.NRefactory.TypeSystem
#endregion
#region GetType/Member
/// <summary>
/// Gets all unresolved type definitions from the file.
/// For partial classes, each part is returned.
/// </summary>
public static IEnumerable<IUnresolvedTypeDefinition> GetAllTypeDefinitions (this IParsedFile file)
{
return TreeTraversal.PreOrder(file.TopLevelTypeDefinitions, t => t.NestedTypes);
}
/// <summary>
/// Gets all unresolved type definitions from the assembly.
/// For partial classes, each part is returned.
/// </summary>
public static IEnumerable<IUnresolvedTypeDefinition> GetAllTypeDefinitions (this IUnresolvedAssembly assembly)
{
return TreeTraversal.PreOrder(assembly.TopLevelTypeDefinitions, t => t.NestedTypes);
}
public static IEnumerable<ITypeDefinition> GetAllTypeDefinitions (this IAssembly assembly)
{
return TreeTraversal.PreOrder(assembly.TopLevelTypeDefinitions, t => t.NestedTypes);
}
/// <summary>
/// Gets all type definitions in the compilation.
/// This may include types from referenced assemblies that are not accessible in the main assembly.
/// </summary>
public static IEnumerable<ITypeDefinition> GetAllTypeDefinitions (this ICompilation compilation)
{
return compilation.MainAssembly.GetAllTypeDefinitions()

2
ICSharpCode.NRefactory/TypeSystem/Implementation/DefaultSolutionSnapshot.cs

@ -24,7 +24,7 @@ namespace ICSharpCode.NRefactory.TypeSystem.Implementation @@ -24,7 +24,7 @@ namespace ICSharpCode.NRefactory.TypeSystem.Implementation
/// <summary>
/// Default implementation of ISolutionSnapshot.
/// </summary>
public sealed class DefaultSolutionSnapshot : ISolutionSnapshot
public class DefaultSolutionSnapshot : ISolutionSnapshot
{
ConcurrentDictionary<IProjectContent, ICompilation> dictionary = new ConcurrentDictionary<IProjectContent, ICompilation>();

Loading…
Cancel
Save