Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to control indent of ternary operator using clang-format?

How do I control the indent of ternary operator in clang-format? I want to have ordinary continuation inedent, e.g.

int foobar = bar ? a
        : b;

Instead I get alignment of operators

int foobar = bar ? a
                 : b;

I already have AlignOperands: false Any ideas?

(full options: -style='{BasedOnStyle: LLVM, TabWidth: 4, IndentWidth: 4, ContinuationIndentWidth: 8, UseTab: Always, AlignAfterOpenBracket: false, BreakBeforeBinaryOperators: All, AlwaysBreakTemplateDeclarations: true, AlignOperands: false, ColumnLimit: 120}')

like image 598
Jochen Avatar asked Aug 23 '16 08:08

Jochen


People also ask

How do I change my clang format?

clang-format supports two ways to provide custom style options: directly specify style configuration in the -style= command line option or use -style=file and put style configuration in the . clang-format or _clang-format file in the project directory.

What is clang tidy?

Clang-Tidy is a clang-based C++ linter tool which provides a shell executable called clang-tidy as the main entry point. It is an extensible framework for diagnosing typical programming errors, or style issues — generally anything which can be detected during static analysis of the code.

How do you use clang formatter?

You can install clang-format and git-clang-format via npm install -g clang-format . To automatically format a file according to Electron C++ code style, run clang-format -i path/to/electron/file.cc . It should work on macOS/Linux/Windows.

Where does clang format look for clang format?

Standalone Tool. clang-format is located in clang/tools/clang-format and can be used to format C/C++/Java/JavaScript/JSON/Objective-C/Protobuf/C# code.


2 Answers

I believe https://reviews.llvm.org/D50078 would resolve this if merged.

like image 54
Steven Lu Avatar answered Sep 28 '22 08:09

Steven Lu


May I suggest this piece of code if you want to go fancy.

int a = 10;
int b = 20;
bool flag = true;

int c =  \
     flag ? 
         a \
        : b;\

std::cout << c << "\n";

Or you can go this way, your choice:

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;
like image 30
Mehroz Mustafa Avatar answered Sep 28 '22 09:09

Mehroz Mustafa