src\Workspaces\SharedUtilitiesAndExtensions\Compiler\CSharp\Formatting\Engine\Trivia\CSharpTriviaFormatter.cs (34)
20private SyntaxTrivia _newLine;
37protected override bool IsWhitespace(SyntaxTrivia trivia)
40protected override bool IsEndOfLine(SyntaxTrivia trivia)
49protected override SyntaxTrivia CreateWhitespace(string text)
52protected override SyntaxTrivia CreateEndOfLine()
62protected override LineColumnRule GetLineColumnRuleBetween(SyntaxTrivia trivia1, LineColumnDelta existingWhitespaceBetween, bool implicitLineBreak, SyntaxTrivia trivia2, CancellationToken cancellationToken)
108var previous = trivia2;
177protected override bool ContainsImplicitLineBreak(SyntaxTrivia trivia)
191private bool IsStartOrEndOfFile(SyntaxTrivia trivia1, SyntaxTrivia trivia2)
205private static bool IsMultilineComment(SyntaxTrivia trivia1)
208private bool TryFormatMultiLineCommentTrivia(LineColumn lineColumn, SyntaxTrivia trivia, out SyntaxTrivia result)
230var firstTrivia = multilineCommentTrivia.First();
241LineColumn lineColumn, SyntaxTrivia trivia, ArrayBuilder<SyntaxTrivia> changes,
249if (TryFormatMultiLineCommentTrivia(lineColumn, trivia, out var newComment))
260LineColumn lineColumn, SyntaxTrivia trivia, ArrayBuilder<TextChange> changes, CancellationToken cancellationToken)
267if (TryFormatMultiLineCommentTrivia(lineColumn, trivia, out var newComment))
276private SyntaxTrivia FormatDocumentComment(LineColumn lineColumn, SyntaxTrivia trivia)
300var newTrivia = singleLineDocumentationCommentExteriorCommentRewriter.VisitTrivia(trivia);
316var newMultiLineTrivia = multiLineDocumentationCommentExteriorCommentRewriter.VisitTrivia(trivia);
322LineColumn lineColumn, SyntaxTrivia trivia, ArrayBuilder<SyntaxTrivia> changes, CancellationToken cancellationToken)
338var formattedTrivia = SyntaxFactory.Trivia((StructuredTriviaSyntax)result.GetFormattedRoot(cancellationToken));
344var docComment = FormatDocumentComment(lineColumn, trivia);
351LineColumn lineColumn, SyntaxTrivia trivia, ArrayBuilder<TextChange> changes, CancellationToken cancellationToken)
372var formattedTrivia = SyntaxFactory.Trivia((StructuredTriviaSyntax)result.GetFormattedRoot(cancellationToken));
376var docComment = FormatDocumentComment(lineColumn, trivia);
385protected override bool LineContinuationFollowedByWhitespaceComment(SyntaxTrivia trivia, SyntaxTrivia nextTrivia)
394protected override bool IsVisualBasicComment(SyntaxTrivia trivia)
src\Workspaces\SharedUtilitiesAndExtensions\Compiler\CSharp\Formatting\Engine\Trivia\CSharpTriviaFormatter.cs (34)
20private SyntaxTrivia _newLine;
37protected override bool IsWhitespace(SyntaxTrivia trivia)
40protected override bool IsEndOfLine(SyntaxTrivia trivia)
49protected override SyntaxTrivia CreateWhitespace(string text)
52protected override SyntaxTrivia CreateEndOfLine()
62protected override LineColumnRule GetLineColumnRuleBetween(SyntaxTrivia trivia1, LineColumnDelta existingWhitespaceBetween, bool implicitLineBreak, SyntaxTrivia trivia2, CancellationToken cancellationToken)
108var previous = trivia2;
177protected override bool ContainsImplicitLineBreak(SyntaxTrivia trivia)
191private bool IsStartOrEndOfFile(SyntaxTrivia trivia1, SyntaxTrivia trivia2)
205private static bool IsMultilineComment(SyntaxTrivia trivia1)
208private bool TryFormatMultiLineCommentTrivia(LineColumn lineColumn, SyntaxTrivia trivia, out SyntaxTrivia result)
230var firstTrivia = multilineCommentTrivia.First();
241LineColumn lineColumn, SyntaxTrivia trivia, ArrayBuilder<SyntaxTrivia> changes,
249if (TryFormatMultiLineCommentTrivia(lineColumn, trivia, out var newComment))
260LineColumn lineColumn, SyntaxTrivia trivia, ArrayBuilder<TextChange> changes, CancellationToken cancellationToken)
267if (TryFormatMultiLineCommentTrivia(lineColumn, trivia, out var newComment))
276private SyntaxTrivia FormatDocumentComment(LineColumn lineColumn, SyntaxTrivia trivia)
300var newTrivia = singleLineDocumentationCommentExteriorCommentRewriter.VisitTrivia(trivia);
316var newMultiLineTrivia = multiLineDocumentationCommentExteriorCommentRewriter.VisitTrivia(trivia);
322LineColumn lineColumn, SyntaxTrivia trivia, ArrayBuilder<SyntaxTrivia> changes, CancellationToken cancellationToken)
338var formattedTrivia = SyntaxFactory.Trivia((StructuredTriviaSyntax)result.GetFormattedRoot(cancellationToken));
344var docComment = FormatDocumentComment(lineColumn, trivia);
351LineColumn lineColumn, SyntaxTrivia trivia, ArrayBuilder<TextChange> changes, CancellationToken cancellationToken)
372var formattedTrivia = SyntaxFactory.Trivia((StructuredTriviaSyntax)result.GetFormattedRoot(cancellationToken));
376var docComment = FormatDocumentComment(lineColumn, trivia);
385protected override bool LineContinuationFollowedByWhitespaceComment(SyntaxTrivia trivia, SyntaxTrivia nextTrivia)
394protected override bool IsVisualBasicComment(SyntaxTrivia trivia)
CodeFixes\Suppression\AbstractSuppressionCodeFixProvider.PragmaHelpers.cs (10)
65private static int GetPositionForPragmaInsertion(ImmutableArray<SyntaxTrivia> triviaList, TextSpan currentDiagnosticSpan, AbstractSuppressionCodeFixProvider fixer, bool isStartToken, out SyntaxTrivia triviaAtIndex)
71bool shouldConsiderTrivia(SyntaxTrivia trivia)
81var trivia = triviaList[index];
112var index = GetPositionForPragmaInsertion(trivia, currentDiagnosticSpan, fixer, isStartToken: true, triviaAtIndex: out var insertAfterTrivia);
136private static bool IsEndOfLineOrHasLeadingEndOfLine(SyntaxTrivia trivia, AbstractSuppressionCodeFixProvider fixer)
142private static bool IsEndOfLineOrHasTrailingEndOfLine(SyntaxTrivia trivia, AbstractSuppressionCodeFixProvider fixer)
148private static bool IsEndOfLineOrContainsEndOfLine(SyntaxTrivia trivia, AbstractSuppressionCodeFixProvider fixer)
163ImmutableArray<SyntaxTrivia> trivia;
174var index = GetPositionForPragmaInsertion(trivia, currentDiagnosticSpan, fixer, isStartToken: false, triviaAtIndex: out var insertBeforeTrivia);
src\Analyzers\Core\Analyzers\RemoveUnnecessarySuppressions\AbstractRemoveUnnecessaryPragmaSuppressionsDiagnosticAnalyzer.cs (19)
157using var _1 = PooledDictionary<string, List<(SyntaxTrivia pragma, bool isDisable)>>.GetInstance(out var idToPragmasMap);
158using var _2 = ArrayBuilder<(SyntaxTrivia pragma, ImmutableArray<string> ids, bool isDisable)>.GetInstance(out var sortedPragmasWithIds);
159using var _3 = PooledDictionary<SyntaxTrivia, bool>.GetInstance(out var pragmasToIsUsedMap);
228PooledDictionary<string, List<(SyntaxTrivia pragma, bool isDisable)>> idToPragmasMap,
229PooledDictionary<SyntaxTrivia, bool> pragmasToIsUsedMap,
230ArrayBuilder<(SyntaxTrivia pragma, ImmutableArray<string> ids, bool isDisable)> sortedPragmasWithIds,
241foreach (var trivia in root.DescendantTrivia(node => node.ContainsDirectives))
510PooledDictionary<string, List<(SyntaxTrivia pragma, bool isDisable)>> idToPragmasMap,
511PooledDictionary<SyntaxTrivia, bool> pragmasToIsUsedMap,
546PooledDictionary<string, List<(SyntaxTrivia pragma, bool isDisable)>> idToPragmasMap,
547PooledDictionary<SyntaxTrivia, bool> pragmasToIsUsedMap)
560SyntaxTrivia? lastEnablePragma = null;
609PooledDictionary<SyntaxTrivia, bool> pragmasToIsUsedMap,
610ArrayBuilder<(SyntaxTrivia pragma, ImmutableArray<string> ids, bool isDisable)> sortedPragmasWithIds,
631PooledDictionary<SyntaxTrivia, bool> pragmasToIsUsedMap,
632ArrayBuilder<(SyntaxTrivia pragma, ImmutableArray<string> ids, bool isDisable)> sortedPragmasWithIds,
678SyntaxTrivia pragma,
679ArrayBuilder<(SyntaxTrivia pragma, ImmutableArray<string> ids, bool isDisable)> sortedPragmasWithIds,
680out SyntaxTrivia togglePragma)
src\Analyzers\Core\CodeFixes\FileHeaders\AbstractFileHeaderCodeFixProvider.cs (4)
58internal static async Task<SyntaxNode> GetTransformedSyntaxRootAsync(ISyntaxFacts syntaxFacts, AbstractFileHeaderHelper fileHeaderHelper, SyntaxTrivia newLineTrivia, Document document, string? fileHeaderTemplate, CancellationToken cancellationToken)
94private static SyntaxNode ReplaceHeader(ISyntaxFacts syntaxFacts, AbstractFileHeaderHelper fileHeaderHelper, SyntaxTrivia newLineTrivia, SyntaxNode root, string expectedFileHeader)
119var triviaLine = triviaList[i];
176private static SyntaxNode AddHeader(ISyntaxFacts syntaxFacts, AbstractFileHeaderHelper fileHeaderHelper, SyntaxTrivia newLineTrivia, SyntaxNode root, string expectedFileHeader)
src\Compilers\Core\Portable\SourceGeneration\GeneratedCodeUtilities.cs (6)
49SyntaxTree tree, Func<SyntaxTrivia, bool> isComment, CancellationToken cancellationToken)
55internal static bool IsGeneratedCode(string? filePath, SyntaxNode root, Func<SyntaxTrivia, bool> isComment)
88private static bool BeginsWithAutoGeneratedComment(SyntaxNode root, Func<SyntaxTrivia, bool> isComment)
94foreach (var trivia in leadingTrivia)
118SyntaxTree tree, Func<SyntaxTrivia, bool> isComment, CancellationToken cancellationToken)
125foreach (var trivia in leadingTrivia)
src\Workspaces\SharedUtilitiesAndExtensions\Compiler\Core\Extensions\SyntaxNodeExtensions.cs (23)
376IEnumerable<SyntaxTrivia> trivia,
377Func<SyntaxTrivia, SyntaxTrivia, CancellationToken, Task<SyntaxTrivia>> computeReplacementAsync,
393IEnumerable<SyntaxTrivia>? trivia,
394Func<SyntaxTrivia, SyntaxTrivia, CancellationToken, Task<SyntaxTrivia>>? computeReplacementTriviaAsync,
405var triviaReplacements = new Dictionary<SyntaxTrivia, SyntaxTrivia>();
453var original = (SyntaxTrivia?)retryAnnotations.GetAnnotations(currentTrivia).SingleOrDefault() ?? currentTrivia;
498if (!triviaReplacements.TryGetValue(original, out var replaced))
547foreach (var trivia in triviaList)
577foreach (var trivia in triviaList.Reverse())
690params SyntaxTrivia[] trivia) where T : SyntaxNode
697return node.WithPrependedLeadingTrivia((IEnumerable<SyntaxTrivia>)trivia);
714IEnumerable<SyntaxTrivia> trivia) where T : SyntaxNode
724params SyntaxTrivia[] trivia) where T : SyntaxNode
731return node.WithAppendedTrailingTrivia((IEnumerable<SyntaxTrivia>)trivia);
748IEnumerable<SyntaxTrivia> trivia) where T : SyntaxNode
758IEnumerable<SyntaxTrivia> leadingTrivia,
759IEnumerable<SyntaxTrivia> trailingTrivia) where T : SyntaxNode
874foreach (var trivia in token.LeadingTrivia)
src\Workspaces\SharedUtilitiesAndExtensions\Compiler\Core\Formatting\TriviaEngine\AbstractTriviaFormatter.cs (45)
41private delegate LineColumnDelta Formatter<T>(LineColumn lineColumn, SyntaxTrivia trivia, ArrayBuilder<T> changes, CancellationToken cancellationToken);
117protected abstract bool IsWhitespace(SyntaxTrivia trivia);
122protected abstract bool IsEndOfLine(SyntaxTrivia trivia);
127protected abstract bool LineContinuationFollowedByWhitespaceComment(SyntaxTrivia previousTrivia, SyntaxTrivia nextTrivia);
134protected abstract bool IsVisualBasicComment(SyntaxTrivia trivia);
170protected abstract SyntaxTrivia CreateWhitespace(string text);
175protected abstract SyntaxTrivia CreateEndOfLine();
180protected abstract LineColumnRule GetLineColumnRuleBetween(SyntaxTrivia trivia1, LineColumnDelta existingWhitespaceBetween, bool implicitLineBreak, SyntaxTrivia trivia2, CancellationToken cancellationToken);
185protected abstract LineColumnDelta Format(LineColumn lineColumn, SyntaxTrivia trivia, ArrayBuilder<SyntaxTrivia> changes, CancellationToken cancellationToken);
190protected abstract LineColumnDelta Format(LineColumn lineColumn, SyntaxTrivia trivia, ArrayBuilder<TextChange> changes, CancellationToken cancellationToken);
195protected abstract bool ContainsImplicitLineBreak(SyntaxTrivia trivia);
240using var _ = ArrayBuilder<SyntaxTrivia>.GetInstance(out var triviaList);
260private static void AddRange(ArrayBuilder<SyntaxTrivia> result, SyntaxTriviaList triviaList)
262foreach (var trivia in triviaList)
289var previousWhitespaceTrivia = default(SyntaxTrivia);
290var previousTrivia = default(SyntaxTrivia);
297SyntaxTrivia trivia;
373SyntaxTrivia trivia1,
375SyntaxTrivia trivia2,
397private LineColumnRule GetOverallLineColumnRuleBetween(SyntaxTrivia trivia1, LineColumnDelta existingWhitespaceBetween, bool implicitLineBreak, SyntaxTrivia trivia2, CancellationToken cancellationToken)
464private void GetTokensAtEdgeOfStructureTrivia(SyntaxTrivia trivia1, SyntaxTrivia trivia2, out SyntaxToken token1, out SyntaxToken token2)
544LineColumn lineColumnBeforeTrivia1, SyntaxTrivia trivia1, LineColumn lineColumnAfterTrivia1, LineColumnDelta existingWhitespaceBetween, SyntaxTrivia trivia2, LineColumnRule rule)
566LineColumn lineColumnBeforeTrivia1, LineColumn lineColumnAfterTrivia1, LineColumnDelta existingWhitespaceBetween, SyntaxTrivia trivia2, LineColumnRule rule)
625private void AddExtraLines(int linesBetweenTokens, ArrayBuilder<SyntaxTrivia> changes)
629using var _ = ArrayBuilder<SyntaxTrivia>.GetInstance(out var lineBreaks);
642private int GetInsertionIndex(ArrayBuilder<SyntaxTrivia> changes)
758ArrayBuilder<SyntaxTrivia> changes)
767ArrayBuilder<SyntaxTrivia> changes)
832private TextSpan GetTextSpan(SyntaxTrivia trivia1, SyntaxTrivia trivia2)
847private bool IsWhitespaceOrEndOfLine(SyntaxTrivia trivia)
852SyntaxTrivia previousTrivia,
853SyntaxTrivia trivia1,
855SyntaxTrivia trivia2)
904protected LineColumn GetLineColumn(LineColumn lineColumn, SyntaxTrivia trivia)
911protected LineColumnDelta GetLineColumnDelta(LineColumn lineColumn, SyntaxTrivia trivia)
936protected int GetExistingIndentation(SyntaxTrivia trivia)
src\Workspaces\SharedUtilitiesAndExtensions\Compiler\Core\Services\FileBannerFacts\AbstractFileBannerFacts.cs (22)
19private readonly Matcher<SyntaxTrivia> _oneOrMoreBlankLines;
24private readonly Matcher<SyntaxTrivia> _bannerMatcher;
29private readonly Matcher<SyntaxTrivia> _fileBannerMatcher;
34Matcher.Single<SyntaxTrivia>(this.SyntaxFacts.IsWhitespaceTrivia, "\\b"));
35var endOfLine = Matcher.Single<SyntaxTrivia>(this.SyntaxFacts.IsEndOfLineTrivia, "\\n");
38var shebangComment = Matcher.Single<SyntaxTrivia>(this.SyntaxFacts.IsShebangDirectiveTrivia, "#!");
39var singleLineComment = Matcher.Single<SyntaxTrivia>(this.SyntaxFacts.IsSingleLineCommentTrivia, "//");
40var multiLineComment = Matcher.Single<SyntaxTrivia>(this.SyntaxFacts.IsMultiLineCommentTrivia, "/**/");
41var singleLineDocumentationComment = Matcher.Single<SyntaxTrivia>(this.SyntaxFacts.IsSingleLineDocCommentTrivia, "///");
42var multiLineDocumentationComment = Matcher.Single<SyntaxTrivia>(this.SyntaxFacts.IsMultiLineDocCommentTrivia, "/** */");
64public ImmutableArray<SyntaxTrivia> GetLeadingBlankLines(SyntaxNode node)
67public ImmutableArray<SyntaxTrivia> GetLeadingBlankLines<TSyntaxNode>(TSyntaxNode node)
81TSyntaxNode node, out ImmutableArray<SyntaxTrivia> strippedTrivia)
84var leadingTriviaToKeep = new List<SyntaxTrivia>(node.GetLeadingTrivia());
94public ImmutableArray<SyntaxTrivia> GetLeadingBannerAndPreprocessorDirectives<TSyntaxNode>(TSyntaxNode node)
109TSyntaxNode node, out ImmutableArray<SyntaxTrivia> strippedTrivia)
123List<SyntaxTrivia> leadingTriviaToStrip, leadingTriviaToKeep;
138leadingTriviaToStrip = new List<SyntaxTrivia>(leadingTrivia.Take(ppIndex + 1));
139leadingTriviaToKeep = new List<SyntaxTrivia>(leadingTrivia.Skip(ppIndex + 1));
143leadingTriviaToKeep = new List<SyntaxTrivia>(leadingTrivia);
164public ImmutableArray<SyntaxTrivia> GetFileBanner(SyntaxNode root)
170public ImmutableArray<SyntaxTrivia> GetFileBanner(SyntaxToken firstToken)