Exemplo n.º 1
0
        protected override bool AddLookupItems(T4CodeCompletionContext context, IItemsCollector collector)
        {
            ITreeNode node = context.BasicContext.File.FindNodeAt(context.BasicContext.SelectedTreeRange);

            Assertion.AssertNotNull(node, "node == null");
            var ranges = context.BasicContext.GetRanges(node);

            collector.AddRanges(ranges);

            var attribute = node.GetContainingNode <IT4DirectiveAttribute>();

            Assertion.AssertNotNull(attribute, "attribute != null");

            var directive = attribute.GetContainingNode <IT4Directive>();

            Assertion.AssertNotNull(directive, "directive != null");

            DirectiveInfo          directiveInfo = _directiveInfoManager.GetDirectiveByName(directive.GetName());
            DirectiveAttributeInfo attributeInfo = directiveInfo?.GetAttributeByName(attribute.GetName());

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

            foreach (string intellisenseValue in attributeInfo.IntelliSenseValues)
            {
                var item = new TextLookupItem(intellisenseValue);
                item.InitializeRanges(ranges, context.BasicContext);
                collector.Add(item);
            }

            return(true);
        }
        private static void ProcessReferenceName(CSharpCodeCompletionContext context, GroupedItemsCollector collector, IReferenceName referenceName, NamedElementKinds elementKinds, ScopeKind localSelfScoped)
        {
            if (referenceName == null)
            {
                return;
            }
            var referenceNameResolveResult = referenceName.Reference.Resolve();
            var referencedElementAsString  = referenceNameResolveResult.DeclaredElement.ConvertToString();

            if (referencedElementAsString == "Class:Moq.Mock`1")
            {
                var typeArgumentList = referenceName.TypeArgumentList;
                var typeArguments    = typeArgumentList.TypeArguments;
                if (typeArguments.Count == 1)
                {
                    var typeArgument = typeArguments[0];
                    var scalarType   = typeArgument.GetScalarType();
                    if (scalarType == null)
                    {
                        return;
                    }
                    var    genericTypeResolveResult = scalarType.Resolve();
                    var    namingManager            = typeArgument.GetPsiServices().Naming;
                    var    suggestionOptions        = new SuggestionOptions();
                    string proposedName;
                    if (genericTypeResolveResult.IsEmpty)
                    {
                        proposedName = namingManager.Suggestion.GetDerivedName(typeArgument.GetPresentableName(CSharpLanguage.Instance), elementKinds, localSelfScoped, CSharpLanguage.Instance, suggestionOptions, referenceName.GetSourceFile());
                    }
                    else
                    {
                        proposedName = namingManager.Suggestion.GetDerivedName(genericTypeResolveResult.DeclaredElement, elementKinds, localSelfScoped, CSharpLanguage.Instance, suggestionOptions, referenceName.GetSourceFile());
                    }
#if RESHARPER8
                    collector.AddToTop(context.LookupItemsFactory.CreateTextLookupItem(proposedName));
                    collector.AddToTop(context.LookupItemsFactory.CreateTextLookupItem(proposedName + "Mock"));
#endif
#if RESHARPER9
                    var textLookupItem = new TextLookupItem(proposedName);
                    textLookupItem.InitializeRanges(context.CompletionRanges, context.BasicContext);
                    textLookupItem.PlaceTop();
                    collector.Add(textLookupItem);
#endif

#if RESHARPER9
                    var textLookupItem2 = new TextLookupItem(proposedName + "Mock");
                    textLookupItem2.InitializeRanges(context.CompletionRanges, context.BasicContext);
                    textLookupItem2.PlaceTop();
                    collector.Add(textLookupItem2);
#endif
                }
            }
        }
Exemplo n.º 3
0
        protected override bool AddLookupItems(T4CodeCompletionContext context, IItemsCollector collector)
        {
            ITreeNode node = context.BasicContext.File.FindNodeAt(context.BasicContext.SelectedTreeRange);

            Assertion.AssertNotNull(node, "node == null");
            var ranges = context.BasicContext.GetRanges(node);

            foreach (string directiveName in T4DirectiveInfoManager.AllDirectives.Select(di => di.Name))
            {
                var item = new TextLookupItem(directiveName, T4Entity.Id);
                item.InitializeRanges(ranges, context.BasicContext);
                collector.Add(item);
            }

            return(true);
        }
Exemplo n.º 4
0
        public HotspotItems GetLookupItems(IHotspotContext context, IList <string> arguments)
        {
            var method = TextControlToPsi.GetContainingTypeOrTypeMember(context.SessionContext.Solution, context.SessionContext.TextControl);

            if (method is IMethod)
            {
                var lookupItems = new List <ILookupItem>();

                var item = new TextLookupItem(((IMethod)method).ReturnType.GetPresentableName(method.PresentationLanguage));

                lookupItems.Add(item);

                var hotSpotItems = new HotspotItems(lookupItems);

                return(hotSpotItems);
            }
            return(null);
        }
