Example #1
0
        SearchScope FindObjectCreateReferences(IMethod ctor)
        {
            ctor = (IMethod)ctor.MemberDefinition;
            string searchTerm = null;

            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,
                       delegate(ICompilation compilation) {
                IMethod imported = compilation.Import(ctor);
                if (imported != null)
                {
                    return new FindObjectCreateReferencesNavigator(imported);
                }
                else
                {
                    return null;
                }
            }));
        }
        protected AstType ConvertType(KnownTypeCode knownTypeCode)
        {
            IType type = refactoringContext.Compilation.FindType(knownTypeCode);

            if (type != null)
            {
                return(ConvertType(type));
            }

            // Backup solution
            return(new SimpleType(KnownTypeReference.GetCSharpNameByTypeCode(knownTypeCode)));
        }
Example #3
0
        SearchScope FindTypeDefinitionReferences(ITypeDefinition typeDefinition, bool findTypeReferencesEvenIfAliased, out SearchScope additionalScope)
        {
            string searchTerm = null;

            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) {
                ITypeDefinition imported = compilation.Import(typeDefinition);
                if (imported != null)
                {
                    return new FindTypeDefinitionReferencesNavigator(imported, searchTerm);
                }
                else
                {
                    return null;
                }
            }));
        }
        AstType ConvertTypeHelper(ITypeDefinition typeDef, IList <IType> typeArguments)
        {
            Debug.Assert(typeArguments.Count >= typeDef.TypeParameterCount);

            string keyword = KnownTypeReference.GetCSharpNameByTypeCode(typeDef.KnownTypeCode);

            if (keyword != null)
            {
                return(new PrimitiveType(keyword));
            }

            // The number of type parameters belonging to outer classes
            int outerTypeParameterCount;

            if (typeDef.DeclaringType != null)
            {
                outerTypeParameterCount = typeDef.DeclaringType.TypeParameterCount;
            }
            else
            {
                outerTypeParameterCount = 0;
            }

            if (resolver != null)
            {
                // Look if there's an alias to the target type
                if (UseAliases)
                {
                    for (ResolvedUsingScope usingScope = resolver.CurrentUsingScope; usingScope != null; usingScope = usingScope.Parent)
                    {
                        foreach (var pair in usingScope.UsingAliases)
                        {
                            if (pair.Value is TypeResolveResult)
                            {
                                if (TypeMatches(pair.Value.Type, typeDef, typeArguments))
                                {
                                    return(new SimpleType(pair.Key));
                                }
                            }
                        }
                    }
                }

                IList <IType> localTypeArguments;
                if (typeDef.TypeParameterCount > outerTypeParameterCount)
                {
                    localTypeArguments = new IType[typeDef.TypeParameterCount - outerTypeParameterCount];
                    for (int i = 0; i < localTypeArguments.Count; i++)
                    {
                        localTypeArguments[i] = typeArguments[outerTypeParameterCount + i];
                    }
                }
                else
                {
                    localTypeArguments = EmptyList <IType> .Instance;
                }
                ResolveResult     rr  = resolver.ResolveSimpleName(typeDef.Name, localTypeArguments);
                TypeResolveResult trr = rr as TypeResolveResult;
                if (trr != null || (localTypeArguments.Count == 0 && resolver.IsVariableReferenceWithSameType(rr, typeDef.Name, out trr)))
                {
                    if (!trr.IsError && TypeMatches(trr.Type, typeDef, typeArguments))
                    {
                        // We can use the short type name
                        SimpleType shortResult = new SimpleType(typeDef.Name);
                        AddTypeArguments(shortResult, typeDef, typeArguments, outerTypeParameterCount, typeDef.TypeParameterCount);
                        return(shortResult);
                    }
                }
            }

            if (AlwaysUseShortTypeNames)
            {
                var shortResult = new SimpleType(typeDef.Name);
                AddTypeArguments(shortResult, typeDef, typeArguments, outerTypeParameterCount, typeDef.TypeParameterCount);
                return(shortResult);
            }
            MemberType result = new MemberType();

            if (typeDef.DeclaringTypeDefinition != null)
            {
                // Handle nested types
                result.Target = ConvertTypeHelper(typeDef.DeclaringTypeDefinition, typeArguments);
            }
            else
            {
                // Handle top-level types
                if (string.IsNullOrEmpty(typeDef.Namespace))
                {
                    result.Target        = new SimpleType("global");
                    result.IsDoubleColon = true;
                }
                else
                {
                    result.Target = ConvertNamespace(typeDef.Namespace);
                }
            }
            result.MemberName = typeDef.Name;
            AddTypeArguments(result, typeDef, typeArguments, outerTypeParameterCount, typeDef.TypeParameterCount);
            return(result);
        }