Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Improve indentation of leading trivia when converting code to move to collection expressions. #69456

Merged
merged 3 commits into from
Aug 10, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -433,33 +433,62 @@ ExpressionSyntax IndentExpression(
if (currentToken == expressionFirstToken)
return currentToken.WithLeadingTrivia(Whitespace(preferredIndentation));

// If a token has any leading whitespace, it must be at the start of a line. Whitespace is
// otherwise always consumed as trailing trivia if it comes after a token.
if (currentToken.LeadingTrivia is [.., (kind: SyntaxKind.WhitespaceTrivia)])
{
// First, figure out how much this token is indented *from the line* the first token was on.
// Then adjust the preferred indentation that amount for this token.
var currentTokenIndentation = GetIndentationStringForToken(currentToken);
var currentTokenPreferredIndentation = currentTokenIndentation.StartsWith(firstTokenOnLineIndentationString)
? preferredIndentation + currentTokenIndentation[firstTokenOnLineIndentationString.Length..]
: preferredIndentation;

// trim off the existing leading whitespace for this token if it has any, then add the new preferred indentation.
var finalLeadingTrivia = currentToken.LeadingTrivia
.Take(currentToken.LeadingTrivia.Count - 1)
.Append(Whitespace(currentTokenPreferredIndentation));

return currentToken.WithLeadingTrivia(finalLeadingTrivia);
}

// Any other token is unchanged.
return currentToken;
return IndentToken(currentToken, preferredIndentation, firstTokenOnLineIndentationString);
});

// Now, once we've indented the expression, attempt to move comments on its containing statement to it.
return TransferComments(parentStatement, updatedExpression, preferredIndentation);
}

SyntaxToken IndentToken(
SyntaxToken token,
string preferredIndentation,
string firstTokenOnLineIndentationString)
{
// If a token has any leading whitespace, it must be at the start of a line. Whitespace is
// otherwise always consumed as trailing trivia if it comes after a token.
if (token.LeadingTrivia is not [.., (kind: SyntaxKind.WhitespaceTrivia)])
return token;

using var _ = ArrayBuilder<SyntaxTrivia>.GetInstance(out var result);

// Walk all trivia (except the final whitespace). If we hit any comments within at the start of a line
// indent them as well.
for (int i = 0, n = token.LeadingTrivia.Count - 1; i < n; i++)
{
var currentTrivia = token.LeadingTrivia[i];
var nextTrivia = token.LeadingTrivia[i + 1];

var afterNewLine = i == 0 || token.LeadingTrivia[i - 1].IsEndOfLine();
if (afterNewLine &&
currentTrivia.IsWhitespace() &&
nextTrivia.IsSingleOrMultiLineComment())
{
result.Add(GetIndentedWhitespaceTrivia(
preferredIndentation, firstTokenOnLineIndentationString, nextTrivia.SpanStart));
}
else
{
result.Add(currentTrivia);
}
}

// Finally, figure out how much this token is indented *from the line* the first token was on.
// Then adjust the preferred indentation that amount for this token.
result.Add(GetIndentedWhitespaceTrivia(
preferredIndentation, firstTokenOnLineIndentationString, token.SpanStart));

return token.WithLeadingTrivia(TriviaList(result));
}

SyntaxTrivia GetIndentedWhitespaceTrivia(string preferredIndentation, string firstTokenOnLineIndentationString, int pos)
{
var positionIndentation = GetIndentationStringForPosition(pos);
return Whitespace(positionIndentation.StartsWith(firstTokenOnLineIndentationString)
? preferredIndentation + positionIndentation[firstTokenOnLineIndentationString.Length..]
: preferredIndentation);
}

static ExpressionSyntax TransferComments(
StatementSyntax parentStatement,
ExpressionSyntax expression,
Expand Down Expand Up @@ -522,9 +551,12 @@ static ExpressionSyntax TransferComments(
}

string GetIndentationStringForToken(SyntaxToken token)
=> GetIndentationStringForPosition(token.SpanStart);

string GetIndentationStringForPosition(int position)
{
var tokenLine = document.Text.Lines.GetLineFromPosition(token.SpanStart);
var indentation = token.SpanStart - tokenLine.Start;
var tokenLine = document.Text.Lines.GetLineFromPosition(position);
var indentation = position - tokenLine.Start;
var indentationString = new IndentationResult(indentation, offset: 0).GetIndentationString(
document.Text, indentationOptions);

Expand Down
Loading