Exemplo n.º 1
0
        public static bool IsContainerCall(this ITreeNode node, string containerClrTypeName)
        {
            var invocationExpression = node as IInvocationExpression;

            if (invocationExpression == null)
            {
                return(false);
            }

            var resolve = invocationExpression.InvocationExpressionReference.Resolve().Result;
            var method  = resolve.DeclaredElement as IMethod;

            if (method == null)
            {
                return(false);
            }
            ITypeElement containingType = method.GetContainingType();

            if (containingType == null)
            {
                return(false);
            }

            IDeclaredType containerClrType = CreateTypeByClrName(node, containerClrTypeName);

            return(containingType.IsDescendantOf(containerClrType.GetTypeElement()));
        }
Exemplo n.º 2
0
            public override void VisitDeclaredType(IDeclaredType declaredType)
            {
                if (!myTypes.Add(declaredType))
                {
                    return;
                }

                var typeElement = declaredType.GetTypeElement();

                if (typeElement == null)
                {
                    return;
                }

                var substitution   = declaredType.GetSubstitution();
                var typeParameters = typeElement.TypeParameters;

                if (typeParameters.Count == 0)
                {
                    return;
                }

                foreach (var typeParameter in typeParameters)
                {
                    substitution[typeParameter].Accept(this);
                }
            }
Exemplo n.º 3
0
        private bool IsNinjectBindCall(ITreeNode element)
        {
            var invocationExpression = element as IInvocationExpression;

            if (invocationExpression == null)
            {
                return(false);
            }

            var resolve = invocationExpression.InvocationExpressionReference.Resolve().Result;
            var method  = resolve.DeclaredElement as IMethod;

            if (method == null)
            {
                return(false);
            }
            ITypeElement containingType = method.GetContainingType();

            if (containingType == null)
            {
                return(false);
            }

            if (bindingRootType == null)
            {
                bindingRootType = TypeFactory.CreateTypeByCLRName("Ninject.Syntax.IBindingRoot", element.GetPsiModule());
            }

            return(containingType.IsDescendantOf(bindingRootType.GetTypeElement()));
        }
