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}')
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.
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.
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.
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.
I believe https://reviews.llvm.org/D50078 would resolve this if merged.
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;
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With