[PATCH] D73354: clang-format: insert trailing commas into containers.

Krasimir Georgiev via Phabricator via cfe-commits cfe-commits at lists.llvm.org
Mon Jan 27 03:58:26 PST 2020


krasimir added a comment.

An alternative approach (I'm not endorsing this) that would *in theory* circumvent non-idempotency issue (but would be more fiddly~fiddly to implement and spill across different layers of the formatter):
This is just a rough idea and can have multiple issues in itself. In general the trick is that if we can teach the token penalty computer and the format outputter about this, we should be able to use it together with other formatting decisions.

- add a new synthetic (annotated) token type, something like TT_PossiblyMissingTrailingComma, that would capture the length-0 text range where this could be inserted;
- use the algorithm here to splay it into the annotated token stream;
- mark it so that no newline can be inserted before such a token
- modify the penalty calculation algorithm to penalize for inserting a newline after this token: if we're inserting a newline after this token and if this token was inserted at a position at or past the maximal number of columns, add a PenaltyExcessCharacter for it.
- during outputting, if this token is followed by a newline, output it as `,`; otherwise output it as an empty string or do not output it.


Repository:
  rG LLVM Github Monorepo

CHANGES SINCE LAST ACTION
  https://reviews.llvm.org/D73354/new/

https://reviews.llvm.org/D73354





More information about the cfe-commits mailing list