Exemplo n.º 1
0
        private void AppendTypeElement([NotNull] ITypeElement typeElement, [NotNull] ISubstitution substitution, QualifierDisplays expectedQualifierDisplay, Context context)
        {
            if (!(typeElement is ITypeParameter) && (context.Options.ShowQualifiers & expectedQualifierDisplay) != QualifierDisplays.None)
            {
                INamespace containingNamespace = typeElement.GetContainingNamespace();
                AppendNamespace(containingNamespace);
                if (!containingNamespace.IsRootNamespace)
                {
                    AppendText(".", _highlighterIdProvider.Operator);
                }

                ITypeElement containingType = typeElement.GetContainingType();
                if (containingType != null && !(typeElement is IDelegate && context.Options.FormatDelegatesAsLambdas))
                {
                    AppendDeclaredType(TypeFactory.CreateType(containingType, substitution), QualifierDisplays.None, context);
                    AppendText(".", _highlighterIdProvider.Operator);
                }
            }

            var deleg = typeElement as IDelegate;

            if (deleg != null && context.Options.FormatDelegatesAsLambdas && expectedQualifierDisplay == QualifierDisplays.Parameters)
            {
                AppendParameters(deleg.InvokeMethod, substitution, false, context);
                AppendText(" => ", _highlighterIdProvider.Operator);
                AppendTypeWithoutModule(substitution.Apply(deleg.InvokeMethod.ReturnType), expectedQualifierDisplay, context);
                return;
            }

            string highlighterId = _highlighterIdProvider.GetForTypeElement(typeElement);

            AppendText(FormatShortName(typeElement.ShortName), highlighterId);
            AppendTypeParameters(typeElement, substitution, context);
        }
Exemplo n.º 2
0
        private void AppendTypeElement([NotNull] ITypeElement typeElement, [NotNull] ISubstitution substitution, NamespaceDisplays expectedNamespaceDisplay)
        {
            if (!(typeElement is ITypeParameter))
            {
                if ((_options.ShowNamespaces & expectedNamespaceDisplay) != NamespaceDisplays.None)
                {
                    AppendNamespace(typeElement.GetContainingNamespace());
                    AppendText(".", VsHighlightingAttributeIds.Operator);
                }

                ITypeElement containingType = typeElement.GetContainingType();
                if (containingType != null && !(typeElement is IDelegate && _options.FormatDelegatesAsLambdas))
                {
                    AppendDeclaredType(TypeFactory.CreateType(containingType, substitution), NamespaceDisplays.None);
                    AppendText(".", VsHighlightingAttributeIds.Operator);
                }
            }

            var deleg = typeElement as IDelegate;

            if (deleg != null && _options.FormatDelegatesAsLambdas && expectedNamespaceDisplay == NamespaceDisplays.Parameters)
            {
                AppendParameters(deleg.InvokeMethod, substitution, false);
                AppendText(" => ", VsHighlightingAttributeIds.Operator);
                AppendType(substitution.Apply(deleg.InvokeMethod.ReturnType), expectedNamespaceDisplay);
                return;
            }

            string attributeId = _options.UseReSharperColors
                                ? HighlightingAttributeIds.GetHighlightAttributeForTypeElement(typeElement)
                                : VsHighlightingAttributeIds.GetForTypeElement(typeElement);

            AppendText(FormatShortName(typeElement.ShortName), attributeId);
            AppendTypeParameters(typeElement, substitution);
        }
Exemplo n.º 3
0
        public static Guid?GetGuidFor(MetaFileGuidCache metaFileGuidCache, ITypeElement typeElement)
        {
            var sourceFile = typeElement.GetDeclarations().FirstOrDefault()?.GetSourceFile();

            if (sourceFile == null || !sourceFile.IsValid())
            {
                return(null);
            }

            if (typeElement.TypeParameters.Count != 0)
            {
                return(null);
            }

            if (typeElement.GetContainingType() != null)
            {
                return(null);
            }

            if (!typeElement.ShortName.Equals(sourceFile.GetLocation().NameWithoutExtension))
            {
                return(null);
            }

            var guid = metaFileGuidCache.GetAssetGuid(sourceFile);

            return(guid);
        }
