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 } } }
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 } } }