public override TriviaData WithIndentation( int indentation, FormattingContext context, ChainedFormattingRules formattingRules, CancellationToken cancellationToken) { // if tokens are not in different line, there is nothing we can do here if (!this.SecondTokenIsFirstTokenOnLine) { return(this); } // well, we are already in a desired format, nothing to do. return as it is. if (this.Spaces == indentation) { return(this); } // do expansive check // we need to actually format here to find out indentation var list = new TriviaList(_token1.TrailingTrivia, _token2.LeadingTrivia); Contract.ThrowIfFalse(list.Count > 0); if (ContainsSkippedTokensOrText(list)) { // we can't format return(this); } // okay, we need to do expansive calculation to find out actual space between two tokens var trivia = Format(context, formattingRules, this.LineBreaks, indentation, cancellationToken); var triviaString = CreateString(trivia, cancellationToken); ExtractLineAndSpace(triviaString, out var lineBreaks, out var spaces); return(CreateComplexTrivia(lineBreaks, spaces, indentation)); }
private LineColumn FormatTrivia <T>(Formatter <T> formatter, WhitespaceAppender <T> whitespaceAdder, List <T> changes, CancellationToken cancellationToken) { var lineColumn = this.InitialLineColumn; var existingWhitespaceDelta = LineColumnDelta.Default; var previousWhitespaceTrivia = default(SyntaxTrivia); var previousTrivia = default(SyntaxTrivia); var implicitLineBreak = false; var list = new TriviaList(this.Token1.TrailingTrivia, this.Token2.LeadingTrivia); foreach (var trivia in list) { if (trivia.RawKind == 0) { continue; } if (IsWhitespaceOrEndOfLine(trivia)) { if (IsEndOfLine(trivia)) { implicitLineBreak = false; } existingWhitespaceDelta = existingWhitespaceDelta.With( GetLineColumnOfWhitespace( lineColumn, previousTrivia, previousWhitespaceTrivia, existingWhitespaceDelta, trivia)); previousWhitespaceTrivia = trivia; continue; } previousWhitespaceTrivia = default(SyntaxTrivia); lineColumn = FormatFirstTriviaAndWhitespaceAfter( lineColumn, previousTrivia, existingWhitespaceDelta, trivia, formatter, whitespaceAdder, changes, implicitLineBreak, cancellationToken); implicitLineBreak = implicitLineBreak || ContainsImplicitLineBreak(trivia); existingWhitespaceDelta = LineColumnDelta.Default; previousTrivia = trivia; } lineColumn = FormatFirstTriviaAndWhitespaceAfter( lineColumn, previousTrivia, existingWhitespaceDelta, default(SyntaxTrivia), formatter, whitespaceAdder, changes, implicitLineBreak, cancellationToken); return(lineColumn); }
internal Enumerator(TriviaList triviaList) { _list1 = triviaList._list1; _list2 = triviaList._list2; _index = -1; _enumerator = _list1.GetEnumerator(); }
internal Enumerator(TriviaList triviaList) { this.list1 = triviaList.list1; this.list2 = triviaList.list2; this.index = -1; this.enumerator = list1.GetEnumerator(); }
public List <TTrivia> FormatToSyntaxTrivia(CancellationToken cancellationToken) { var changes = ListPool <SyntaxTrivia> .Allocate(); var lineColumn = FormatTrivia(Format, AddWhitespaceTrivia, changes, cancellationToken); // deal with edges // insert empty linebreaks at the begining of trivia list AddExtraLines(lineColumn.Line, changes); if (Succeeded()) { var temp = new List <TTrivia>(changes.Select(Convert)); ListPool <SyntaxTrivia> .Free(changes); return(temp); } ListPool <SyntaxTrivia> .Free(changes); var triviaList = new TriviaList(this.Token1.TrailingTrivia, this.Token2.LeadingTrivia); return(new List <TTrivia>(triviaList.Select(Convert))); }
protected abstract bool ContainsSkippedTokensOrText(TriviaList list);
private LineColumn FormatTrivia <T>(Formatter <T> formatter, WhitespaceAppender <T> whitespaceAdder, ArrayBuilder <T> changes, CancellationToken cancellationToken) { var lineColumn = this.InitialLineColumn; var existingWhitespaceDelta = LineColumnDelta.Default; var previousWhitespaceTrivia = default(SyntaxTrivia); var previousTrivia = default(SyntaxTrivia); var implicitLineBreak = false; var list = new TriviaList(this.Token1.TrailingTrivia, this.Token2.LeadingTrivia); // Holds last position before _ ' Comment so we can reset after processing comment var previousLineColumn = LineColumn.Default; SyntaxTrivia trivia; for (var i = 0; i < list.Count; i++) { trivia = list[i]; if (trivia.RawKind == 0) { continue; } if (IsWhitespaceOrEndOfLine(trivia)) { existingWhitespaceDelta = existingWhitespaceDelta.With( GetLineColumnOfWhitespace( lineColumn, previousTrivia, previousWhitespaceTrivia, existingWhitespaceDelta, trivia)); if (IsEndOfLine(trivia)) { implicitLineBreak = false; // If we are on a new line we don't want to continue // reseting indenting this handles the case of a NewLine // followed by whitespace and a comment previousLineColumn = LineColumn.Default; } else if (LineContinuationFollowedByWhitespaceComment(previousTrivia, (i + 1) < list.Count ? list[i + 1] : default)) { // we have a comment following an underscore space the formatter // thinks this next line should be shifted to right by // indentation value. Since we know through the test above that // this is the special case of _ ' Comment we don't want the extra indent // so we set the LineColumn value back to where it was before the comment previousLineColumn = lineColumn; } previousWhitespaceTrivia = trivia; continue; } previousWhitespaceTrivia = default; lineColumn = FormatFirstTriviaAndWhitespaceAfter( lineColumn, previousTrivia, existingWhitespaceDelta, trivia, formatter, whitespaceAdder, changes, implicitLineBreak, cancellationToken); if (previousLineColumn.Column != 0 && previousLineColumn.Column < lineColumn.Column && IsVisualBasicComment(trivia)) { lineColumn = previousLineColumn; // When we see a NewLine we don't want any special handling // for _ ' Comment previousLineColumn = LineColumn.Default; } implicitLineBreak = implicitLineBreak || ContainsImplicitLineBreak(trivia); existingWhitespaceDelta = LineColumnDelta.Default; previousTrivia = trivia; } lineColumn = FormatFirstTriviaAndWhitespaceAfter( lineColumn, previousTrivia, existingWhitespaceDelta, default,