Exemplo n.º 4
0
        private bool ownsTypeMatch(IDeclaration declaration)
        {
            if (string.IsNullOrEmpty(_isOfType))
            {
                return(true);
            }

            if (_isOfTypeType == null)
            {
                return(false);
            }

            if (declaration is ITypeOwner)
            {
                IDeclaredType declaredType = ((ITypeOwner)declaration).Type as IDeclaredType;
                if (declaredType == null)
                {
                    return(false);
                }
                ITypeElement typeElement = declaredType.GetTypeElement();
                if (typeElement == null)
                {
                    return(false);
                }
                return(typeElement.IsDescendantOf(_isOfTypeType));
            }
            else
            {
                return(false);
            }
        }
        private static bool AddEnumerationMembers([NotNull] CSharpCodeCompletionContext context,
                                                  [NotNull] GroupedItemsCollector collector,
                                                  [NotNull] IDeclaredType qualifierType,
                                                  [NotNull] IReferenceExpression referenceExpression)
        {
            var enumerationType = (IEnum)qualifierType.GetTypeElement().NotNull();
            var substitution    = qualifierType.GetSubstitution();
            var memberValues    = new List <Pair <IField, string> >();

            var isFlagsEnum = enumerationType.HasAttributeInstance(FlagsAttributeClrName, false);

            if (!isFlagsEnum)
            {
                foreach (var member in enumerationType.EnumMembers)
                {
                    var formattable = member.ConstantValue.Value as IFormattable;
                    var memberValue = (formattable != null)
            ? formattable.ToString("D", CultureInfo.InvariantCulture) : string.Empty;
                    memberValues.Add(Pair.Of(member, memberValue));
                }
            }
            else
            {
                foreach (var member in enumerationType.EnumMembers)
                {
                    var convertible = member.ConstantValue.Value as IConvertible;
                    var memberValue = (convertible != null)
            ? GetBinaryRepresentation(convertible) : string.Empty;
                    memberValues.Add(Pair.Of(member, memberValue));
                }
            }

            if (memberValues.Count == 0)
            {
                return(false);
            }

            // create pointer to . in reference expression
            var maxLength        = memberValues.Max(x => x.Second.Length);
            var reparsedDotRange = referenceExpression.Delimiter.GetTreeTextRange();
            var originalDotRange = context.UnterminatedContext.ToOriginalTreeRange(reparsedDotRange);
            var file             = context.BasicContext.File;
            var dotMarker        = file.GetDocumentRange(originalDotRange).CreateRangeMarker();

            foreach (var member in memberValues)
            {
                var normalizedValue = member.Second.PadLeft(maxLength, '0');
                var value           = isFlagsEnum ? normalizedValue : member.Second;

                var instance       = new DeclaredElementInstance <IField>(member.First, substitution);
                var textLookupItem = new EnumMemberLookupItem(dotMarker, instance, normalizedValue, value, isFlagsEnum);

                collector.AddSomewhere(textLookupItem);
            }

            return(true);
        }
        protected override void Run(IClassDeclaration classDeclaration, ElementProblemAnalyzerData data, IHighlightingConsumer consumer)
        {
            if (!myIsEnabled.Value)
            {
                return;
            }

            var typeElement = classDeclaration.DeclaredElement;

            if (typeElement == null || !ZoneConstants.IsZoneClass(typeElement))
            {
                return;
            }

            var mark    = myMarks.GetMark(typeElement.GetContainingNamespace());
            var classes = myMarks.EnumerateClasses(mark).ToList();

            // find all base zones
            var baseZones = new HashSet <IClass>();

            foreach (var cls in classes)
            {
                foreach (var baseZone in MarksService.EnumerableMarkClasses(cls, true))
                {
                    if (!baseZone.Equals(cls))
                    {
                        baseZones.Add(baseZone);
                    }
                }
            }

            foreach (var typeUsage in classDeclaration.SuperTypeUsageNodes.OfType <IUserDeclaredTypeUsage>())
            {
                IDeclaredType superType        = CSharpTypeFactory.CreateDeclaredType(typeUsage);
                var           superTypeElement = superType.GetTypeElement();
                if (superTypeElement != null)
                {
                    if (ZoneConstants.IsIRequire(superTypeElement as IInterface))
                    {
                        var substitution = superType.GetSubstitution();
                        foreach (var parameter in substitution.Domain)
                        {
                            var zoneType = substitution[parameter] as IDeclaredType;
                            if (zoneType != null)
                            {
                                var zoneClass = zoneType.GetTypeElement() as IClass;
                                if (zoneClass != null && baseZones.Contains(zoneClass))
                                {
                                    consumer.AddHighlighting(new RedundantDependencySpecificationError(typeUsage), classDeclaration.GetContainingNode <IFile>());
                                }
                            }
                        }
                    }
                }
            }
        }
        static bool IsValidFieldOfType(this IDeclaredElement element, string typeFullName)
        {
            IDeclaredType fieldType = element.GetValidatedFieldType();

            if (fieldType == null)
            {
                return(false);
            }

            return(fieldType.GetTypeElement().HasAttributeInstance(new ClrTypeName(typeFullName), false));
        }
Exemplo n.º 8
0
        private static ITypeName GetName(this IDeclaredType type,
                                         IDictionary <DeclaredElementInstance, IName> seenElements)
        {
            var typeElement = type.GetTypeElement();

            // typeElement can be null, for example when resolving the second
            // parameter type in the incomplete method declaration
            // > public void M(int i, )
            return(typeElement == null
                ? Names.UnknownType
                : (ITypeName)typeElement.GetName(type.GetSubstitution(), seenElements));
        }
