[all-commits] [llvm/llvm-project] a324fc: clang-format: insert trailing commas into containers.

Martin Probst via All-commits all-commits at lists.llvm.org
Wed Jan 29 04:26:16 PST 2020


  Branch: refs/heads/master
  Home:   https://github.com/llvm/llvm-project
  Commit: a324fcf1ae62d065b957e66a9d2f5c18b6259d27
      https://github.com/llvm/llvm-project/commit/a324fcf1ae62d065b957e66a9d2f5c18b6259d27
  Author: Martin Probst <martin at probst.io>
  Date:   2020-01-29 (Wed, 29 Jan 2020)

  Changed paths:
    M clang/include/clang/Format/Format.h
    M clang/lib/Format/Format.cpp
    M clang/unittests/Format/FormatTest.cpp
    M clang/unittests/Format/FormatTestJS.cpp

  Log Message:
  -----------
  clang-format: insert trailing commas into containers.

Summary:
This change adds an option to insert trailing commas into container
literals. For example, in JavaScript:

    const x = [
      a,
      b,
       ^~~~~ inserted if missing.
    ]

This is implemented as a seperate post-processing pass after formatting
(because formatting might change whether the container literal does or
does not wrap). This keeps the code relatively simple and orthogonal,
though it has the notable drawback that the newly inserted comma is not
taken into account for formatting decisions (e.g. it might exceed the 80
char limit). To avoid exceeding the ColumnLimit, a comma is only
inserted if it fits into the limit.

Trailing comma insertion conceptually conflicts with argument
bin-packing: inserting a comma disables bin-packing, so we cannot do
both. clang-format rejects FormatStyle configurations that do both with
this change.

Reviewers: krasimir, MyDeveloperDay

Subscribers: cfe-commits

Tags: #clang




More information about the All-commits mailing list