Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can clang-format align variable or macro assignments in columns?

Is it possible to have clang-format align variable assignments in columns? For example:

int someInteger             = 42; std::string someString      = "string"; const unsigned someUnsigned = 42;  #define SOME_INTEGER        42 #define SOME_STRING_LITERAL "string" #define SOME_CONSTANT       42  enum Enum {     ONE   = 1,     TWO   = 2,     THREE = 3,     FOUR  = 4,     FIVE  = 5,     SIX   = 6,     SEVEN = 7 }; 

is more readable than:

int someInteger = 42; const unsigned someUnsigned = 42; std::string someString = "string";  #define SOME_INTEGER 42 #define SOME_STRING_LITERAL "string" #define SOME_CONSTANT 42  enum Enum {     ONE = 1,     TWO = 2,     THREE = 3,     FOUR = 4,     FIVE = 5,     SIX = 6,     SEVEN = 7 }; 

I realize that it may not be practical for clang-format to always do this, but when code as already been manually formatted like said code, it would be nice for clang-format to leave the formatting in place.

like image 730
syvex Avatar asked Nov 24 '14 17:11

syvex


People also ask

How does clang-format work?

clang-format is a tool to automatically format C/C++/Objective-C code, so that developers don't need to worry about style issues during code reviews. It is highly recommended to format your changed C++ code before opening pull requests, which will save you and the reviewers' time.

How do you customize your 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-format default style?

clang-format file uses YAML format: key1: value1 key2: value2 # A comment. ... The configuration file can consist of several sections each having different Language: parameter denoting the programming language this section of the configuration is targeted at.

What is clang tidy?

clang-tidy is a clang-based C++ “linter” tool. Its purpose is to provide an extensible framework for diagnosing and fixing typical programming errors, like style violations, interface misuse, or bugs that can be deduced via static analysis.


2 Answers

It looks like 3.7 supports something like this (haven't tested yet).

From the docs

AlignConsecutiveAssignments (bool)
If true, aligns consecutive assignments.

This will align the assignment operators of consecutive lines. This will result in formattings like code int aaaa = 12; int b = 23; int ccc = 23; endcode

(sic)

like image 174
Michael Anderson Avatar answered Sep 21 '22 19:09

Michael Anderson


Clang-format does not have any option to do this.

If you want to tell clang-format to leave certain lines alone, you can make it do so with // clang-format off and // clang-format on comments.

like image 30
bames53 Avatar answered Sep 17 '22 19:09

bames53