Exemplo n.º 9
0
 public bool IsUnityImplicitType([NotNull] ITypeElement typeElement, [NotNull] IPsiModule module)
 {
     foreach (KeyValuePair <string, string> pair in m_unitySettings.UnityClasses.EnumIndexedValues())
     {
         IDeclaredType typeByClrName = TypeFactory.CreateTypeByCLRName(pair.Value, module);
         ITypeElement  unityClass    = typeByClrName.GetTypeElement();
         if (typeElement.IsDescendantOf(unityClass))
         {
             return(true);
         }
     }
     return(false);
 }
        private bool IsSimple(IType type)
        {
            // todo fix for enum
            bool          isEnum       = false;
            IDeclaredType declaredType = type.GetScalarType();

            if (declaredType != null)
            {
                isEnum = declaredType.GetTypeElement() is IEnum;
            }

            return(type.IsSimplePredefined() || isEnum);
        }
Exemplo n.º 11
0
        public override bool IsAvailable(IUserDataHolder cache)
        {
            using (ReadLockCookie.Create())
            {
                if (Provider.SelectedElement != null)
                {
                    _superType = TypeHelper.CreateTypeByCLRName(SuperTypeName, Provider.PsiModule, Provider.SelectedElement.GetResolveContext());
                    if (_superType.GetTypeElement() != null)
                    {
                        _classDeclaration = Provider.SelectedElement.Parent as IClassDeclaration;
                    }
                }
            }

            // !_classDeclaration.IsStatic doesn't work, IsStatic is returns true
            return _classDeclaration != null && !_classDeclaration.IsStaticEx() && _classDeclaration.SuperTypes.IsEmpty();
        }
Exemplo n.º 12
0
        private static IEnumerable<IComponentRegistration> CreateRegistration(IInvocationExpression invocationExpression, IDeclaredType first, IDeclaredType last)
        {
            if (first == null || last == null)
            {
                yield break;
            }

            ITypeElement fromType = first.GetTypeElement();
            ITypeElement toType = last.GetTypeElement();

            if (fromType != null && toType != null)
            {
                yield return fromType.Equals(toType)
                                 ? new ComponentRegistration(invocationExpression, fromType)
                                 : new ComponentRegistration(invocationExpression, fromType, toType);
            }
        }
Exemplo n.º 13
0
        private void AppendDeclaredType([NotNull] IDeclaredType declaredType, QualifierDisplays expectedQualifierDisplay, Context context)
        {
            if (declaredType.IsNullable())
            {
                IType underlyingType = declaredType.GetNullableUnderlyingType();
                if (underlyingType != null)
                {
                    AppendTypeWithoutModule(underlyingType, expectedQualifierDisplay, context);
                    AppendText("?", _highlighterIdProvider.Operator);
                    return;
                }
            }

            if (declaredType is IDynamicType)
            {
                AppendText("dynamic", _highlighterIdProvider.Keyword);
                return;
            }

            if (context.Options.UseTypeKeywords)
            {
                string typeKeyword = CSharpTypeFactory.GetTypeKeyword(declaredType.GetClrName());
                if (typeKeyword != null)
                {
                    AppendText(typeKeyword, _highlighterIdProvider.Keyword);
                    return;
                }
            }
            else if (declaredType.IsVoid())
            {
                AppendText("void", _highlighterIdProvider.Keyword);
                return;
            }

            ITypeElement typeElement = declaredType.GetTypeElement();

            if (typeElement == null || !typeElement.IsValid())
            {
                PsiLanguageType language = CSharpLanguage.Instance ?? (PsiLanguageType)UnknownLanguage.Instance;
                AppendText(declaredType.GetPresentableName(language), null);
            }
            else
            {
                AppendTypeElement(typeElement, declaredType.GetSubstitution(), expectedQualifierDisplay, context);
            }
        }
