Skip to content

Commit 7b23985

Browse files
authored
Merge pull request #6 from MrLuje/feature/fakeiteasy
Add support of FakeItEasy #5
2 parents 0bf56a9 + e76e46e commit 7b23985

File tree

4 files changed

+87
-0
lines changed

4 files changed

+87
-0
lines changed
22 KB
Loading
Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
using Microsoft.CodeAnalysis;
2+
using Microsoft.CodeAnalysis.Completion;
3+
using Microsoft.CodeAnalysis.CSharp;
4+
using Microsoft.CodeAnalysis.CSharp.Syntax;
5+
using Mocking.Helpers.FakeItEasy;
6+
using System;
7+
using System.Collections.Generic;
8+
using System.Linq;
9+
using System.Threading.Tasks;
10+
11+
namespace Mocking.Helpers.FakeItEasy
12+
{
13+
[ExportCompletionProvider(nameof(FakeItEasyIgnoredCompletion), LanguageNames.CSharp)]
14+
public class FakeItEasyIgnoredCompletion : CompletionProvider
15+
{
16+
private FakeItEasyProvider _provider;
17+
18+
public FakeItEasyIgnoredCompletion()
19+
{
20+
this._provider = new FakeItEasyProvider();
21+
}
22+
23+
internal bool IsFakeItEasyCallToMethod(InvocationExpressionSyntax invocation)
24+
{
25+
return SyntaxHelpers.IsMethodNamed(invocation, this._provider.MockingMethodName);
26+
}
27+
28+
public override async Task ProvideCompletionsAsync(CompletionContext context)
29+
{
30+
try
31+
{
32+
if (!context.Document.SupportsSemanticModel || !context.Document.SupportsSyntaxTree) return;
33+
34+
var hasFakeItEasyReferenced = context.Document.Project.MetadataReferences.Any(r => r.Display.Contains(this._provider.AssemblyName));
35+
if (!hasFakeItEasyReferenced) return;
36+
37+
var syntaxRoot = await context.Document.GetSyntaxRootAsync();
38+
var token = SyntaxHelpers.GetSelectedTokens(syntaxRoot, context.Position);
39+
40+
// Not in an opened method
41+
if (token.Parent == null) return;
42+
43+
var mockedMethodArgumentList = token.Parent as ArgumentListSyntax;
44+
var mockedMethodInvocation = mockedMethodArgumentList.Ancestors()
45+
.OfType<InvocationExpressionSyntax>()
46+
.Where(IsFakeItEasyCallToMethod)
47+
.FirstOrDefault();
48+
49+
if (mockedMethodInvocation == null) return;
50+
51+
var semanticModel = await context.Document.GetSemanticModelAsync();
52+
var matchingMockedMethods = SyntaxHelpers.GetCandidatesMockedMethodSignaturesForLambda(semanticModel, mockedMethodInvocation);
53+
54+
var completionService = new CompletionService(context, token, semanticModel, this._provider);
55+
56+
foreach (IMethodSymbol matchingMockedMethodSymbol in matchingMockedMethods)
57+
{
58+
completionService.AddSuggestionsForMethod(matchingMockedMethodSymbol, mockedMethodArgumentList);
59+
}
60+
}
61+
catch
62+
{
63+
}
64+
}
65+
}
66+
}
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
using Microsoft.CodeAnalysis;
2+
using Microsoft.CodeAnalysis.CSharp.Syntax;
3+
using Mocking.Helpers.Interfaces;
4+
using System;
5+
using System.Collections.Generic;
6+
using System.Linq;
7+
using System.Text;
8+
9+
namespace Mocking.Helpers.FakeItEasy
10+
{
11+
public class FakeItEasyProvider : BaseMockingProvider
12+
{
13+
public override string MockingMethodName => "CallTo";
14+
public override string AssemblyName => "FakeItEasy";
15+
public override string MockingWildcardMethod => "A<{0}>._";
16+
}
17+
}

README.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,10 @@ Support autocompletion of _It.IsAny_ in _Setup_ method
1212
Support autocompletion of _Arg.Any_ in _For_ method
1313
![moq](Mocking.Helpers/Mocking.Helpers.Vsix/Resources/nsubstitute.png)
1414

15+
## FakeItEasy
16+
Support autocompletion of _A<>.Ignored_ in _CallTo_ Method
17+
![fakeiteasy](Mocking.Helpers/Mocking.Helpers.Vsix/Resources/fakeiteasy.png)
18+
1519
## Troubleshooting
1620
This extension relies on [Roslyn](https://github.com/dotnet/roslyn) for type/methods parsing.
1721

0 commit comments

Comments
 (0)