|
| 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 | +} |
0 commit comments