Exemplo n.º 14
0
        private static IEnumerable <IComponentRegistration> CreateRegistration(IInvocationExpression invocationExpression, IDeclaredType first, IDeclaredType last)
        {
            if (first == null || last == null)
            {
                yield break;
            }

            ITypeElement fromType = first.GetTypeElement();
            ITypeElement toType   = last.GetTypeElement();

            if (fromType != null && toType != null)
            {
                yield return(fromType.Equals(toType)
                                 ? new ComponentRegistration(invocationExpression, fromType)
                                 : new ComponentRegistration(invocationExpression, fromType, toType));
            }
        }
Exemplo n.º 15
0
        private void AppendDeclaredType([NotNull] IDeclaredType declaredType, NamespaceDisplays expectedNamespaceDisplay)
        {
            if (declaredType.IsNullable())
            {
                IType underlyingType = declaredType.GetNullableUnderlyingType();
                if (underlyingType != null)
                {
                    AppendType(underlyingType, expectedNamespaceDisplay);
                    AppendText("?", VsHighlightingAttributeIds.Operator);
                    return;
                }
            }

            if (declaredType is IDynamicType)
            {
                AppendText("dynamic", VsHighlightingAttributeIds.Keyword);
                return;
            }

            if (_options.UseTypeKeywords)
            {
                string typeKeyword = CSharpTypeFactory.GetTypeKeyword(declaredType.GetClrName());
                if (typeKeyword != null)
                {
                    AppendText(typeKeyword, VsHighlightingAttributeIds.Keyword);
                    return;
                }
            }
            else if (declaredType.IsVoid())
            {
                AppendText("void", VsHighlightingAttributeIds.Keyword);
                return;
            }

            ITypeElement typeElement = declaredType.GetTypeElement();

            if (typeElement == null || !typeElement.IsValid())
            {
                AppendText(declaredType.GetPresentableName(UnknownLanguage.Instance), null);
                return;
            }

            AppendTypeElement(typeElement, declaredType.GetSubstitution(), expectedNamespaceDisplay);
        }
        public IModule GetTargetModule <T>(T element)
        {
            var type = element as IType;

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

            IDeclaredType declaredType = type.GetScalarType();

            if (declaredType != null)
            {
                ITypeElement typeElement = declaredType.GetTypeElement();
                if (typeElement != null)
                {
                    return(typeElement.Module.ContainingProjectModule);
                }
            }

            return(null);
        }
Exemplo n.º 17
0
        public override IEnumerable <IComponentRegistration> GetComponentRegistrations(ITreeNode registrationRootElement)
        {
            IStructuralMatchResult match = Match(registrationRootElement);

            if (match.Matched)
            {
                var invocationExpression = match.GetMatchedElement("bind") as IInvocationExpression;
                if (invocationExpression == null)
                {
                    yield break;
                }

                IDeclaredType declaredType = GetDeclaredType(invocationExpression);
                if (declaredType != null)
                {
                    ITypeElement typeElement = declaredType.GetTypeElement();
                    if (typeElement != null)
                    {
                        yield return(new ComponentRegistration(registrationRootElement, typeElement));
                    }
                }
            }
        }
Exemplo n.º 18
0
        private StaticDeclaredTypeWrapper MakeDeclaredType(IDeclaredType typeHandle)
        {
            return declaredTypeMemoizer.Memoize(typeHandle, () =>
            {
                ITypeElement typeElement = typeHandle.GetTypeElement();
                if (typeElement == null)
                    throw new ReflectionResolveException(
                        String.Format(
                            "Cannot obtain type element for type '{0}' possibly because its source code is not available.",
                            typeHandle.GetCLRName()));

                return MakeDeclaredType(typeElement, typeHandle.GetSubstitution());
            });
        }
