示例#1
0
        private async Task<Microsoft.CodeAnalysis.CodeActions.CodeAction> GetActionAsync(SemanticModel semanticModel, SyntaxTree tree, SyntaxToken token)
        {
            var methodSymbol = GetMethodDefinitionSymbol(semanticModel, token);
            if (methodSymbol == null || methodSymbol.Locations.Any(l => l.IsInMetadata))
            {
                // can't find method definition defined in source
                return null;
            }

            // adding or deleting ref/out on anonymous method/lambda not supported
            if (methodSymbol.MethodKind == MethodKind.AnonymousFunction)
            {
                return null;
            }

            var argumentAndParameters = await GetArgumentAndParametersAsync(semanticModel, methodSymbol, token).ConfigureAwait(false);
            if (argumentAndParameters == null)
            {
                return null;
            }

            // currently only support everything in one file
            var nodes = (new SyntaxNode[] { argumentAndParameters.Item1 }).Concat(argumentAndParameters.Item2);
            if (this.document.Project.GetContainingDocuments(nodes, this.cancellationToken).Count() != 1)
            {
                return null;
            }

            if (tree.OnArgumentOrParameterWithoutRefOut(this.position))
            {
                return AddOutOrRefCodeAction.Applicable(semanticModel, argumentAndParameters.Item1, argumentAndParameters.Item2) ?
                    new AddOutOrRefCodeAction(this.document, semanticModel, argumentAndParameters.Item1, argumentAndParameters.Item2) : null;
            }
            else
            {
                return RemoveOutOrRefCodeAction.Applicable(semanticModel, argumentAndParameters.Item1, argumentAndParameters.Item2) ?
                    new RemoveOutOrRefCodeAction(this.document, semanticModel, argumentAndParameters.Item1, argumentAndParameters.Item2) : null;
            }
        }