For quite a long time I use QtCreator as the main and only IDE. These are mainly C ++ projects. However, from the very beginning I was not satisfied with the auto-formatting of the code. The fact is that the code formatting common for C ++ does not suit me at all. Just a couple of days ago, I found on the network a description of the formatting method that I use, and call it "JavaStyle" there. Short example:

#include <iostream> #include <vector> #include <map> typedef std::vector<uint> PathType; typedef std::map<uint,bool> VisType; typedef std::map<uint,std::vector<uint>> RelType; void PrintPath(uint I, PathType P, VisType V, RelType &R) { if (V.find(I) != V.end()) return; P.push_back(I); V[I]=true; for(const auto& x: P) std::cout << ":" << x; std::cout << std::endl; for(const auto& i: R[I]) PrintPath(i,P,V,R); } int main() { VisType Vis; PathType Path; RelType Rel = { {0,{1,5,6}}, {1,{0,2,6}}, {2,{1,3,6}}, {3,{2,4,6}}, {4,{3,5,6}}, {5,{0,4,6}}, {6,{0,1,2,3,4,5}} }; for(auto const& i:Rel) PrintPath(i.first,Path,Vis,Rel); } 

For some time now, the inconvenience of auto-formatting as I need is beginning to get in the way. In the settings of QtCreator, I did not find the method I needed, there the curly brackets are set rigidly on the next line, only you can adjust the indents.

Questions:

  • Is it possible to somehow customize / "train with plugins" QtCreator for the necessary auto formatting?
  • Which other IDEs have similar QtCreator functionality and allow the auto formatting I need?
  • "common for C ++ formatting code" - which one? Offhand their pieces 10 :) - PinkTux
  • The main difference between my style and "all other 10" is that braces are transferred there, thereby artificially stretching the code. I do not like it. - Majestio
  • See the link in my answer, section "Bracket Style Options". "There" is in which of the 12? Everywhere is different :) - PinkTux
  • Yes, there is. And this is it: --style = java / --style = attach / -A2 - Majestio
  • In the creator, is it possible to customize the code style? - JK_Action

1 answer 1

I share the way I use everywhere, regardless of IDE or editor. And everywhere I get the same result. The only requirement for the method is support for running external utilities (and QtCreator can do this).

The method is very simple: launching AStyle with the necessary keys as this external utility.

Plus (but this is already an amateur) - pre-commit hook a la:

 git diff --name-only | while read FILE; do if [[ "$FILE" =~ ^.+\.(c|cpp|h)$ ]]; then astyle -n "$FILE" fi done 

The astyle settings are stored in the global config, so here you can specify only the -n key if necessary (do not create backups).

Naturally, instead of astyle any other utility can be used.

  • So this is post-formatting, isn't it? - Majestio
  • @Majestio, "launch AStyle with the necessary keys as this external utility . " QtCreator documentation . - PinkTux
  • Thanks, I will try. Yesterday I collected QtCreator 4.1.0 and I saw the “Stylizer” in the settings, including AStyle. I'll figure it out if formatting is in the process of typing, and not just saving - it means to a point. - Majestio