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); if (!TryFindToken(root, context.Span.Start, out SyntaxToken token)) { return; } Document document = context.Document; Diagnostic diagnostic = context.Diagnostics[0]; switch (diagnostic.Id) { case DiagnosticIdentifiers.AddEmptyLineBetweenBlockAndStatement: { CodeAction codeAction = CodeAction.Create( CodeFixTitles.AddEmptyLine, ct => CodeFixHelpers.AppendEndOfLineAsync(document, token, ct), GetEquivalenceKey(diagnostic)); context.RegisterCodeFix(codeAction, diagnostic); break; } case DiagnosticIdentifiers.AddNewLineBeforeConditionalOperatorInsteadOfAfterItOrViceVersa: { if (DiagnosticProperties.ContainsInvert(diagnostic.Properties)) { var conditionalExpression = (ConditionalExpressionSyntax)token.Parent; string title = null; if (token.IsKind(SyntaxKind.QuestionToken)) { title = (SyntaxTriviaAnalysis.IsTokenFollowedWithNewLineAndNotPrecededWithNewLine(conditionalExpression.WhenTrue, conditionalExpression.ColonToken, conditionalExpression.WhenFalse)) ? "Add newline after '?' and ':' instead of before it" : "Add newline after '?' instead of before it"; } else { title = "Add newline after ':' instead of before it"; } CodeAction codeAction = CodeAction.Create( title, ct => AddNewLineAfterConditionalOperatorInsteadOfBeforeItAsync(document, conditionalExpression, ct), GetEquivalenceKey(diagnostic)); context.RegisterCodeFix(codeAction, diagnostic); } else { var conditionalExpression = (ConditionalExpressionSyntax)token.Parent; string title = null; if (token.IsKind(SyntaxKind.QuestionToken)) { title = (SyntaxTriviaAnalysis.IsTokenFollowedWithNewLineAndNotPrecededWithNewLine(conditionalExpression.WhenTrue, conditionalExpression.ColonToken, conditionalExpression.WhenFalse)) ? "Add newline before '?' and ':' instead of after it" : "Add newline before '?' instead of after it"; } else { title = "Add newline before ':' instead of after it"; } CodeAction codeAction = CodeAction.Create( title, ct => AddNewLineBeforeConditionalOperatorInsteadOfAfterItAsync(document, conditionalExpression, ct), GetEquivalenceKey(diagnostic)); context.RegisterCodeFix(codeAction, diagnostic); } break; } case DiagnosticIdentifiers.AddNewLineBeforeExpressionBodyArrowInsteadOfAfterItOrViceVersa: case DiagnosticIdentifiers.AddNewLineBeforeEqualsSignInsteadOfAfterItOrViceVersa: { AddNewLineBeforeOrAfter(); break; } case DiagnosticIdentifiers.AddNewLineAfterAttributeList: { CodeAction codeAction = CodeAction.Create( CodeFixTitles.AddNewLine, ct => CodeFixHelpers.AddNewLineBeforeAsync(document, token, ct), GetEquivalenceKey(diagnostic)); context.RegisterCodeFix(codeAction, diagnostic); break; } case DiagnosticIdentifiers.AddNewLineBetweenClosingBraceAndWhileKeywordOrViceVersa: { if (DiagnosticProperties.ContainsInvert(diagnostic.Properties)) { CodeAction codeAction = CodeAction.Create( CodeFixTitles.RemoveNewLine, ct => CodeFixHelpers.ReplaceTriviaBetweenAsync(document, token, token.GetNextToken(), cancellationToken: ct), GetEquivalenceKey(diagnostic)); context.RegisterCodeFix(codeAction, diagnostic); } else { CodeAction codeAction = CodeAction.Create( CodeFixTitles.AddNewLine, ct => CodeFixHelpers.AddNewLineBeforeAsync(document, token, ct), GetEquivalenceKey(diagnostic)); context.RegisterCodeFix(codeAction, diagnostic); } break; } } void AddNewLineBeforeOrAfter() { if (DiagnosticProperties.ContainsInvert(diagnostic.Properties)) { CodeAction codeAction = CodeAction.Create( $"Add newline after '{token}' instead of before it", ct => CodeFixHelpers.AddNewLineAfterInsteadOfBeforeAsync(document, token, ct), GetEquivalenceKey(diagnostic)); context.RegisterCodeFix(codeAction, diagnostic); } else { CodeAction codeAction = CodeAction.Create( $"Add newline before '{token}' instead of after it", ct => CodeFixHelpers.AddNewLineBeforeInsteadOfAfterAsync(document, token, ct), GetEquivalenceKey(diagnostic)); context.RegisterCodeFix(codeAction, diagnostic); } } }
public 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.AddBlankLineAfterTopComment: { CodeAction codeAction = CodeAction.Create( CodeFixTitles.AddBlankLine, ct => document.ReplaceTokenAsync(trivia.Token, trivia.Token.AppendEndOfLineToLeadingTrivia(), ct), GetEquivalenceKey(diagnostic)); context.RegisterCodeFix(codeAction, diagnostic); break; } case DiagnosticIdentifiers.AddBlankLineBeforeTopDeclaration: case DiagnosticIdentifiers.AddBlankLineBetweenAccessors: { CodeAction codeAction = CodeAction.Create( CodeFixTitles.AddBlankLine, ct => CodeFixHelpers.AppendEndOfLineAsync(document, trivia.Token, ct), GetEquivalenceKey(diagnostic)); context.RegisterCodeFix(codeAction, diagnostic); break; } case DiagnosticIdentifiers.BlankLineBetweenSingleLineAccessors: case DiagnosticIdentifiers.BlankLineBetweenUsingDirectives: { if (DiagnosticProperties.ContainsInvert(diagnostic.Properties)) { CodeAction codeAction = CodeAction.Create( CodeFixTitles.RemoveBlankLine, ct => CodeFixHelpers.RemoveBlankLinesBeforeAsync(document, trivia.Token, ct), GetEquivalenceKey(diagnostic)); context.RegisterCodeFix(codeAction, diagnostic); } else { CodeAction codeAction = CodeAction.Create( CodeFixTitles.AddBlankLine, ct => CodeFixHelpers.AppendEndOfLineAsync(document, trivia.Token, ct), GetEquivalenceKey(diagnostic)); context.RegisterCodeFix(codeAction, diagnostic); } break; } case DiagnosticIdentifiers.RemoveBlankLineBetweenUsingDirectivesWithSameRootNamespace: { CodeAction codeAction = CodeAction.Create( CodeFixTitles.RemoveBlankLine, ct => CodeFixHelpers.RemoveBlankLinesBeforeAsync(document, trivia.Token, ct), GetEquivalenceKey(diagnostic)); context.RegisterCodeFix(codeAction, diagnostic); break; } case DiagnosticIdentifiers.RemoveNewLineBeforeBaseList: 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; } } }