Clion sort include statements
Asked Answered
T

1

12

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.

Tusker answered 19/3, 2017 at 1:16 Comment(1)
I know there's a way in IntelliJ IDEA, but I don't know if they ported that functionality to CLion.Matchlock
M
2

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

Maclean answered 16/1, 2020 at 10:34 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.