Пример #1
0
        public sealed override async Task RegisterCodeFixesAsync(CodeFixContext context)
        {
            SyntaxNode root = await context.Document.GetSyntaxRootAsync(context.CancellationToken).ConfigureAwait(false);

            var statement = (StatementSyntax)root
                            .FindNode(context.Span, getInnermostNodeForTie: true)?
                            .FirstAncestorOrSelf(f => f.IsKind(SyntaxKind.IfStatement, SyntaxKind.SwitchStatement));

            Debug.Assert(statement != null, $"{nameof(statement)} is null");

            if (statement == null)
            {
                return;
            }

            CodeAction codeAction = CodeAction.Create(
                "Use return instead of assignment",
                cancellationToken =>
            {
                return(UseReturnInsteadOfAssignmentRefactoring.RefactorAsync(
                           context.Document,
                           statement,
                           cancellationToken));
            },
                DiagnosticIdentifiers.UseReturnInsteadOfAssignment + EquivalenceKeySuffix);

            context.RegisterCodeFix(codeAction, context.Diagnostics);
        }
Пример #2
0
        public sealed override async Task RegisterCodeFixesAsync(CodeFixContext context)
        {
            SyntaxNode root = await context.GetSyntaxRootAsync().ConfigureAwait(false);

            if (!TryFindFirstAncestorOrSelf(root, context.Span, out StatementSyntax statement, predicate: f => f.IsKind(SyntaxKind.IfStatement, SyntaxKind.SwitchStatement)))
            {
                return;
            }

            CodeAction codeAction = CodeAction.Create(
                "Use return instead of assignment",
                cancellationToken =>
            {
                return(UseReturnInsteadOfAssignmentRefactoring.RefactorAsync(
                           context.Document,
                           statement,
                           cancellationToken));
            },
                GetEquivalenceKey(DiagnosticIdentifiers.UseReturnInsteadOfAssignment));

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