Ejemplo n.º 1
0
        private static Func <CancellationToken, Task <Document> > GetCreateChangedDocument(CodeFixContext context, SyntaxNode node)
        {
            switch (node.Kind())
            {
            case SyntaxKind.MethodDeclaration:
            {
                var methodDeclaration = (MethodDeclarationSyntax)node;

                SyntaxToken semicolonToken = methodDeclaration.SemicolonToken;

                if (semicolonToken.Kind() == SyntaxKind.None)
                {
                    break;
                }

                ParameterListSyntax parameterList = methodDeclaration.ParameterList;

                if (parameterList == null)
                {
                    break;
                }

                return(cancellationToken =>
                    {
                        MethodDeclarationSyntax newNode = methodDeclaration
                                                          .WithParameterList(parameterList.AppendToTrailingTrivia(semicolonToken.GetAllTrivia()))
                                                          .WithSemicolonToken(default(SyntaxToken))
                                                          .WithBody(Block())
                                                          .WithFormatterAnnotation();

                        return context.Document.ReplaceNodeAsync(node, newNode, cancellationToken);
                    });
            }

            case SyntaxKind.ConstructorDeclaration:
            {
                var constructorDeclaration = (ConstructorDeclarationSyntax)node;

                SyntaxToken semicolonToken = constructorDeclaration.SemicolonToken;

                if (semicolonToken.Kind() == SyntaxKind.None)
                {
                    break;
                }

                ParameterListSyntax parameterList = constructorDeclaration.ParameterList;

                if (parameterList == null)
                {
                    break;
                }

                return(cancellationToken =>
                    {
                        ConstructorDeclarationSyntax newNode = constructorDeclaration
                                                               .WithParameterList(parameterList.AppendToTrailingTrivia(semicolonToken.GetAllTrivia()))
                                                               .WithSemicolonToken(default(SyntaxToken))
                                                               .WithBody(Block())
                                                               .WithFormatterAnnotation();

                        return context.Document.ReplaceNodeAsync(node, newNode, cancellationToken);
                    });
            }

            case SyntaxKind.DestructorDeclaration:
            {
                var destructorDeclaration = (DestructorDeclarationSyntax)node;

                SyntaxToken semicolonToken = destructorDeclaration.SemicolonToken;

                if (semicolonToken.Kind() == SyntaxKind.None)
                {
                    break;
                }

                ParameterListSyntax parameterList = destructorDeclaration.ParameterList;

                if (parameterList == null)
                {
                    break;
                }

                return(cancellationToken =>
                    {
                        DestructorDeclarationSyntax newNode = destructorDeclaration
                                                              .WithParameterList(parameterList.AppendToTrailingTrivia(semicolonToken.GetAllTrivia()))
                                                              .WithSemicolonToken(default(SyntaxToken))
                                                              .WithBody(Block())
                                                              .WithFormatterAnnotation();

                        return context.Document.ReplaceNodeAsync(node, newNode, cancellationToken);
                    });
            }

            case SyntaxKind.OperatorDeclaration:
            {
                var operatorDeclaration = (OperatorDeclarationSyntax)node;

                SyntaxToken semicolonToken = operatorDeclaration.SemicolonToken;

                if (semicolonToken.Kind() == SyntaxKind.None)
                {
                    break;
                }

                ParameterListSyntax parameterList = operatorDeclaration.ParameterList;

                if (parameterList == null)
                {
                    break;
                }

                return(cancellationToken =>
                    {
                        OperatorDeclarationSyntax newNode = operatorDeclaration
                                                            .WithParameterList(parameterList.AppendToTrailingTrivia(semicolonToken.GetAllTrivia()))
                                                            .WithSemicolonToken(default(SyntaxToken))
                                                            .WithBody(Block())
                                                            .WithFormatterAnnotation();

                        return context.Document.ReplaceNodeAsync(node, newNode, cancellationToken);
                    });
            }

            case SyntaxKind.ConversionOperatorDeclaration:
            {
                var conversionOperatorDeclaration = (ConversionOperatorDeclarationSyntax)node;

                SyntaxToken semicolonToken = conversionOperatorDeclaration.SemicolonToken;

                if (semicolonToken.Kind() == SyntaxKind.None)
                {
                    break;
                }

                ParameterListSyntax parameterList = conversionOperatorDeclaration.ParameterList;

                if (parameterList == null)
                {
                    break;
                }

                return(cancellationToken =>
                    {
                        ConversionOperatorDeclarationSyntax newNode = conversionOperatorDeclaration
                                                                      .WithParameterList(parameterList.AppendToTrailingTrivia(semicolonToken.GetAllTrivia()))
                                                                      .WithSemicolonToken(default(SyntaxToken))
                                                                      .WithBody(Block())
                                                                      .WithFormatterAnnotation();

                        return context.Document.ReplaceNodeAsync(node, newNode, cancellationToken);
                    });
            }

            case SyntaxKind.GetAccessorDeclaration:
            case SyntaxKind.SetAccessorDeclaration:
            {
                var accessorDeclaration = (AccessorDeclarationSyntax)node;

                SyntaxToken semicolonToken = accessorDeclaration.SemicolonToken;

                if (semicolonToken.Kind() == SyntaxKind.None)
                {
                    break;
                }

                return(cancellationToken =>
                    {
                        AccessorDeclarationSyntax newNode = accessorDeclaration
                                                            .WithSemicolonToken(default(SyntaxToken))
                                                            .WithBody(Block(
                                                                          Token(default(SyntaxTriviaList), SyntaxKind.OpenBraceToken, TriviaList(ElasticSpace)),
                                                                          default(SyntaxList <StatementSyntax>),
                                                                          Token(default(SyntaxTriviaList), SyntaxKind.CloseBraceToken, semicolonToken.LeadingAndTrailingTrivia())));

                        SyntaxToken keyword = newNode.Keyword;

                        if (!keyword.HasTrailingTrivia)
                        {
                            newNode = newNode.WithKeyword(keyword.WithTrailingTrivia(ElasticSpace));
                        }

                        return context.Document.ReplaceNodeAsync(node, newNode, cancellationToken);
                    });
            }

            case SyntaxKind.LocalFunctionStatement:
            {
                var localFunction = (LocalFunctionStatementSyntax)node;

                SyntaxToken semicolonToken = localFunction.SemicolonToken;

                if (semicolonToken.Kind() == SyntaxKind.None)
                {
                    break;
                }

                ParameterListSyntax parameterList = localFunction.ParameterList;

                if (parameterList == null)
                {
                    break;
                }

                return(cancellationToken =>
                    {
                        LocalFunctionStatementSyntax newNode = localFunction
                                                               .WithParameterList(parameterList.AppendToTrailingTrivia(semicolonToken.GetAllTrivia()))
                                                               .WithSemicolonToken(default(SyntaxToken))
                                                               .WithBody(Block())
                                                               .WithFormatterAnnotation();

                        return context.Document.ReplaceNodeAsync(node, newNode, cancellationToken);
                    });
            }
            }

            Debug.Fail(node.Kind().ToString());

            return(null);
        }