Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Clion sort include statements

Tags:

clion

Is there a way to sort my #include statements in Clion? Additionally, can I do this automatically every time I save? I didn't manage to find any such functionality or plugin.

like image 373
Anastasios Andronidis Avatar asked Mar 19 '17 01:03

Anastasios Andronidis


1 Answers

Yes, it is possible with help of clang-format.

File->Settings...->Languages & Framework->C/C++->Clangd->Enable clangs server

clang-format should be installed in your system. Normally it is available in your favourite repository. You can specify the path to it if required

File->Settings...->Tools->clang-format

You have to put .clang-format file into your project root with coding rules. More information you can find on clang-format web site. For example, I am using Google coding rules. My content looks like this:

 Language:   Cpp                                                                                                                                                                                                                                                                             
 BasedOnStyle: Google 

This includes already the include statements sorting. However, there is a choice of other ready-to-use coding styles like LLVM, Mozilla, WebKit, Chromium which you can use and if necessary modify or you can create your own format by providing set of rules you want. The rule you might be interesting in is

SortIncludes (bool)
    If true, clang-format will sort #includes.

Please refer to the clang format documentation here

like image 67
rhaport Avatar answered Nov 16 '22 09:11

rhaport