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

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

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

            if (statement == null)
            {
                return;
            }

            foreach (Diagnostic diagnostic in context.Diagnostics)
            {
                CodeAction codeAction = CodeAction.Create(
                    "Add braces",
                    cancellationToken => AddBracesRefactoring.RefactorAsync(context.Document, statement, cancellationToken),
                    diagnostic.Id + EquivalenceKeySuffix);

                context.RegisterCodeFix(codeAction, diagnostic);
            }
        }
        private void AnalyzeDoStatement(SyntaxNodeAnalysisContext context)
        {
            var doStatement = (DoStatementSyntax)context.Node;

            FormatEmbeddedStatementOnSeparateLineRefactoring.Analyze(context, doStatement);
            AddBracesRefactoring.Analyze(context, doStatement);
        }
        private void AnalyzeWhileStatement(SyntaxNodeAnalysisContext context)
        {
            var whileStatement = (WhileStatementSyntax)context.Node;

            FormatEmbeddedStatementOnSeparateLineRefactoring.Analyze(context, whileStatement);
            AddEmptyLineAfterEmbeddedStatementRefactoring.Analyze(context, whileStatement);
            AddBracesRefactoring.Analyze(context, whileStatement);
        }
Пример #4
0
        public sealed override async Task RegisterCodeFixesAsync(CodeFixContext context)
        {
            SyntaxNode root = await context.GetSyntaxRootAsync().ConfigureAwait(false);

            if (!TryFindFirstAncestorOrSelf(root, context.Span, out StatementSyntax statement))
            {
                return;
            }

            foreach (Diagnostic diagnostic in context.Diagnostics)
            {
                CodeAction codeAction = CodeAction.Create(
                    "Add braces",
                    cancellationToken => AddBracesRefactoring.RefactorAsync(context.Document, statement, cancellationToken),
                    GetEquivalenceKey(diagnostic));

                context.RegisterCodeFix(codeAction, diagnostic);
            }
        }
        public sealed override async Task RegisterCodeFixesAsync(CodeFixContext context)
        {
            SyntaxNode root = await context.Document.GetSyntaxRootAsync(context.CancellationToken).ConfigureAwait(false);

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

            if (statement == null)
            {
                return;
            }

            CodeAction codeAction = CodeAction.Create(
                "Add braces",
                cancellationToken => AddBracesRefactoring.RefactorAsync(context.Document, statement, cancellationToken),
                DiagnosticIdentifiers.AddBraces + EquivalenceKeySuffix);

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