protected override bool AddLookupItems(CSharpCodeCompletionContext context, IItemsCollector collector) { var candidateExistingElements = new List <ISymbolInfo>(); var table = GetSymbolTable(context); table?.ForAllSymbolInfos(info => { var declaredElement = info.GetDeclaredElement(); var 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 (var candidateExistingElement in candidateExistingElements) { var proposedCompletion = candidateExistingElement.ShortName + ".Object"; var lookupItem = GetLookupItem(context, proposedCompletion); collector.Add(lookupItem); } if (context.ExpectedTypesContext != null) { foreach (var expectedType in context.ExpectedTypesContext.ExpectedITypes) { if (expectedType.Type == null) { continue; } if (expectedType.Type.IsInterfaceType()) { string typeName = expectedType.Type.GetPresentableName(CSharpLanguage.Instance); var proposedCompletion = "new Mock<" + typeName + ">().Object"; var lookupItem = GetLookupItem(context, proposedCompletion); collector.Add(lookupItem); } } } return(true); }
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); }