Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to get single line aligned case statements in a switch, using clang-format

I would like clang-format to format like:

switch (x)
{
case long_name: return 1;
case sn:        return 2;
}

The AllowShortCaseLabelsOnASingleLine option gets them on the same line,
but I have not found a way to get the statements aligned.

like image 386
sp2danny Avatar asked Jun 14 '17 09:06

sp2danny


People also ask

How do I change the style of a clang file?

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 alignconsecutivestyle in Clang?

AlignConsecutiveAssignments ( AlignConsecutiveStyle) clang-format 3.8 Style of aligning consecutive assignments. Consecutive will result in formattings like: Do not align assignments on consecutive lines. Align assignments on consecutive lines. This will result in formattings like:

How do I align a reference to the left in Clang?

To configure this in the .clang-format file, use: Reference alignment style (overrides PointerAlignment for references). RAS_Pointer (in configuration: Pointer ) Align reference like PointerAlignment. RAS_Left (in configuration: Left ) Align reference to the left.

What is the style argument used for in Clang-format?

The style argument is used to determine the style rules that clang-format will apply. You can use any of the styles described above, or -style=file to tell clang-format that it must use your .clang-format file. By default, clang-format will display formatting discrepancies as shell output. I prefer to have clang-format update the files directly.


1 Answers

As of right now, it's not possible to do what you're asking using ClangFormat.

The official explanation as to why that is:

Each additional style option adds costs to the clang-format project. Some of these costs affect the clang-format development itself, as we need to make sure that any given combination of options work and that new features don’t break any of the existing options in any way. There are also costs for end users as options become less discoverable and people have to think about and make a decision on options they don’t really care about.

The goal of the clang-format project is more on the side of supporting a limited set of styles really well as opposed to supporting every single style used by a codebase somewhere in the wild. [...]

The only way you may be able to do something like that would be to:

  1. Suggest the style option to the developers;
  2. Wait until the desired style option gets added;
  3. Use an alternative to ClangFormat which is capable of aligning the statements.

Sources:

  1. https://clang.llvm.org/docs/ClangFormatStyleOptions.html#configurable-format-style-options
  2. https://clang.llvm.org/docs/ClangFormatStyleOptions.html#adding-additional-style-options
like image 146
NemPlayer Avatar answered Oct 24 '22 12:10

NemPlayer