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

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

            if (!TryFindTrivia(root, context.Span.Start, out SyntaxTrivia trivia, findInsideTrivia: false))
            {
                return;
            }

            switch (diagnostic.Id)
            {
            case DiagnosticIdentifiers.AddEmptyLineBetweenAccessors:
            case DiagnosticIdentifiers.AddEmptyLineBetweenSingleLineAccessors:
            case DiagnosticIdentifiers.AddEmptyLineBetweenUsingDirectivesWithDifferentRootNamespace:
            {
                CodeAction codeAction = CodeAction.Create(
                    CodeFixTitles.AddEmptyLine,
                    ct => CodeFixHelpers.AppendEndOfLineAsync(document, trivia.Token, ct),
                    GetEquivalenceKey(diagnostic));

                context.RegisterCodeFix(codeAction, diagnostic);
                break;
            }

            case DiagnosticIdentifiers.RemoveEmptyLineBetweenSingleLineAccessors:
            case DiagnosticIdentifiers.RemoveEmptyLineBetweenUsingDirectivesWithDifferentRootNamespace:
            case DiagnosticIdentifiers.RemoveEmptyLineBetweenUsingDirectivesWithSameRootNamespace:
            {
                CodeAction codeAction = CodeAction.Create(
                    CodeFixTitles.RemoveEmptyLine,
                    ct => CodeFixHelpers.RemoveEmptyLinesBeforeAsync(document, trivia.Token, ct),
                    GetEquivalenceKey(diagnostic));

                context.RegisterCodeFix(codeAction, diagnostic);
                break;
            }

            case DiagnosticIdentifiers.RemoveNewLineBeforeBaseList:
            case DiagnosticIdentifiers.RemoveNewLineBetweenClosingBraceAndWhileKeyword:
            case DiagnosticIdentifiers.RemoveNewLineBetweenIfKeywordAndElseKeyword:
            {
                CodeAction codeAction = CodeAction.Create(
                    CodeFixTitles.RemoveNewLine,
                    ct => CodeFixHelpers.ReplaceTriviaBetweenAsync(document, trivia.Token, trivia.Token.GetNextToken(), cancellationToken: ct),
                    GetEquivalenceKey(diagnostic));

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

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

            if (!TryFindTrivia(root, context.Span.Start, out SyntaxTrivia trivia, findInsideTrivia: false))
            {
                return;
            }

            switch (diagnostic.Id)
            {
            case DiagnosticIdentifiers.AddEmptyLineBetweenDeclarations:
            case DiagnosticIdentifiers.AddEmptyLineBetweenSingleLineDeclarations:
            case DiagnosticIdentifiers.AddEmptyLineBetweenDeclarationAndDocumentationComment:
            case DiagnosticIdentifiers.AddEmptyLineBetweenSingleLineDeclarationsOfDifferentKind:
            {
                CodeAction codeAction = CodeAction.Create(
                    CodeFixTitles.AddEmptyLine,
                    ct => CodeFixHelpers.AppendEndOfLineAsync(document, trivia.Token, ct),
                    GetEquivalenceKey(diagnostic));

                context.RegisterCodeFix(codeAction, diagnostic);
                break;
            }

            case DiagnosticIdentifiers.RemoveEmptyLineBetweenSingleLineDeclarationsOfSameKind:
            {
                CodeAction codeAction = CodeAction.Create(
                    CodeFixTitles.RemoveEmptyLine,
                    ct => CodeFixHelpers.RemoveEmptyLinesBeforeAsync(document, trivia.Token, ct),
                    GetEquivalenceKey(diagnostic));

                context.RegisterCodeFix(codeAction, diagnostic);
                break;
            }
            }
        }