예제 #1
0
        private static ConversionOperatorDeclarationSyntax UseExpressionBodyIfDesired(
            CodeGenerationOptions options,
            ConversionOperatorDeclarationSyntax declaration,
            ParseOptions parseOptions
            )
        {
            if (declaration.ExpressionBody == null)
            {
                var expressionBodyPreference =
                    options.Options.GetOption(
                        CSharpCodeStyleOptions.PreferExpressionBodiedOperators
                        ).Value;

                if (
                    declaration.Body.TryConvertToArrowExpressionBody(
                        declaration.Kind(),
                        parseOptions,
                        expressionBodyPreference,
                        out var expressionBody,
                        out var semicolonToken
                        )
                    )
                {
                    return(declaration
                           .WithBody(null)
                           .WithExpressionBody(expressionBody)
                           .WithSemicolonToken(semicolonToken));
                }
            }

            return(declaration);
        }
예제 #2
0
        public static ConversionOperatorDeclarationSyntax ToStatementBody(ConversionOperatorDeclarationSyntax method)
        {
            var body = method.ExpressionBody.Expression.WithLeadingTrivia(SyntaxFactory.Space);

            return(method.WithBody(SyntaxFactory.Block((StatementSyntax)SyntaxFactory.ReturnStatement(body)))
                   .WithExpressionBody(null)
                   .WithSemicolonToken(SyntaxFactory.MissingToken(SyntaxKind.SemicolonToken))
                   .WithTrailingTrivia(method.GetTrailingTrivia()));
        }
예제 #3
0
        public override SyntaxNode VisitConversionOperatorDeclaration(ConversionOperatorDeclarationSyntax node)
        {
            if (node.Body == null)
            {
                return(node);
            }

            BlockSyntax block = (BlockSyntax)SyntaxFactory.ParseStatement(GetDefaultMessage());

            return(node.WithBody(block));
        }
예제 #4
0
        private static ConversionOperatorDeclarationSyntax UseExpressionBodyIfDesired(
            CSharpCodeGenerationOptions options, ConversionOperatorDeclarationSyntax declaration)
        {
            if (declaration.ExpressionBody == null)
            {
                if (declaration.Body?.TryConvertToArrowExpressionBody(
                        declaration.Kind(), options.Preferences.LanguageVersion, options.Preferences.PreferExpressionBodiedOperators,
                        out var expressionBody, out var semicolonToken) == true)
                {
                    return(declaration.WithBody(null)
                           .WithExpressionBody(expressionBody)
                           .WithSemicolonToken(semicolonToken));
                }
            }

            return(declaration);
        }
        private async Task <Document> HandleConversionOperatorDeclaration(ConversionOperatorDeclarationSyntax declaration, Document document, CancellationToken cancellationToken)
        {
            var returnStatement = SyntaxFactory.ReturnStatement(
                returnKeyword: SyntaxFactory.Token(SyntaxKind.ReturnKeyword),
                expression: declaration.ExpressionBody.Expression,
                semicolonToken: declaration.SemicolonToken);

            var newDeclaration = declaration
                                 .WithBody(SyntaxFactory.Block(returnStatement))
                                 .WithExpressionBody(null)
                                 .WithSemicolonToken(default(SyntaxToken))
                                 .WithAdditionalAnnotations(Formatter.Annotation);

            var oldRoot = await document.GetSyntaxRootAsync(cancellationToken);

            var newRoot = oldRoot.ReplaceNode(declaration, newDeclaration);

            return(document.WithSyntaxRoot(newRoot));
        }
예제 #6
0
        private static ConversionOperatorDeclarationSyntax UseExpressionBodyIfDesired(
            Workspace workspace, ConversionOperatorDeclarationSyntax declaration, ParseOptions options)
        {
            if (declaration.ExpressionBody == null)
            {
                var preferExpressionBody = workspace.Options.GetOption(CSharpCodeStyleOptions.PreferExpressionBodiedOperators).Value;
                if (preferExpressionBody)
                {
                    if (declaration.Body.TryConvertToExpressionBody(
                            options, out var expressionBody, out var semicolonToken))
                    {
                        return declaration.WithBody(null)
                                          .WithExpressionBody(expressionBody)
                                          .WithSemicolonToken(semicolonToken);
                    }
                }
            }

            return declaration;
        }
        private async Task<Document> HandleConversionOperatorDeclaration(ConversionOperatorDeclarationSyntax declaration, Document document, CancellationToken cancellationToken)
        {
            var returnStatement = SyntaxFactory.ReturnStatement(
                returnKeyword: SyntaxFactory.Token(SyntaxKind.ReturnKeyword),
                expression: declaration.ExpressionBody.Expression,
                semicolonToken: declaration.SemicolonToken);

            var newDeclaration = declaration
                .WithBody(SyntaxFactory.Block(returnStatement))
                .WithExpressionBody(null)
                .WithSemicolonToken(default(SyntaxToken))
                .WithAdditionalAnnotations(Formatter.Annotation);

            var oldRoot = await document.GetSyntaxRootAsync(cancellationToken);
            var newRoot = oldRoot.ReplaceNode(declaration, newDeclaration);

            return document.WithSyntaxRoot(newRoot);
        }
예제 #8
0
        private static ConversionOperatorDeclarationSyntax UseExpressionBodyIfDesired(
            Workspace workspace, ConversionOperatorDeclarationSyntax declaration, ParseOptions options)
        {
            if (declaration.ExpressionBody == null)
            {
                var preferExpressionBody = workspace.Options.GetOption(CSharpCodeStyleOptions.PreferExpressionBodiedOperators).Value;
                if (preferExpressionBody)
                {
                    var expressionBody = declaration.Body.TryConvertToExpressionBody(options);
                    if (expressionBody != null)
                    {
                        return declaration.WithBody(null)
                                          .WithExpressionBody(expressionBody)
                                          .WithSemicolonToken(SyntaxFactory.Token(SyntaxKind.SemicolonToken));
                    }
                }
            }

            return declaration;
        }