Code formatting for TTree::Branch

Hi,
cross listing my stackoverflow post. I’m wondering if there is a code formatting tool that supports vertical alignment of function arguments as in

  tree->Branch("Regression_Q2_WORST", &Regression_Q2_WORST, "Regression_Q2_WORST/F");
  tree->Branch("Regression_Q2_TRUE",  &Regression_Q2_TRUE,  "Regression_Q2_TRUE/F");
  tree->Branch("Regression_Q2_HI",    &Regression_Q2_HI,    "Regression_Q2_HI/F");
  tree->Branch("Regression_Q2_LO",    &Regression_Q2_LO,    "Regression_Q2_LO/F");

so far I just use tools that don’t change white spaces after the first non-white space character (vim auto-indent) and do the vertical alignment by hand. For trailing comments and assignment operators I’ve seen vertical alignment in clang-format though.

Cheers,
Paul

Hi Paul,

I am not sure one can achieve that with clang-format: did you try? I take the opportunity to advertise a new reference for the ROOT coding style clang-format configuration file: https://root.cern.ch/coding-conventions#ClangFormat

Cheers,
D

Tried yes, managed no. Though I’m not sure if that’s my inability to configure clang-format correctly or if comma-alignment is just not available with clang-format.
ps: thanks for the pointer to the clang-format settings for the coding conventions.

If you’ve got just one non-struct pod variable in the branch, you can simply omit the third parameter.

So:
tree->Branch("Regression_Q2_WORST", &Regression_Q2_WORST, "Regression_Q2_WORST/F");

can become:
tree->Branch("Regression_Q2_WORST", &Regression_Q2_WORST);

That doesn’t solve your problem but reduces it by 50%. You could also use a macro if you want to type Regression_Q2_WORST just once (beware of macros!).

Bonus: if you change the type of Regression_Q2_WORST to e.g. Double_t, the code will still be correct. No need to change /F to the new type.

If you want to use clang-format, surround your code with

// clang-format off
... code ...
// clang-format on

and clang-format will leave it alone.

1 Like

thanks for the additions,
I will probably not use macros to keep the code readable and maintainable for non-macro knowers.
I also mostly clang-format small blocks of code interactively in the editor, so no immediate need to protect code from clang-format, as I wouldn’t run it once the code is aligned properly.

But in any case good to have it on record here, maybe somebody else will pick it up!

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.