예제 #1
0
        public override async Task ProvideCompletionsAsync(CompletionContext context)
        {
            try
            {
                if (!context.Document.SupportsSemanticModel || !context.Document.SupportsSyntaxTree)
                {
                    return;
                }

                var hasMoqReferenced = context.Document.Project.MetadataReferences.Any(r => r.Display.Contains(this._provider.AssemblyName));
                if (!hasMoqReferenced)
                {
                    return;
                }

                var syntaxRoot = await context.Document.GetSyntaxRootAsync();

                var token = SyntaxHelpers.GetSelectedTokens(syntaxRoot, context.Position);

                // Not in an opened method
                if (token.Parent == null)
                {
                    return;
                }

                var mockedMethodArgumentList = token.Parent as ArgumentListSyntax;
                var setupMethodInvocation    = mockedMethodArgumentList.Ancestors()
                                               .OfType <InvocationExpressionSyntax>()
                                               .Where(IsMoqSetupMethod)
                                               .FirstOrDefault();

                if (setupMethodInvocation == null)
                {
                    return;
                }

                var semanticModel = await context.Document.GetSemanticModelAsync();

                var matchingMockedMethods = SyntaxHelpers.GetCandidatesMockedMethodSignaturesForLambda(semanticModel, setupMethodInvocation);

                var completionService = new CompletionService(context, token, semanticModel, this._provider);

                foreach (IMethodSymbol matchingMockedMethodSymbol in matchingMockedMethods)
                {
                    completionService.AddSuggestionsForMethod(matchingMockedMethodSymbol, mockedMethodArgumentList);
                }
            }
            catch
            {
            }
        }