Exemplo n.º 5
0
        public HotspotItems GetLookupItems(IHotspotContext context, IList<string> arguments)
        {
            var method = TextControlToPsi.GetContainingTypeOrTypeMember(context.SessionContext.Solution, context.SessionContext.TextControl);

            if (method is IMethod)
            {
                var lookupItems = new List<ILookupItem>();

                var item = new TextLookupItem(((IMethod)method).ReturnType.GetPresentableName(method.PresentationLanguage));
                
                lookupItems.Add(item);
                
                var hotSpotItems = new HotspotItems(lookupItems);
                
                return hotSpotItems;
            }
            return null;
        }
        public void Apply(BaseCodeCompletionContext context, IList<ILookupItem> result)
        {
            IDeclaredParameter declaredParameter = GetDeclaredParameter(context);
            Assert.CheckNotNull(declaredParameter);

            IParameterDescriptor descriptor =
                declaredParameter.ParameterDescriptorProvider.GetParameterDescriptor(declaredParameter.Name);

            LookupItemBase item = null;
            if (!descriptor.RequredType)
                item = new TextLookupItem("value");
            else
                item = new TextLookupItem("type");

            item.InsertRange = new TextRange(0);
            item.ReplaceRange = new TextRange(0, context.GetPrefix().Length);

            result.Add(item);
        }
        public void Apply(CodeCompletionContext context, IList<ILookupItem> result)
        {
            IXmlTag tag = context.GetAttributeTag();
            Assert.CheckNotNull(tag);
            IXmlAttribute attribute = context.Token.GetContainingElement<IXmlAttribute>(false);
            Assert.CheckNotNull(attribute);

            IParameterDescriptor descriptor = null;
            IDeclaredParameter declaredParameter = tag as IDeclaredParameter;
            if (declaredParameter != null && attribute.AttributeName == L4NConstants.VALUE)
            {
                descriptor = declaredParameter.ParameterDescriptorProvider.GetParameterDescriptor(declaredParameter.Name);
            }
            else
            {
                IParameterDescriptorProvider parameterDescriptorProvider = tag as IParameterDescriptorProvider;
                if (parameterDescriptorProvider == null)
                    return;
                descriptor = parameterDescriptorProvider.GetParameterDescriptor(attribute.AttributeName);
            }

            if(descriptor == null)
                return;

            if(!descriptor.IsEnumerable)
                return;

            string[] values = descriptor.PossibleValues;
            if(values == null)
                return;

            foreach (string v in values)
            {
                TextLookupItem item = new TextLookupItem(v);
                item.InsertRange = new TextRange(0);
                item.ReplaceRange = new TextRange(0, context.GetPrefix().Length);
                result.Add(item);
            }
        }
Exemplo n.º 8
0
        protected override bool AddLookupItems(T4CodeCompletionContext context, IItemsCollector collector)
        {
            ITreeNode node = context.BasicContext.File.FindNodeAt(context.BasicContext.SelectedTreeRange);

            Assertion.AssertNotNull(node, "node == null");
            var ranges = context.BasicContext.GetRanges(node);

            collector.AddRanges(ranges);

            var directive = node.GetContainingNode <IT4Directive>();

            Assertion.AssertNotNull(directive, "directive != null");
            DirectiveInfo directiveInfo = _directiveInfoManager.GetDirectiveByName(directive.GetName());

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

            JetHashSet <string> existingNames = directive
                                                .GetAttributes()
                                                .Select(attr => attr.GetName())
                                                .ToJetHashSet(s => s, StringComparer.OrdinalIgnoreCase);

            foreach (string attributeName in directiveInfo.SupportedAttributes.Select(attr => attr.Name))
            {
                if (existingNames.Contains(attributeName))
                {
                    continue;
                }

                var item = new TextLookupItem(attributeName);
                item.InitializeRanges(ranges, context.BasicContext);
                collector.Add(item);
            }

            return(true);
        }
Exemplo n.º 9
0
        public override HotspotItems GetLookupItems(IHotspotContext context)
        {
            var document = context.ExpressionRange.Document;

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

            var method = TextControlToPsi.GetContainingTypeOrTypeMember(context.SessionContext.Solution, document, context.ExpressionRange.StartOffsetRange().TextRange.StartOffset) as IMethod;

            if (method != null)
            {
                var lookupItems          = new List <ILookupItem>();
                var methodReturnTypeName = method.ReturnType.GetPresentableName(method.PresentationLanguage);
                var item = new TextLookupItem(methodReturnTypeName);
                lookupItems.Add(item);
                var hotSpotItems = new HotspotItems(lookupItems);
                return(hotSpotItems);
            }

            return(null);
        }
