Skip to content

Commit c6e81da

Browse files
committed
Add Mock.Of suggestion
1 parent 0333c7f commit c6e81da

2 files changed

Lines changed: 11 additions & 41 deletions

File tree

Abc.MoqComplete/Abc.MoqComplete/CompletionProvider/SuggestMockProvider.cs

Lines changed: 10 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -34,51 +34,21 @@ protected override bool IsAvailable(CSharpCodeCompletionContext context)
3434

3535
protected override bool AddLookupItems(CSharpCodeCompletionContext context, IItemsCollector collector)
3636
{
37-
var candidateExistingElements = new List<ISymbolInfo>();
38-
var table = GetSymbolTable(context);
39-
40-
table?.ForAllSymbolInfos(info =>
41-
{
42-
var declaredElement = info.GetDeclaredElement();
43-
var type = declaredElement.Type();
44-
45-
if (type != null)
46-
{
47-
if (type.GetClassType().ConvertToString() == "Class:Moq.Mock`1")
48-
{
49-
IType typeParameter = TypesUtil.GetTypeArgumentValue(type, 0);
50-
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)))
51-
{
52-
candidateExistingElements.Add(info);
53-
}
54-
}
55-
}
56-
});
57-
58-
foreach (var candidateExistingElement in candidateExistingElements)
59-
{
60-
var proposedCompletion = candidateExistingElement.ShortName + ".Object";
61-
var lookupItem = GetLookupItem(context, proposedCompletion);
62-
collector.Add(lookupItem);
63-
}
37+
if (context.ExpectedTypesContext == null)
38+
return false;
6439

65-
if (context.ExpectedTypesContext != null)
40+
foreach (var expectedType in context.ExpectedTypesContext.ExpectedITypes)
6641
{
67-
foreach (var expectedType in context.ExpectedTypesContext.ExpectedITypes)
42+
if (expectedType.Type.IsInterfaceType())
6843
{
69-
if (expectedType.Type == null)
70-
continue;
71-
72-
if (expectedType.Type.IsInterfaceType())
73-
{
74-
string typeName = expectedType.Type.GetPresentableName(CSharpLanguage.Instance);
75-
var proposedCompletion = "new Mock<" + typeName + ">().Object";
76-
var lookupItem = GetLookupItem(context, proposedCompletion);
77-
collector.Add(lookupItem);
78-
}
44+
var typeName = expectedType.Type.GetPresentableName(CSharpLanguage.Instance);
45+
var newMock = GetLookupItem(context, "new Mock<" + typeName + ">().Object");
46+
var mockOf = GetLookupItem(context, $"Mock.Of<{typeName}>()");
47+
collector.Add(newMock);
48+
collector.Add(mockOf);
7949
}
8050
}
81-
return true;
51+
return context.ExpectedTypesContext.ExpectedITypes.Count > 0;
8252
}
8353

8454
private static ILookupItem GetLookupItem(CSharpCodeCompletionContext context, string proposedCompletion)

Abc.MoqComplete/Directory.Build.props

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@
2222
</PropertyGroup>
2323

2424
<PropertyGroup>
25-
<Version>1.2.12</Version>
25+
<Version>1.2.13</Version>
2626
</PropertyGroup>
2727

2828
</Project>

0 commit comments

Comments
 (0)