예제 #1
0
        public void ProcessMembers(CSharpCodeCompletionContext context, GroupedItemsCollector collector, ISymbolTable symbols)
        {
            symbols.ForAllSymbolInfos(symbol =>
            {
                var member = symbol.GetDeclaredElement() as ITypeMember;
                if (member != null && member.GetElementType() == ExpectedMemberType)
                {
                    string nameArgument         = string.Format("\"{0}\"", symbol.ShortName);
                    var declaredElementInstance = new DeclaredElementInstance <ITypeMember>((ITypeMember)symbol.GetDeclaredElement(),
                                                                                            symbol.GetSubstitution());

                    if (!IncludeSymbol(declaredElementInstance))
                    {
                        return;
                    }


                    IList <string> arguments = new List <string>();

                    if (IncludeNameArgument)
                    {
                        arguments.Add(nameArgument);
                    }

                    if (NeedsBindingFlags(member))
                    {
                        arguments.Add(GetExpectedBindingFlags(member).GetFullString());
                    }

                    if (ShouldProvideMemberSpecificArguments(symbols.GetSymbolInfos(member.ShortName))) //additional arguments needs to be provided
                    {
                        ProvideMemberSpecificArguments(declaredElementInstance, arguments, NeedsBindingFlags(member));
                    }

                    ReflectionMemberLookupItem lookupItem;
                    if (member is IMethod && ((IMethod)member).TypeParameters.Count != 0)
                    {
                        lookupItem = new ReflectionGenericMethodLookupItem(symbol.ShortName,
                                                                           declaredElementInstance,
                                                                           context,
                                                                           context.BasicContext.LookupItemsOwner);
                    }
                    else
                    {
                        lookupItem = new ReflectionMemberLookupItem(symbol.ShortName,
                                                                    string.Join(", ", arguments.ToArray()),
                                                                    declaredElementInstance,
                                                                    context,
                                                                    context.BasicContext.LookupItemsOwner);
                    }



                    lookupItem.InitializeRanges(context.CompletionRanges, context.BasicContext);
                    lookupItem.OrderingString = string.Format("__A_MEMBER_{0}", symbol.ShortName); //
                    collector.AddToTop(lookupItem);
                }
            });
        }
        protected override bool AddLookupItems(CSharpCodeCompletionContext context, GroupedItemsCollector collector)
        {
            bool         moqIsSeen = false;
            var          candidateExistingElements = new List <ISymbolInfo>();
            ISymbolTable table = GetSymbolTable(context);

            if (table != null)
            {
                table.ForAllSymbolInfos(info =>
                {
                    IDeclaredElement declaredElement = info.GetDeclaredElement();
                    if (declaredElement.ConvertToString() == "Class:Moq.Mock")
                    {
                        moqIsSeen = true;
                    }
                    IType type = declaredElement.Type();
                    if (type != null)
                    {
                        if (type.GetClassType().ConvertToString() == "Class:Moq.Mock`1")
                        {
                            IType typeParameter = TypesUtil.GetTypeArgumentValue(type, 0);
                            if (typeParameter != null && context.ExpectedTypesContext != null && context.ExpectedTypesContext.ExpectedITypes != null && context.ExpectedTypesContext.ExpectedITypes.Select(x => x.Type).Where(x => x != null).Any(x => typeParameter.IsExplicitlyConvertibleTo(x, ClrPredefinedTypeConversionRule.INSTANCE)))
                            {
                                candidateExistingElements.Add(info);
                            }
                        }
                    }
                });
            }
            foreach (ISymbolInfo candidateExistingElement in candidateExistingElements)
            {
#if RESHARPER8
                collector.AddToTop(context.LookupItemsFactory.CreateTextLookupItem(candidateExistingElement.ShortName + ".Object"));
#endif
#if RESHARPER9
                var lookupItem = new TextLookupItem(candidateExistingElement.ShortName + ".Object");
                lookupItem.InitializeRanges(context.CompletionRanges, context.BasicContext);
                lookupItem.PlaceTop();
                collector.Add(lookupItem);
#endif
            }
            if (moqIsSeen && !candidateExistingElements.Any() && context.ExpectedTypesContext != null)
            {
                foreach (ExpectedTypeCompletionContextBase.ExpectedIType expectedType in context.ExpectedTypesContext.ExpectedITypes)
                {
                    if (expectedType.Type == null)
                    {
                        continue;
                    }
                    if (expectedType.Type.IsInterfaceType())
                    {
                        string typeName = expectedType.Type.GetPresentableName(CSharpLanguage.Instance);
#if RESHARPER8
                        if (candidateExistingElements.Any())
                        {
                            collector.AddToBottom(context.LookupItemsFactory.CreateTextLookupItem("new Mock<" + typeName + ">().Object"));
                        }
                        else
                        {
                            collector.AddToTop(context.LookupItemsFactory.CreateTextLookupItem("new Mock<" + typeName + ">().Object"));
                        }
#endif
#if RESHARPER9
                        var lookupItem = new TextLookupItem("new Mock<" + typeName + ">().Object");
                        lookupItem.InitializeRanges(context.CompletionRanges, context.BasicContext);
                        if (candidateExistingElements.Any())
                        {
                            lookupItem.PlaceBottom();
                        }
                        else
                        {
                            lookupItem.PlaceTop();
                        }
                        collector.Add(lookupItem);
#endif
                    }
                }
            }
            return(true);
        }
        protected override bool AddLookupItems(CSharpCodeCompletionContext context, GroupedItemsCollector collector)
        {
            bool         nSubstituteIsSeen = false;
            ISymbolTable table             = GetSymbolTable(context);

            if (table != null)
            {
                table.ForAllSymbolInfos(info =>
                {
                    IDeclaredElement declaredElement = info.GetDeclaredElement();
                    if (declaredElement.ConvertToString() == "Class:NSubstitute.Substitute")
                    {
                        nSubstituteIsSeen = true;
                    }
                });
            }
            if (!nSubstituteIsSeen)
            {
                return(true);
            }
            if (context.ExpectedTypesContext != null)
            {
                foreach (ExpectedTypeCompletionContextBase.ExpectedIType expectedType in context.ExpectedTypesContext.ExpectedITypes)
                {
                    if (expectedType.Type == null)
                    {
                        continue;
                    }
                    var typeName = expectedType.Type.GetPresentableName(CSharpLanguage.Instance);
#if RESHARPER9
                    var textLookupItem = new TextLookupItem("Arg.Any<" + typeName + ">()");
                    textLookupItem.InitializeRanges(context.CompletionRanges, context.BasicContext);
                    textLookupItem.PlaceTop();
                    collector.Add(textLookupItem);
#endif
                }
            }
            if (context.TerminatedContext == null)
            {
                return(true);
            }
            var identifier           = context.TerminatedContext.TreeNode as IIdentifier;
            var mockedMethodArgument = identifier
                                       .GetParentSafe <IReferenceExpression>()
                                       .GetParentSafe <ICSharpArgument>();
            if (mockedMethodArgument == null)
            {
                return(true);
            }
            var mockedMethodInvocationExpression = mockedMethodArgument
                                                   .GetParentSafe <IArgumentList>()
                                                   .GetParentSafe <IInvocationExpression>();
            if (mockedMethodInvocationExpression == null)
            {
                return(true);
            }
            if (mockedMethodInvocationExpression.Reference != null)
            {
                var mockedMethodResolved = mockedMethodInvocationExpression.Reference.Resolve();
                var declaredElements     = Enumerable.Repeat(mockedMethodResolved.DeclaredElement, 1)
                                           .Concat(mockedMethodResolved.Result.Candidates)
                                           .Where(x => x != null);
                var methods = declaredElements
                              .OfType <IMethod>()
                              .Where(x => x.Parameters.Count() > 1)
                              .ToList();
                methods.ForEach(method =>
                {
                    var parameter = method.Parameters.Select(x => "Arg.Any<" + x.Type.GetPresentableName(CSharpLanguage.Instance) + ">()");
#if RESHARPER8
                    collector.AddToTop(context.LookupItemsFactory.CreateTextLookupItem(string.Join(", ", parameter)));
#endif
#if RESHARPER9
                    var textLookupItem = new TextLookupItem(string.Join(", ", parameter));
                    textLookupItem.InitializeRanges(context.CompletionRanges, context.BasicContext);
                    textLookupItem.PlaceTop();
                    collector.Add(textLookupItem);
#endif
                });
            }
            return(true);
        }