Exemplo n.º 10
0
        /// <summary>
        /// The get lookup items.
        /// </summary>
        /// <param name="context">
        /// The context.
        /// </param>
        /// <param name="arguments">
        /// The arguments.
        /// </param>
        /// <returns>
        /// </returns>
        public HotspotItems GetLookupItems(IHotspotContext context, IList<string> arguments)
        {
            var solution = context.SessionContext.Solution;
              var textControl = context.SessionContext.TextControl;

              var element = TextControlToPsi.GetElementFromCaretPosition<IElement>(solution, textControl);

              var text = GetText(element);
              if (text == null)
              {
            return null;
              }

              var item = new TextLookupItem(text);

              var result = new HotspotItems(item);

              return result;
        }
        private void AddLookupItemsNew([NotNull] CSharpCodeCompletionContext context, [NotNull] GroupedItemsCollector collector)
        {
            if (context.TerminatedContext == null)
            {
                return;
            }
            var identifier           = context.TerminatedContext.TreeNode as IIdentifier;
            var mockedMethodArgument = identifier
                                       .GetParentSafe <IReferenceExpression>()
                                       .GetParentSafe <ICSharpArgument>();

            if (mockedMethodArgument == null)
            {
                return;
            }
            var callbackInvocationExpression = mockedMethodArgument
                                               .GetParentSafe <IArgumentList>()
                                               .GetParentSafe <IInvocationExpression>();

            if (callbackInvocationExpression == null || !callbackInvocationExpression.IsMoqCallbackMethod())
            {
                return;
            }
            var invokedExpression = callbackInvocationExpression.InvokedExpression as IReferenceExpression;

            if (invokedExpression == null)
            {
                return;
            }
            var setupOrReturnInvocationExpression = invokedExpression.QualifierExpression as IInvocationExpression;
            IInvocationExpression setupInvocationExpression;

            if (setupOrReturnInvocationExpression.IsMoqSetupMethod())
            {
                setupInvocationExpression = setupOrReturnInvocationExpression;
            }
            else if (setupOrReturnInvocationExpression.IsMoqReturnsMethod())
            {
                var invokedExpression2 = setupOrReturnInvocationExpression.InvokedExpression as IReferenceExpression;
                if (invokedExpression2 == null)
                {
                    return;
                }
                setupInvocationExpression = invokedExpression2.QualifierExpression as IInvocationExpression;
            }
            else
            {
                return;
            }
            var targetMethod = setupInvocationExpression.GetMockedMethodFromSetupMethod();

            if (targetMethod == null)
            {
                return;
            }
            if (targetMethod.Item1.Parameters.Any(x => x.Type == null))
            {
                return;
            }
            var argsString = targetMethod.Item1.Parameters.Select(x =>
            {
                return((targetMethod.Item2 == null ? x.Type.GetPresentableName(CSharpLanguage.Instance) : targetMethod.Item2.Apply(x.Type).GetPresentableName(CSharpLanguage.Instance)) + " " + x.ShortName);
            });

#if RESHARPER8
            var textualLookupItem = context.LookupItemsFactory.CreateTextLookupItem("(" + string.Join(", ", argsString) + ") => {}");
            collector.AddToTop(textualLookupItem);
#endif
#if RESHARPER9
            var textLookupItem = new TextLookupItem("(" + string.Join(", ", argsString) + ") => {}");
            textLookupItem.PlaceTop();
            textLookupItem.InitializeRanges(context.CompletionRanges, context.BasicContext);
            collector.Add(textLookupItem);
#endif
        }
        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);
        }
        private void AddLookupItemsNew([NotNull] CSharpCodeCompletionContext context, [NotNull] GroupedItemsCollector collector)
        {
            if (context.TerminatedContext == null)
            {
                return;
            }
            var identifier           = context.TerminatedContext.TreeNode as IIdentifier;
            var mockedMethodArgument = identifier
                                       .GetParentSafe <IReferenceExpression>()
                                       .GetParentSafe <ICSharpArgument>();

            if (mockedMethodArgument == null)
            {
                return;
            }
            var mockedMethodInvocationExpression = mockedMethodArgument
                                                   .GetParentSafe <IArgumentList>()
                                                   .GetParentSafe <IInvocationExpression>();

            if (mockedMethodInvocationExpression == null)
            {
                return;
            }
            var setupMethodInvocationExpression = mockedMethodInvocationExpression
                                                  .GetParentSafe <ILambdaExpression>()
                                                  .GetParentSafe <IArgument>()
                                                  .GetParentSafe <IArgumentList>()
                                                  .GetParentSafe <IInvocationExpression>();

            if (setupMethodInvocationExpression == null || !setupMethodInvocationExpression.IsMoqSetupMethod())
            {
                return;
            }
            int argumentIndex = mockedMethodArgument.IndexOf();

            if (argumentIndex == 0 && 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 => "It.IsAny<" + 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
                });
            }
            if (context.ExpectedTypesContext != null)
            {
                foreach (var expectedType in context.ExpectedTypesContext.ExpectedITypes)
                {
                    if (expectedType.Type == null)
                    {
                        continue;
                    }
                    var typeName = expectedType.Type.GetPresentableName(CSharpLanguage.Instance);
#if RESHARPER8
                    collector.AddToTop(context.LookupItemsFactory.CreateTextLookupItem("It.IsAny<" + typeName + ">()"));
#endif
#if RESHARPER9
                    var textLookupItem = new TextLookupItem("It.IsAny<" + typeName + ">()");
                    textLookupItem.InitializeRanges(context.CompletionRanges, context.BasicContext);
                    textLookupItem.PlaceTop();
                    collector.Add(textLookupItem);
#endif
                }
            }
        }