private static Task <Document> AddBlankLineAfterRegionDirectiveAndBeforeEndRegionAsync( Document document, DirectiveTriviaSyntax directiveTrivia, CancellationToken cancellationToken) { switch (directiveTrivia.Kind()) { case SyntaxKind.RegionDirectiveTrivia: return(CodeFixHelpers.AddBlankLineAfterDirectiveAsync(document, directiveTrivia, cancellationToken)); case SyntaxKind.EndRegionDirectiveTrivia: return(CodeFixHelpers.AddBlankLineBeforeDirectiveAsync(document, directiveTrivia, cancellationToken)); default: throw new InvalidOperationException(); } }
public 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.AddBlankLine, ct => AddBlankLineBeforeUsingDirectiveAsync(document, usingDirective, ct), GetEquivalenceKey(diagnostic)); context.RegisterCodeFix(codeAction, diagnostic); } else { CodeAction codeAction = CodeAction.Create( CodeFixTitles.AddBlankLine, ct => CodeFixHelpers.AppendEndOfLineAsync(document, usingDirective, ct), GetEquivalenceKey(diagnostic)); context.RegisterCodeFix(codeAction, diagnostic); } break; } case RegionDirectiveTriviaSyntax regionDirective: { CodeAction codeAction = CodeAction.Create( CodeFixTitles.AddBlankLine, ct => CodeFixHelpers.AddBlankLineBeforeDirectiveAsync(document, regionDirective, ct), GetEquivalenceKey(diagnostic)); context.RegisterCodeFix(codeAction, diagnostic); break; } case EndRegionDirectiveTriviaSyntax endRegionDirective: { CodeAction codeAction = CodeAction.Create( CodeFixTitles.AddBlankLine, ct => CodeFixHelpers.AddBlankLineAfterDirectiveAsync(document, endRegionDirective, ct), GetEquivalenceKey(diagnostic)); context.RegisterCodeFix(codeAction, diagnostic); break; } } }