コード例 #1
0
        private static Task <Document> AddEmptyLineAfterRegionDirectiveAndBeforeEndRegionAsync(
            Document document,
            DirectiveTriviaSyntax directiveTrivia,
            CancellationToken cancellationToken)
        {
            switch (directiveTrivia.Kind())
            {
            case SyntaxKind.RegionDirectiveTrivia:
                return(CodeFixHelpers.AddEmptyLineAfterDirectiveAsync(document, directiveTrivia, cancellationToken));

            case SyntaxKind.EndRegionDirectiveTrivia:
                return(CodeFixHelpers.AddEmptyLineBeforeDirectiveAsync(document, directiveTrivia, cancellationToken));

            default:
                throw new InvalidOperationException();
            }
        }
        public sealed override async Task RegisterCodeFixesAsync(CodeFixContext context)
        {
            SyntaxNode root = await context.GetSyntaxRootAsync().ConfigureAwait(false);

            if (!TryFindFirstAncestorOrSelf(
                    root,
                    context.Span,
                    out SyntaxNode node,
                    findInsideTrivia: true,
                    predicate: f => f.IsKind(SyntaxKind.UsingDirective, SyntaxKind.RegionDirectiveTrivia, SyntaxKind.EndRegionDirectiveTrivia)))
            {
                return;
            }

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

            switch (node)
            {
            case UsingDirectiveSyntax usingDirective:
            {
                if (context.Span.Start == usingDirective.SpanStart)
                {
                    CodeAction codeAction = CodeAction.Create(
                        CodeFixTitles.AddEmptyLine,
                        ct => AddEmptyLineBeforeUsingDirectiveAsync(document, usingDirective, ct),
                        GetEquivalenceKey(diagnostic));

                    context.RegisterCodeFix(codeAction, diagnostic);
                }
                else
                {
                    CodeAction codeAction = CodeAction.Create(
                        CodeFixTitles.AddEmptyLine,
                        ct => CodeFixHelpers.AppendEndOfLineAsync(document, usingDirective, ct),
                        GetEquivalenceKey(diagnostic));

                    context.RegisterCodeFix(codeAction, diagnostic);
                }

                break;
            }

            case RegionDirectiveTriviaSyntax regionDirective:
            {
                CodeAction codeAction = CodeAction.Create(
                    CodeFixTitles.AddEmptyLine,
                    ct => CodeFixHelpers.AddEmptyLineBeforeDirectiveAsync(document, regionDirective, ct),
                    GetEquivalenceKey(diagnostic));

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

            case EndRegionDirectiveTriviaSyntax endRegionDirective:
            {
                CodeAction codeAction = CodeAction.Create(
                    CodeFixTitles.AddEmptyLine,
                    ct => CodeFixHelpers.AddEmptyLineAfterDirectiveAsync(document, endRegionDirective, ct),
                    GetEquivalenceKey(diagnostic));

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