taskwarrior icon indicating copy to clipboard operation
taskwarrior copied to clipboard

Start using clang-format to format C++ code

Open djmitche opened this issue 10 months ago • 2 comments

The C++ style and formatting in Taskwarrior is unusual, and difficult to keep up with by hand or in reviews. Let's let computers do the work for us! Ideally clangd-format can be configured to enforce roughly the style used in Taskwarrior, then run once over the codebase and enforced in CI thereafter.

This should make it easy for contributors to bang together some C++, run clangd-format on it, and then make a PR, so we don't need to make review comments about the formatting and also maintain a consistent formatting for the project.

djmitche avatar Apr 05 '24 01:04 djmitche

Nitpick: isn't it clang-format?

Perhaps a separate issue but what would we think about checking in a compile_commands.json file so clangd works out of the box?

ryneeverett avatar Apr 16 '24 01:04 ryneeverett

Oh, good point regarding the spelling!

CMake can actually create compile_commands.json with -DCMAKE_EXPORT_COMPILE_COMMANDS=ON - it's working for me! Maybe documenting that somewhere would be good?

djmitche avatar Apr 16 '24 01:04 djmitche

I looked into it already a little bit. As far as my search suggested we can setup clang-format quite simple with providing the necessary file in the repository. However, in order to have it useful one would need to automate the process of running clang-format, as not every developer is using an IDE, or we do not even know which IDE's are used.

Searching a bit deeper into this direction Stack Overflow Question, I stumpled over the pre-commit framework, which also provides a pre-commit.ci service which would allow us to use e.g. clang-format for the C, C++ code, black for Python files as well as maybe some other linter for the Rust code.

Is this a way we would like to go here?

felixschurk avatar Jul 11 '24 10:07 felixschurk

Yes, I think we will want to enforce this on PRs.

Two important considerations: can the tool be configured to the particular style of Taskwarrior, and do we need to make a huge PR to bring the whole codebase into compliance?

djmitche avatar Jul 11 '24 11:07 djmitche

Yes and yes.

As far as I know one can configure clang-format to be as one likes, however I assume as described in #1987 already pointed out we are quite close to Google Coding Style.

I assume that one big PR with one "format" Commit makes sense. To not lose the git blame options in GitHub I think we can setup this which can also be used for locally using the functionality.

felixschurk avatar Jul 11 '24 12:07 felixschurk

Oh, that ignore functionality is pretty cool! I suppose the next step is just to do it, then?

djmitche avatar Jul 11 '24 17:07 djmitche

Yes, I will do over the next few days.

felixschurk avatar Jul 11 '24 18:07 felixschurk