示例#1
0
        public override async Task RegisterCodeFixesAsync(CodeFixContext context)
        {
            SyntaxNode root = await context.GetSyntaxRootAsync().ConfigureAwait(false);

            if (!TryFindFirstAncestorOrSelf(
                    root,
                    context.Span,
                    out SyntaxNode node,
                    predicate: f => f.IsKind(SyntaxKind.ArrowExpressionClause) || CSharpFacts.CanHaveExpressionBody(f.Kind())))
            {
                return;
            }

            Document   document   = context.Document;
            Diagnostic diagnostic = context.Diagnostics[0];

            if (node is ArrowExpressionClauseSyntax arrowExpressionClause)
            {
                CodeAction codeAction = CodeAction.Create(
                    ConvertExpressionBodyToBlockBodyRefactoring.Title,
                    ct => ConvertExpressionBodyToBlockBodyRefactoring.RefactorAsync(document, arrowExpressionClause, ct),
                    GetEquivalenceKey(diagnostic));

                context.RegisterCodeFix(codeAction, diagnostic);
            }
            else
            {
                CodeAction codeAction = CodeAction.Create(
                    ConvertBlockBodyToExpressionBodyRefactoring.Title,
                    ct => ConvertBlockBodyToExpressionBodyRefactoring.RefactorAsync(document, node, ct),
                    GetEquivalenceKey(diagnostic));

                context.RegisterCodeFix(codeAction, diagnostic);
            }
        }
示例#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)
        {
            IEnumerable <ExpressionStatementSyntax> expressionStatements = parameterSymbols
                                                                           .Where(f => f.RefKind == RefKind.Out && !alwaysAssigned.Contains(f))
                                                                           .Select(f =>
            {
                ExpressionStatementSyntax expressionStatement = SimpleAssignmentStatement(
                    IdentifierName(f.Name),
                    f.Type.GetDefaultValueSyntax(document.GetDefaultSyntaxOptions()));

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

            SyntaxNode newNode = null;

            if (bodyOrExpressionBody is ArrowExpressionClauseSyntax expressionBody)
            {
                newNode = ConvertExpressionBodyToBlockBodyRefactoring.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));
        }