public sealed override async Task RegisterCodeFixesAsync(CodeFixContext context) { SyntaxNode root = await context.GetSyntaxRootAsync().ConfigureAwait(false); if (!TryFindFirstAncestorOrSelf(root, context.Span, out BinaryExpressionSyntax binaryExpression)) { return; } Document document = context.Document; Diagnostic diagnostic = context.Diagnostics[0]; if (DiagnosticProperties.ContainsInvert(diagnostic.Properties)) { CodeAction codeAction = CodeAction.Create( $"Add newline after '{binaryExpression.OperatorToken.ToString()}' instead of before it", ct => CodeFixHelpers.AddNewLineAfterInsteadOfBeforeAsync(document, binaryExpression.Left, binaryExpression.OperatorToken, binaryExpression.Right, ct), GetEquivalenceKey(diagnostic)); context.RegisterCodeFix(codeAction, diagnostic); } else { CodeAction codeAction = CodeAction.Create( $"Add newline before '{binaryExpression.OperatorToken.ToString()}' instead of after it", ct => CodeFixHelpers.AddNewLineBeforeInsteadOfAfterAsync(document, binaryExpression.Left, binaryExpression.OperatorToken, binaryExpression.Right, ct), GetEquivalenceKey(diagnostic)); context.RegisterCodeFix(codeAction, diagnostic); } }
public override async Task RegisterCodeFixesAsync(CodeFixContext context) { SyntaxNode root = await context.GetSyntaxRootAsync().ConfigureAwait(false); if (!TryFindFirstAncestorOrSelf(root, context.Span, out ConditionalAccessExpressionSyntax conditionalAccess)) { return; } Document document = context.Document; Diagnostic diagnostic = context.Diagnostics[0]; if (diagnostic.Properties.TryGetValue(nameof(NewLinePosition), out string newLinePositionRaw) && Enum.TryParse(newLinePositionRaw, out NewLinePosition newLinePosition) && newLinePosition == NewLinePosition.After) { CodeAction codeAction = CodeAction.Create( $"Place '{conditionalAccess.OperatorToken.ToString()}' on the previous line", ct => CodeFixHelpers.AddNewLineAfterInsteadOfBeforeAsync( document, conditionalAccess.Expression, conditionalAccess.OperatorToken, conditionalAccess.WhenNotNull, ct), GetEquivalenceKey(diagnostic)); context.RegisterCodeFix(codeAction, diagnostic); } else { CodeAction codeAction = CodeAction.Create( $"Place '{conditionalAccess.OperatorToken.ToString()}' on the next line", ct => CodeFixHelpers.AddNewLineBeforeInsteadOfAfterAsync( document, conditionalAccess.Expression, conditionalAccess.OperatorToken, conditionalAccess.WhenNotNull, ct), GetEquivalenceKey(diagnostic)); context.RegisterCodeFix(codeAction, diagnostic); } }
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.AddNewLineBeforeConditionalOperatorInsteadOfAfterIt: { 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.AddNewLineAfterConditionalOperatorInsteadOfBeforeIt: { 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); break; } case DiagnosticIdentifiers.AddNewLineAfterExpressionBodyArrowInsteadOfBeforeIt: { CodeAction codeAction = CodeAction.Create( "Add newline after '=>' instead of before it", ct => { var expressionBody = (ArrowExpressionClauseSyntax)token.Parent; return(CodeFixHelpers.AddNewLineAfterInsteadOfBeforeAsync(document, token.GetPreviousToken(), token, expressionBody.Expression, ct)); }, GetEquivalenceKey(diagnostic)); context.RegisterCodeFix(codeAction, diagnostic); break; } case DiagnosticIdentifiers.AddNewLineBeforeExpressionBodyArrowInsteadOfAfterIt: { CodeAction codeAction = CodeAction.Create( "Add newline before '=>' instead of after it", ct => { var expressionBody = (ArrowExpressionClauseSyntax)token.Parent; return(CodeFixHelpers.AddNewLineBeforeInsteadOfAfterAsync(document, token.GetPreviousToken(), token, expressionBody.Expression, ct)); }, GetEquivalenceKey(diagnostic)); context.RegisterCodeFix(codeAction, diagnostic); break; } case DiagnosticIdentifiers.AddNewLineAfterAttributeList: { CodeAction codeAction = CodeAction.Create( CodeFixTitles.AddNewLine, ct => CodeFixHelpers.AddNewLineBeforeAsync(document, token, 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); } } }