Exemplo n.º 19
0
        private StaticTypeWrapper MakeType(IDeclaredType typeHandle)
        {
            ITypeParameter typeParameterHandle = typeHandle.GetTypeElement() as ITypeParameter;
            if (typeParameterHandle != null)
                return MakeGenericParameterType(typeParameterHandle);

            return MakeDeclaredType(typeHandle);
        }
        private void ProcessSuperType(XunitTestClassElement classElement, IDeclaredType type)
        {
            var @class = type.GetTypeElement() as IClass;
            if (@class == null)
                return;

            foreach (IMethod method in @class.GetMembers().Where(UnitTestElementPsiIdentifier.IsUnitTest))
                GetOrCreateMethodElement(classElement, @class, method);
        }
Exemplo n.º 21
0
 public static void Initialize(CSharpGeneratorContext context)
 {
     _listDeclaredType = TypeFactory.CreateTypeByCLRName(typeof(List <>).FullName, context.PsiModule,
                                                         context.Anchor.GetResolveContext());
     _listTypeElement = _listDeclaredType.GetTypeElement();
 }
Exemplo n.º 22
0
        private static bool AddEnumerationMembers(
            [NotNull] CSharpCodeCompletionContext context, [NotNull] IDeclaredType enumerationType, [NotNull] GroupedItemsCollector collector)
        {
            var enumTypeElement = enumerationType.GetTypeElement() as IEnum;

            if (enumTypeElement == null)
            {
                return(false);
            }

            var enumSubstitution      = enumerationType.GetSubstitution();
            var enumMembersWithValues = new List <Pair <DeclaredElementInstance, string> >(); // todo: [R#] List<T1, T2>

            var isFlagsEnum = enumTypeElement.HasAttributeInstance(PredefinedType.FLAGS_ATTRIBUTE_CLASS, false);

            foreach (var enumMember in enumTypeElement.EnumMembers)
            {
                var enumValue = enumMember.ConstantValue.Value;
                var enumCase  = new DeclaredElementInstance(enumMember, enumSubstitution);

                if (isFlagsEnum)
                {
                    var convertible = enumValue as IConvertible;
                    if (convertible != null)
                    {
                        enumMembersWithValues.Add(Pair.Of(enumCase, GetBinaryRepresentation(convertible)));
                    }
                }
                else
                {
                    var formattable = enumValue as IFormattable;
                    if (formattable != null)
                    {
                        enumMembersWithValues.Add(Pair.Of(enumCase, formattable.ToString("D", CultureInfo.InvariantCulture)));
                    }
                }
            }

            if (enumMembersWithValues.Count == 0)
            {
                return(false);
            }

            var maxLength = enumMembersWithValues.Max(x => x.Second.Length);
            var index     = 0;

            foreach (var enumMember in enumMembersWithValues)
            {
                var elementInstance = enumMember.First;
                var elementValue    = isFlagsEnum ? enumMember.Second.PadLeft(maxLength, '0') : enumMember.Second;

                var info = new EnumCaseInfo(elementInstance, context, isFlagsEnum, elementValue);
                info.Placement.OrderString = "___ENUM_MEMBER_" + index.ToString("D16");
                info.Ranges = context.CompletionRanges;

                index++;

                var lookupItem = LookupItemFactory.CreateLookupItem(info)
                                 .WithPresentation(item =>
                {
                    var presentation = new DeclaredElementPresentation <CSharpDeclaredElementInfo>(item.Info, PresenterStyles.DefaultPresenterStyle);

                    var caseValue = item.Info.CaseValue;
                    if (caseValue.Length <= 32)
                    {
                        presentation.DisplayTypeName = new RichText("= " + caseValue, EnumValueStyle);
                    }

                    return(presentation);
                })
                                 .WithMatcher(item => new DeclaredElementMatcher(item.Info, IdentifierMatchingStyle.Default))
                                 .WithBehavior(item => new EnumCaseCheckBehavior(item.Info));

                collector.Add(lookupItem);
            }

            return(true);
        }
 private static ITypeName IfTypeIsPredefined(ITypeElement typeElem, IDeclaredType targetElemen, string id)
 {
     return(Equals(typeElem, targetElemen.GetTypeElement()) ? Names.Type(id) : null);
 }