Exemplo n.º 4
0
        public static Guid?GetGuidFor(MetaFileGuidCache metaFileGuidCache, ITypeElement typeElement)
        {
            // partial classes
            var declarations = typeElement.GetDeclarations();

            foreach (var declaration in declarations)
            {
                var sourceFile = declaration.GetSourceFile();
                if (sourceFile == null || !sourceFile.IsValid())
                {
                    continue;
                }

                if (!typeElement.ShortName.Equals(sourceFile.GetLocation().NameWithoutExtension))
                {
                    continue;
                }

                if (typeElement.TypeParameters.Count != 0)
                {
                    continue;
                }

                if (typeElement.GetContainingType() != null)
                {
                    continue;
                }

                var guid = metaFileGuidCache.GetAssetGuid(sourceFile);
                return(guid);
            }

            return(null);
        }
Exemplo n.º 5
0
        private int GetGenericArgumentsCount(ITypeElement typeElement)
        {
            int count = 0;

            while (typeElement != null)
            {
                count      += typeElement.TypeParameters.Count;
                typeElement = typeElement.GetContainingType();
            }

            return(count);
        }
        private static string GetAssemblyQualifiedName(this ITypeElement type,
                                                       ISubstitution substitution,
                                                       IDictionary <DeclaredElementInstance, IName> seenElements)
        {
            if (type == null)
            {
                return(Names.UnknownType.Identifier);
            }

            var clrTypeName      = type.GetClrName();
            var containingModule = type.Module.ContainingProjectModule;

            Asserts.NotNull(containingModule, "module is null");
            var moduleName = containingModule.GetQualifiedName();

            var typeParameters = type.GetTypeParametersList(substitution, seenElements);

            string myName;
            var    parent     = type.GetContainingType();
            var    myFullName = clrTypeName.FullName;

            if (parent != null)
            {
                var parentName = parent.GetName <ITypeName>(substitution);
                // including the generic `N ticks
                var parentFullName = parentName.FullName;
                // shortName does not include the generic `N ticks, so we have to find it in the fullname...
                var startOfShortName = myFullName.LastIndexOf("+", StringComparison.Ordinal) + 1;
                //  ... and ignore the leading part
                var fullShortName = myFullName.Substring(startOfShortName);
                myName = string.Format("{0}+{1}", parentFullName, fullShortName);
            }
            else
            {
                myName = myFullName;
            }

            return(string.Format(
                       "{0}{1}, {2}",
                       myName,
                       typeParameters,
                       moduleName));
        }
        private StaticDeclaredTypeWrapper MakeDeclaredType(ITypeElement typeElementHandle, ISubstitution substitutionHandle)
        {
            if (typeElementHandle is ITypeParameter)
                throw new ArgumentException("This method should never be called with a generic parameter as input.",
                    "typeElementHandle");

            ITypeElement declaringTypeElementHandle = typeElementHandle.GetContainingType();
            StaticDeclaredTypeWrapper type;
            if (declaringTypeElementHandle != null)
            {
                StaticDeclaredTypeWrapper declaringType = MakeDeclaredType(declaringTypeElementHandle,
                    substitutionHandle);
                type = new StaticDeclaredTypeWrapper(this, typeElementHandle, declaringType, declaringType.Substitution);
            }
            else
            {
                type = new StaticDeclaredTypeWrapper(this, typeElementHandle, null, StaticTypeSubstitution.Empty);
            }

#if ! RESHARPER_50_OR_NEWER
            var typeParameterHandles = new List<ITypeParameter>(typeElementHandle.AllTypeParameters);
#else
            var typeParameterHandles = new List<ITypeParameter>(typeElementHandle.GetAllTypeParameters());
#endif
#if RESHARPER_31 || RESHARPER_40 || RESHARPER_41
            if (substitutionHandle.IsIdempotent(typeParameterHandles))
#else
            if (substitutionHandle.IsIdempotentAll(typeParameterHandles))
#endif
            {
                return type;
            }

        ITypeInfo[] genericArguments = GenericCollectionUtils.ConvertAllToArray<ITypeParameter, ITypeInfo>(typeParameterHandles, delegate(ITypeParameter typeParameterHandle)
            {
                IType substitutedType = substitutionHandle.Apply(typeParameterHandle);
                if (substitutedType.IsUnknown)
                    return MakeGenericParameterType(typeParameterHandle);

                return MakeType(substitutedType);
            });
            return type.MakeGenericType(genericArguments);
        }
        private void BuildTypeGenericParameters(StaticDeclaredTypeWrapper ownerType, ITypeElement typeHandle, List<StaticGenericParameterWrapper> genericParameters)
        {
            ITypeElement declaringType = typeHandle.GetContainingType();
            if (declaringType != null)
                BuildTypeGenericParameters(ownerType, declaringType, genericParameters);

            foreach (ITypeParameter parameterHandle in typeHandle.TypeParameters)
                genericParameters.Add(StaticGenericParameterWrapper.CreateGenericTypeParameter(this, parameterHandle, ownerType));
        }
        private XunitTestClassElement GetOrCreateClassElement(ITypeElement testClass, IProject projectElement, ProjectModelElementEnvoy projectEnvoy)
        {
            ITypeElement containingType = testClass.GetContainingType();
            XunitTestClassElement parent = null;
            if (containingType != null)
                parent = GetParent(containingType);

            return factory.GetOrCreateClassElement(testClass.GetClrName(), projectElement, projectEnvoy, parent);
        }
        private static void CheckMethodGroup(
            [NotNull] IReferenceExpression methodReference,
            [NotNull] IMethod method, [NotNull] IHighlightingConsumer consumer)
        {
            string message    = null;
            var    methodType = method.GetContainingType();

            if (methodType is IInterface)
            {
                message = string.Format("from interface '{0}' method", methodType.ShortName);
                consumer.AddHighlighting(
                    new SlowDelegateCreationHighlighting(methodReference, message),
                    methodReference.GetExpressionRange());
                return;
            }

            // there is not lags if method is instance method
            if (!method.IsStatic &&
                methodReference.QualifierExpression != null &&
                methodReference.QualifierExpression.IsClassifiedAsVariable)
            {
                return;
            }

            var substitution   = methodReference.Reference.Resolve().Result.Substitution;
            var typeParameters = new JetHashSet <ITypeParameter>();

            // collect all the type parameters from the method reference
            if (!substitution.IsEmpty())
            {
                foreach (var typeParameter in substitution.Domain)
                {
                    var substitutionType = substitution.Apply(typeParameter);
                    var targs            = TypeParametersCollectingVisitor.Collect(substitutionType);
                    typeParameters.UnionWith(targs);
                }
            }

            // get the delegate creation owner type, if member is not static
            var delegateCreationMember = methodReference.GetContainingTypeMemberDeclaration();

            if (delegateCreationMember == null || delegateCreationMember.DeclaredElement == null)
            {
                return;
            }

            ITypeElement delegateCreationOwnerType = null;

            if (!delegateCreationMember.DeclaredElement.IsStatic)
            {
                delegateCreationOwnerType = delegateCreationMember.DeclaredElement.GetContainingType();
            }

            // look for implicit qualification with the type parameters
            ITypeElement lastType = null;

            for (var qualifier = methodReference; qualifier != null;
                 qualifier = qualifier.QualifierExpression as IReferenceExpression)
            {
                if (qualifier.IsClassifiedAsVariable)
                {
                    lastType = null;
                    break;
                }

                var resolveResult = qualifier.Reference.Resolve();
                lastType = resolveResult.DeclaredElement as ITypeElement;
            }

            if (lastType != null)
            {
                for (var hidden = lastType.GetContainingType(); hidden != null; hidden = hidden.GetContainingType())
                {
                    if (hidden.TypeParameters.Count > 0)
                    {
                        foreach (var typeParameter in hidden.TypeParameters)
                        {
                            typeParameters.Add(typeParameter);
                        }
                    }
                }
            }

            foreach (var parameter in typeParameters)
            {
                if (parameter.IsValueType)
                {
                    continue;
                }

                if (delegateCreationOwnerType != null &&
                    delegateCreationOwnerType.TypeParameters.Contains(parameter))
                {
                    continue;
                }

                if (message == null)
                {
                    message = "method group parametrized with type parameter ";
                }

                if (parameter.OwnerType != null)
                {
                    message += string.Format("'{0}' of type '{1}'", parameter.ShortName, parameter.OwnerType.ShortName);
                }
                else if (parameter.OwnerMethod != null)
                {
                    message += string.Format("'{0}' of method '{1}'", parameter.ShortName, parameter.OwnerMethod.ShortName);
                }
            }

            if (message != null)
            {
                consumer.AddHighlighting(
                    new SlowDelegateCreationHighlighting(methodReference, message),
                    methodReference.GetExpressionRange());
            }
        }