OSDN Git Service

clang-format: support aligned nested conditionals formatting
authorFrancois Ferrand <thetypz@gmail.com>
Fri, 15 Jun 2018 12:22:23 +0000 (14:22 +0200)
committerFrancois Ferrand <francois.ferrand@faurecia.com>
Wed, 22 Apr 2020 15:36:33 +0000 (17:36 +0200)
commit5daa25fd7a184524759b6ad065a8bd7e95aa149a
tree299cc02753575c307f22ec3203590395d1ecf080
parentd7ab9e7c9b309ebac094bba209f7c15ad5f01768
clang-format: support aligned nested conditionals formatting

When multiple ternary operators are chained, e.g. like an if/else-if/
else-if/.../else sequence, clang-format will keep aligning the colon
with the question mark, which increases the indent for each
conditionals:

  int a = condition1 ? result1
                     : condition2 ? result2
                                  : condition3 ? result3
                                               : result4;

This patch detects the situation (e.g. conditionals used in false branch
of another conditional), to avoid indenting in that case:

  int a = condition1   ? result1
          : condition2 ? result2
          : condition3 ? result3
                       : result4;

When BreakBeforeTernaryOperators is false, this will format like this:

  int a = condition1 ? result1 :
          condition2 ? result2 :
          conditino3 ? result3 :
                       result4;
clang/lib/Format/ContinuationIndenter.cpp
clang/lib/Format/ContinuationIndenter.h
clang/lib/Format/WhitespaceManager.cpp
clang/lib/Format/WhitespaceManager.h
clang/unittests/Format/FormatTest.cpp