public override SyntaxNode VisitParenthesizedLambdaExpression(ParenthesizedLambdaExpressionSyntax node)
        {
            node = (ParenthesizedLambdaExpressionSyntax)base.VisitParenthesizedLambdaExpression(node);

            if (transformKind == TransformKind.LambdaToAnonMethod)
            {
                // If any of the lambda parameters do not have type explicitly specified then we don't do any transforms.
                foreach (var parameter in node.ParameterList.Parameters)
                {
                    if (parameter.Type == null)
                    {
                        return(node);
                    }
                }

                // If the body of the lambda is not a block syntax we don't do any transforms.
                if (node.Body.Kind() != SyntaxKind.Block)
                {
                    return(node);
                }

                return(SyntaxFactory.AnonymousMethodExpression(
                           default(SyntaxToken),
                           SyntaxFactory.Token(SyntaxKind.DelegateKeyword),
                           node.ParameterList,
                           (BlockSyntax)node.Body));
            }

            return(node);
        }
 private static SyntaxNode ConvertAnonymousMethodToAsync(AnonymousMethodExpressionSyntax anonymousMethod)
 {
     return(SyntaxFactory.AnonymousMethodExpression(
                SyntaxFactory.Token(SyntaxKind.AsyncKeyword),
                anonymousMethod.DelegateKeyword,
                anonymousMethod.ParameterList,
                anonymousMethod.Block)
            .WithTriviaFrom(anonymousMethod)
            .WithAdditionalAnnotations(Formatter.Annotation));
 }
Beispiel #3
0
        public override SyntaxNode MakeSyntaxNode()
        {
            var res = SyntaxFactory.AnonymousMethodExpression(AsyncKeyword, DelegateKeyword, ParameterList, Body);

            if (Block != null)
            {
                res = res.WithBlock(Block);
            }
            IsChanged = false;
            return(res);
        }
        public override async Task ComputeRefactoringsAsync(CodeRefactoringContext context)
        {
            var document = context.Document;

            if (document.Project.Solution.Workspace.Kind == WorkspaceKind.MiscellaneousFiles)
            {
                return;
            }
            var span = context.Span;

            if (!span.IsEmpty)
            {
                return;
            }
            var cancellationToken = context.CancellationToken;

            if (cancellationToken.IsCancellationRequested)
            {
                return;
            }
            var model = await document.GetSemanticModelAsync(cancellationToken).ConfigureAwait(false);

            if (model.IsFromGeneratedCode(cancellationToken))
            {
                return;
            }
            var root = await model.SyntaxTree.GetRootAsync(cancellationToken).ConfigureAwait(false);

            var token = root.FindToken(span.Start);

            if (!token.IsKind(SyntaxKind.EqualsGreaterThanToken))
            {
                return;
            }
            var node = token.Parent;

            if (!node.IsKind(SyntaxKind.ParenthesizedLambdaExpression) && !node.IsKind(SyntaxKind.SimpleLambdaExpression))
            {
                return;
            }

            context.RegisterRefactoring(
                CodeActionFactory.Create(
                    token.Span,
                    DiagnosticSeverity.Info,
                    GettextCatalog.GetString("To anonymous method"),
                    t2 =>
            {
                var parameters = new List <ParameterSyntax>();

                CSharpSyntaxNode bodyExpr;
                if (node.IsKind(SyntaxKind.ParenthesizedLambdaExpression))
                {
                    var ple = (ParenthesizedLambdaExpressionSyntax)node;
                    parameters.AddRange(ConvertParameters(model, node, ple.ParameterList.Parameters));
                    bodyExpr = ple.Body;
                }
                else
                {
                    var sle = ((SimpleLambdaExpressionSyntax)node);
                    parameters.AddRange(ConvertParameters(model, node, new[] { sle.Parameter }));
                    bodyExpr = sle.Body;
                }

                if (ConvertLambdaBodyExpressionToStatementCodeRefactoringProvider.RequireReturnStatement(model, node))
                {
                    bodyExpr = SyntaxFactory.Block(SyntaxFactory.ReturnStatement(bodyExpr as ExpressionSyntax));
                }
                var ame = SyntaxFactory.AnonymousMethodExpression(
                    parameters.Count == 0 ? null : SyntaxFactory.ParameterList(SyntaxFactory.SeparatedList(parameters)),
                    bodyExpr as BlockSyntax ?? SyntaxFactory.Block(SyntaxFactory.ExpressionStatement(bodyExpr as ExpressionSyntax))
                    );

                var newRoot = root.ReplaceNode((SyntaxNode)node, ame.WithAdditionalAnnotations(Formatter.Annotation));
                return(Task.FromResult(document.WithSyntaxRoot(newRoot)));
            }
                    )
                );
        }
        public override async Task ComputeRefactoringsAsync(CodeRefactoringContext context)
        {
            var document = context.Document;

            if (document.Project.Solution.Workspace.Kind == WorkspaceKind.MiscellaneousFiles)
            {
                return;
            }
            var span = context.Span;

            if (!span.IsEmpty)
            {
                return;
            }
            var cancellationToken = context.CancellationToken;

            if (cancellationToken.IsCancellationRequested)
            {
                return;
            }
            var model = await document.GetSemanticModelAsync(cancellationToken).ConfigureAwait(false);

            if (model.IsFromGeneratedCode(cancellationToken))
            {
                return;
            }
            var root = await model.SyntaxTree.GetRootAsync(cancellationToken).ConfigureAwait(false);

            var node = root.FindNode(span);

            if (!node.IsKind(SyntaxKind.IdentifierName))
            {
                return;
            }

            if (node.Parent.IsKind(SyntaxKind.SimpleMemberAccessExpression))
            {
                node = node.Parent;
            }

            var info = model.GetTypeInfo(node, cancellationToken);
            var type = info.ConvertedType ?? info.Type;

            if (type == null)
            {
                return;
            }

            var invocationMethod = type.GetDelegateInvokeMethod();

            if (invocationMethod == null)
            {
                return;
            }

            context.RegisterRefactoring(
                CodeActionFactory.Create(
                    node.Span,
                    DiagnosticSeverity.Info,
                    GettextCatalog.GetString("To anonymous method"),
                    t2 =>
            {
                var expr = SyntaxFactory.InvocationExpression(
                    (ExpressionSyntax)node,
                    SyntaxFactory.ArgumentList(SyntaxFactory.SeparatedList(invocationMethod.Parameters.Select(p => SyntaxFactory.Argument(SyntaxFactory.IdentifierName(p.Name)))))
                    );
                var parameters = invocationMethod.Parameters.Select(p => CreateParameterSyntax(model, node, p)).ToList();
                var stmt       = invocationMethod.ReturnType.SpecialType == SpecialType.System_Void ? (StatementSyntax)SyntaxFactory.ExpressionStatement(expr) : SyntaxFactory.ReturnStatement(expr);
                var ame        = SyntaxFactory.AnonymousMethodExpression(
                    parameters.Count == 0 ? null : SyntaxFactory.ParameterList(SyntaxFactory.SeparatedList(parameters)),
                    SyntaxFactory.Block(stmt)
                    );
                var newRoot = root.ReplaceNode((SyntaxNode)node, ame.WithAdditionalAnnotations(Formatter.Annotation));
                return(Task.FromResult(document.WithSyntaxRoot(newRoot)));
            }
                    )
                );
        }