Skip to content

Draft: clang-format formatter plugin

Daniel Mensinger requested to merge dmensinger/kdevelop:libFormat into master

A new source code formatter plugin based on clang-format / LibFormatt.

Current functionality

  • (re)formatting source code
  • using the predefined LLVM / WebKit / etc. styles
  • creating custom formatter styles
  • GUI for editing custom styles
    • almost all options supported
    • supports exporting / importing .clang-format files
  • supported languages:
    • C / C++
    • ObjectiveC
    • Java
    • Javascript

About the style edit dialog...

clang-format has a lot of style options. The main problem is that these options are all stored in one (well, technically two) C struct. There is no interface for getting a list of all supported options with some metadata (name, description, data type, etc.), which would make generating a GUI easy. Additionally, this struct changes with different clang/LLVM versions. This makes hardcoding the GUI impossible without template metaprogramming and/or excessive use of #if blocks. Naturally, the plugin would also have to be constantly updated with new LLVM versions.

That's why I have decided to write a small (~1500 lines of code) code generator for generating the GUI and mapping it to the clang-format config struct. The generator uses the clang C API, that is also used for the KDevelop C++ background parser (so no new dependencies and no fragile parsing of C++ code with std::regex).

As a result, all options (except for those with the type vector<CustomStruct>) that the local LibFormat library supports can be set in the GUI.

Edited by Milian Wolff

Merge request reports