Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Using clang-format in CLion

I'd like to format my code using clang-format in CLion. I've gone through the settings and haven't found any setting that would allow me to run an external command to transform code. How are such things normally done in CLion?

like image 935
Klemen Forstnerič Avatar asked Jan 07 '16 05:01

Klemen Forstnerič


People also ask

How do I use clang format?

To automatically format a file according to Electron C++ code style, run clang-format -i path/to/electron/file.cc . It should work on macOS/Linux/Windows. The workflow to format your changed code: Make codes changes in Electron repository.

Does clang format work for C?

clang-format is located in clang/tools/clang-format and can be used to format C/C++/Java/JavaScript/JSON/Objective-C/Protobuf/C# code.

How do I enable clang tidy in CLion?

Provide the path to your custom Clang-Tidy binary in Settings/Preferences | Languages & Frameworks | C/C++ (or click Specify Clang-Tidy executable in the Inspections Settings dialog). This setting is IDE-wide: your custom Clang-Tidy binary will be used for all projects.

How do you beautify in CLion?

Press Ctrl+Alt+S to open the IDE settings and select Tools | Actions on Save. Enable the Reformat code option. Additionally, you can configure the way the IDE will reformat your code: Click Configure scope to specify the patterns of filenames and directories that you want to exclude from reformatting.


1 Answers

You can use External Tools in CLion.

Go to File->Settings->Tools->External Tools and click on the plus sign.

A window should pop up. Use a name of your choice.

For the Tool settings tab I'm using this configuration:

  • Program: clang-format-3.7 (you should use the name of your executable here)

  • Parameters: -i $FileName$

  • Working directory: $FileDir$

Now, with your file open, you can go to Tools->External tools and run the config above. It basically calls clang-format and does inplace formatting.

You can also set a custom keymap to it, just search the name of your external tool in "Keymap" of the Settings menu.

like image 113
Allan Hasegawa Avatar answered Sep 20 '22 08:09

Allan Hasegawa