internal static SyntaxToken GetNewStartTokenWithAddedPragma(SyntaxToken startToken, TextSpan currentDiagnosticSpan, Diagnostic diagnostic, AbstractSuppressionCodeFixProvider fixer, Func <SyntaxNode, SyntaxNode> formatNode, bool isRemoveSuppression = false) { var trivia = startToken.LeadingTrivia.ToImmutableArray(); SyntaxTrivia insertAfterTrivia; var index = GetPositionForPragmaInsertion(trivia, currentDiagnosticSpan, fixer, isStartToken: true, triviaAtIndex: out insertAfterTrivia); index++; bool needsLeadingEOL; if (index > 0) { needsLeadingEOL = !fixer.IsEndOfLine(insertAfterTrivia); } else if (startToken.FullSpan.Start == 0) { needsLeadingEOL = false; } else { needsLeadingEOL = true; } var pragmaTrivia = !isRemoveSuppression? fixer.CreatePragmaDisableDirectiveTrivia(diagnostic, formatNode, needsLeadingEOL, needsTrailingEndOfLine : true) : fixer.CreatePragmaRestoreDirectiveTrivia(diagnostic, formatNode, needsLeadingEOL, needsTrailingEndOfLine: true); return(startToken.WithLeadingTrivia(trivia.InsertRange(index, pragmaTrivia))); }
private static SyntaxToken GetNewEndToken(SyntaxToken endToken, Diagnostic diagnostic, AbstractSuppressionCodeFixProvider fixer) { ImmutableArray <SyntaxTrivia> trivia; var isEOF = fixer.IsEndOfFileToken(endToken); if (isEOF) { trivia = endToken.LeadingTrivia.ToImmutableArray(); } else { trivia = endToken.TrailingTrivia.ToImmutableArray(); } SyntaxTrivia lastNonEOLTrivia = trivia.LastOrDefault(t => !fixer.IsEndOfLine(t)); // Insert the #pragma restore directive after the last trailing trivia that is not a new line trivia. int index; if (lastNonEOLTrivia == default(SyntaxTrivia)) { index = 0; } else { index = trivia.IndexOf(lastNonEOLTrivia) + 1; } bool needsTrailingEOL; if (index < trivia.Length) { needsTrailingEOL = !fixer.IsEndOfLine(trivia[index]); } else if (isEOF) { needsTrailingEOL = false; } else { needsTrailingEOL = true; } var pragmaRestoreTrivia = fixer.CreatePragmaRestoreDirectiveTrivia(diagnostic, needsTrailingEOL); if (isEOF) { return(endToken.WithLeadingTrivia(trivia.InsertRange(index, pragmaRestoreTrivia))); } else { return(endToken.WithTrailingTrivia(trivia.InsertRange(index, pragmaRestoreTrivia))); } }
internal static SyntaxToken GetNewEndTokenWithAddedPragma( SyntaxToken endToken, TextSpan currentDiagnosticSpan, Diagnostic diagnostic, AbstractSuppressionCodeFixProvider fixer, Func <SyntaxNode, CancellationToken, SyntaxNode> formatNode, bool isRemoveSuppression, CancellationToken cancellationToken) { ImmutableArray <SyntaxTrivia> trivia; var isEOF = fixer.IsEndOfFileToken(endToken); if (isEOF) { trivia = endToken.LeadingTrivia.ToImmutableArray(); } else { trivia = endToken.TrailingTrivia.ToImmutableArray(); } var index = GetPositionForPragmaInsertion(trivia, currentDiagnosticSpan, fixer, isStartToken: false, triviaAtIndex: out var insertBeforeTrivia); bool needsTrailingEOL; if (index < trivia.Length) { needsTrailingEOL = !IsEndOfLineOrHasLeadingEndOfLine(insertBeforeTrivia, fixer); } else if (isEOF) { needsTrailingEOL = false; } else { needsTrailingEOL = true; } var pragmaTrivia = !isRemoveSuppression ? fixer.CreatePragmaRestoreDirectiveTrivia(diagnostic, formatNode, needsLeadingEndOfLine : true, needsTrailingEndOfLine : needsTrailingEOL, cancellationToken) : fixer.CreatePragmaDisableDirectiveTrivia(diagnostic, formatNode, needsLeadingEndOfLine: true, needsTrailingEndOfLine: needsTrailingEOL, cancellationToken); if (isEOF) { return(endToken.WithLeadingTrivia(trivia.InsertRange(index, pragmaTrivia))); } else { return(endToken.WithTrailingTrivia(trivia.InsertRange(index, pragmaTrivia))); } }
internal static SyntaxToken GetNewStartTokenWithAddedPragma(SyntaxToken startToken, TextSpan currentDiagnosticSpan, Diagnostic diagnostic, AbstractSuppressionCodeFixProvider fixer, Func<SyntaxNode, SyntaxNode> formatNode, bool isRemoveSuppression = false) { var trivia = startToken.LeadingTrivia.ToImmutableArray(); SyntaxTrivia insertAfterTrivia; var index = GetPositionForPragmaInsertion(trivia, currentDiagnosticSpan, fixer, isStartToken: true, triviaAtIndex: out insertAfterTrivia); index++; bool needsLeadingEOL; if (index > 0) { needsLeadingEOL = !IsEndOfLineOrHasTrailingEndOfLine(insertAfterTrivia, fixer); } else if (startToken.FullSpan.Start == 0) { needsLeadingEOL = false; } else { needsLeadingEOL = true; } var pragmaTrivia = !isRemoveSuppression ? fixer.CreatePragmaDisableDirectiveTrivia(diagnostic, formatNode, needsLeadingEOL, needsTrailingEndOfLine: true) : fixer.CreatePragmaRestoreDirectiveTrivia(diagnostic, formatNode, needsLeadingEOL, needsTrailingEndOfLine: true); return startToken.WithLeadingTrivia(trivia.InsertRange(index, pragmaTrivia)); }
internal static SyntaxToken GetNewEndTokenWithAddedPragma(SyntaxToken endToken, TextSpan currentDiagnosticSpan, Diagnostic diagnostic, AbstractSuppressionCodeFixProvider fixer, Func<SyntaxNode, SyntaxNode> formatNode, bool isRemoveSuppression = false) { ImmutableArray<SyntaxTrivia> trivia; var isEOF = fixer.IsEndOfFileToken(endToken); if (isEOF) { trivia = endToken.LeadingTrivia.ToImmutableArray(); } else { trivia = endToken.TrailingTrivia.ToImmutableArray(); } SyntaxTrivia insertBeforeTrivia; var index = GetPositionForPragmaInsertion(trivia, currentDiagnosticSpan, fixer, isStartToken: false, triviaAtIndex: out insertBeforeTrivia); bool needsTrailingEOL; if (index < trivia.Length) { needsTrailingEOL = !IsEndOfLineOrHasLeadingEndOfLine(insertBeforeTrivia, fixer); } else if (isEOF) { needsTrailingEOL = false; } else { needsTrailingEOL = true; } var pragmaTrivia = !isRemoveSuppression ? fixer.CreatePragmaRestoreDirectiveTrivia(diagnostic, formatNode, needsLeadingEndOfLine: true, needsTrailingEndOfLine: needsTrailingEOL) : fixer.CreatePragmaDisableDirectiveTrivia(diagnostic, formatNode, needsLeadingEndOfLine: true, needsTrailingEndOfLine: needsTrailingEOL); if (isEOF) { return endToken.WithLeadingTrivia(trivia.InsertRange(index, pragmaTrivia)); } else { return endToken.WithTrailingTrivia(trivia.InsertRange(index, pragmaTrivia)); }; }
private static SyntaxToken GetNewEndToken(SyntaxToken endToken, Diagnostic diagnostic, AbstractSuppressionCodeFixProvider fixer) { ImmutableArray<SyntaxTrivia> trivia; var isEOF = fixer.IsEndOfFileToken(endToken); if (isEOF) { trivia = endToken.LeadingTrivia.ToImmutableArray(); } else { trivia = endToken.TrailingTrivia.ToImmutableArray(); } SyntaxTrivia lastNonEOLTrivia = trivia.LastOrDefault(t => !fixer.IsEndOfLine(t)); // Insert the #pragma restore directive after the last trailing trivia that is not a new line trivia. int index; if (lastNonEOLTrivia == default(SyntaxTrivia)) { index = 0; } else { index = trivia.IndexOf(lastNonEOLTrivia) + 1; } bool needsTrailingEOL; if (index < trivia.Length) { needsTrailingEOL = !fixer.IsEndOfLine(trivia[index]); } else if (isEOF) { needsTrailingEOL = false; } else { needsTrailingEOL = true; } var pragmaRestoreTrivia = fixer.CreatePragmaRestoreDirectiveTrivia(diagnostic, needsTrailingEOL); if (isEOF) { return endToken.WithLeadingTrivia(trivia.InsertRange(index, pragmaRestoreTrivia)); } else { return endToken.WithTrailingTrivia(trivia.InsertRange(index, pragmaRestoreTrivia)); } }