[clang] [clang-format] Respect ColumnLimit while aligning multiline expressions (PR #163863)
via cfe-commits
cfe-commits at lists.llvm.org
Fri Oct 17 15:45:03 PDT 2025
================
@@ -552,37 +553,76 @@ static unsigned AlignTokens(const FormatStyle &Style, F &&Matches,
// If there is more than one matching token per line, or if the number of
// preceding commas, do not match anymore, end the sequence.
if (FoundMatchOnLine || CommasBeforeMatch != CommasBeforeLastMatch) {
- MatchedIndices.push_back(i);
+ MatchedIndices.push_back(I);
AlignCurrentSequence();
}
CommasBeforeLastMatch = CommasBeforeMatch;
FoundMatchOnLine = true;
if (StartOfSequence == 0)
- StartOfSequence = i;
+ StartOfSequence = I;
unsigned ChangeWidthLeft = CurrentChange.StartOfTokenColumn;
unsigned ChangeWidthAnchor = 0;
unsigned ChangeWidthRight = 0;
+ unsigned CurrentChangeWidthRight = 0;
if (RightJustify)
if (ACS.PadOperators)
ChangeWidthAnchor = CurrentChange.TokenLength;
else
ChangeWidthLeft += CurrentChange.TokenLength;
else
- ChangeWidthRight = CurrentChange.TokenLength;
- for (unsigned j = i + 1; j != e && Changes[j].NewlinesBefore == 0; ++j) {
- ChangeWidthRight += Changes[j].Spaces;
+ CurrentChangeWidthRight = CurrentChange.TokenLength;
+ llvm::SmallPtrSet<const FormatToken *, 8> MatchingParensToEncounter;
+ for (unsigned J = I + 1; J != E && (Changes[J].NewlinesBefore == 0 ||
+ !MatchingParensToEncounter.empty());
+ ++J) {
+ const auto &Change = Changes[J];
+ const auto *Tok = Change.Tok;
+
+ if (Tok->MatchingParen) {
+ if (Tok->isOneOf(tok::l_paren, tok::l_brace, tok::l_square,
+ TT_TemplateOpener)) {
+ // If the next token is on the next line, we probably don't need to
+ // check the following lengths, because it most likely isn't aligned
+ // with the rest.
+ if (J + 1 != E && Changes[J + 1].NewlinesBefore == 0)
+ MatchingParensToEncounter.insert(Tok->MatchingParen);
+ } else {
+ MatchingParensToEncounter.erase(Tok);
+ }
+ }
+
+ if (Change.NewlinesBefore != 0) {
+ ChangeWidthRight = std::max(ChangeWidthRight, CurrentChangeWidthRight);
+ const auto ChangeWidthStart = ChangeWidthLeft + ChangeWidthAnchor;
+ // If the position of the current token is columnwise before the begin
----------------
sstwcw wrote:
The logic does not cover all the cases. We can worry about it later. If the `ContinuationIndentWidth` parameter is large, then the line will start to the right of the operator, but it should not move.
https://github.com/llvm/llvm-project/pull/163863
More information about the cfe-commits
mailing list