Esempio n. 1
0
        public sealed override async Task RegisterCodeFixesAsync(CodeFixContext context)
        {
            SyntaxNode root = await context.GetSyntaxRootAsync().ConfigureAwait(false);

            if (!TryFindFirstAncestorOrSelf(root, context.Span, out ArrowExpressionClauseSyntax arrowExpressionClause))
            {
                return;
            }

            CodeAction codeAction = CodeAction.Create(
                "Expand expression body",
                cancellationToken => ExpandExpressionBodyRefactoring.RefactorAsync(context.Document, arrowExpressionClause, cancellationToken),
                GetEquivalenceKey(DiagnosticIdentifiers.AvoidMultilineExpressionBody));

            context.RegisterCodeFix(codeAction, context.Diagnostics);
        }
Esempio n. 2
0
        private static Task <Document> RefactorAsync(
            Document document,
            SyntaxNode node,
            StatementSyntax statement,
            SyntaxNode bodyOrExpressionBody,
            ImmutableArray <IParameterSymbol> parameterSymbols,
            ImmutableArray <ISymbol> alwaysAssigned,
            SemanticModel semanticModel,
            CancellationToken cancellationToken = default(CancellationToken))
        {
            IEnumerable <ExpressionStatementSyntax> expressionStatements = parameterSymbols
                                                                           .Where(f => f.RefKind == RefKind.Out && !alwaysAssigned.Contains(f))
                                                                           .Select(f =>
            {
                ExpressionStatementSyntax expressionStatement = SimpleAssignmentStatement(
                    IdentifierName(f.Name),
                    f.Type.GetDefaultValueSyntax(semanticModel, bodyOrExpressionBody.Span.End));

                return(expressionStatement.WithFormatterAnnotation());
            });

            SyntaxNode newNode = null;

            if (bodyOrExpressionBody is ArrowExpressionClauseSyntax expressionBody)
            {
                newNode = ExpandExpressionBodyRefactoring.Refactor(expressionBody, semanticModel, cancellationToken);

                newNode = InsertStatements(newNode, expressionStatements);
            }
            else if (statement != null)
            {
                if (statement.IsEmbedded())
                {
                    newNode = node.ReplaceNode(statement, Block(expressionStatements.Concat(new StatementSyntax[] { statement })));
                }
                else
                {
                    newNode = node.InsertNodesBefore(statement, expressionStatements);
                }
            }
            else
            {
                newNode = InsertStatements(node, expressionStatements);
            }

            return(document.ReplaceNodeAsync(node, newNode, cancellationToken));
        }
Esempio n. 3
0
        private static Task <Document> RefactorAsync(
            Document document,
            SyntaxNode node,
            StatementSyntax statement,
            SyntaxNode bodyOrExpressionBody,
            IParameterSymbol parameter,
            SemanticModel semanticModel,
            CancellationToken cancellationToken)
        {
            ExpressionStatementSyntax expressionStatement = SimpleAssignmentStatement(
                IdentifierName(parameter.Name),
                parameter.Type.ToDefaultValueSyntax(semanticModel, bodyOrExpressionBody.Span.End));

            expressionStatement = expressionStatement.WithFormatterAnnotation();

            SyntaxNode newNode = null;

            if (bodyOrExpressionBody.IsKind(SyntaxKind.ArrowExpressionClause))
            {
                newNode = ExpandExpressionBodyRefactoring.Refactor((ArrowExpressionClauseSyntax)bodyOrExpressionBody, semanticModel, cancellationToken);

                newNode = InsertStatement(newNode, expressionStatement);
            }
            else
            {
                if (statement != null)
                {
                    if (EmbeddedStatementHelper.IsEmbeddedStatement(statement))
                    {
                        newNode = node.ReplaceNode(statement, Block(expressionStatement, statement));
                    }
                    else
                    {
                        newNode = node.InsertNodesBefore(statement, new StatementSyntax[] { expressionStatement });
                    }
                }
                else
                {
                    newNode = InsertStatement(node, expressionStatement);
                }
            }

            return(document.ReplaceNodeAsync(node, newNode, cancellationToken));
        }
        public sealed override async Task RegisterCodeFixesAsync(CodeFixContext context)
        {
            SyntaxNode root = await context.GetSyntaxRootAsync().ConfigureAwait(false);

            ArrowExpressionClauseSyntax arrowExpressionClause = root
                                                                .FindNode(context.Span, getInnermostNodeForTie: true)?
                                                                .FirstAncestorOrSelf <ArrowExpressionClauseSyntax>();

            if (arrowExpressionClause == null)
            {
                return;
            }

            CodeAction codeAction = CodeAction.Create(
                "Expand expression body",
                cancellationToken => ExpandExpressionBodyRefactoring.RefactorAsync(context.Document, arrowExpressionClause, cancellationToken),
                DiagnosticIdentifiers.AvoidMultilineExpressionBody + EquivalenceKeySuffix);

            context.RegisterCodeFix(codeAction, context.Diagnostics);
        }