简体   繁体   English

在 CLion 中使用 clang-format

[英]Using clang-format in CLion

I'd like to format my code using clang-format in CLion.我想在 CLion 中使用 clang-format 格式化我的代码。 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?这些事情通常在 CLion 中是如何完成的?

You can use External Tools in CLion.您可以在 CLion 中使用外部工具

Go to File->Settings->Tools->External Tools and click on the plus sign.转到File->Settings->Tools->External Tools ,然后单击加号。

A window should pop up.应该会弹出一个窗口。 Use a name of your choice.使用您选择的名称。

For the Tool settings tab I'm using this configuration:对于Tool settings选项卡,我正在使用此配置:

  • Program: clang-format-3.7 (you should use the name of your executable here)程序: clang-format-3.7 (您应该在此处使用可执行文件的名称)

  • Parameters: -i $FileName$参数: -i $FileName$

  • Working directory: $FileDir$工作目录: $FileDir$

Now, with your file open, you can go to Tools->External tools and run the config above.现在,打开文件后,您可以转到“ Tools->External tools并运行上面的配置。 It basically calls clang-format and does inplace formatting.它基本上调用clang-format并进行就地格式化。

You can also set a custom keymap to it, just search the name of your external tool in "Keymap" of the Settings menu.您还可以为其设置自定义键盘映射,只需在“设置”菜单的“键盘映射”中搜索外部工具的名称即可。

The lates version of CLion 2019.1 has native support for ClangFormat. CLion 2019.1的最新版本原生支持 ClangFormat。

For previous version go to File->Preferences->Plugins and search for ClangFormatIJ .对于以前的版本,转到File->Preferences->Plugins并搜索ClangFormatIJ Install this plugin.安装这个插件。

It installs the context menu to invoke locally installed clang-format for a file or for a selection.它安装上下文菜单以调用本地安装的文件或选择的clang-format It also set up the key shortcut for 'Reformat current Statement with clang-format' action.它还为“使用 clang-format 重新格式化当前语句”操作设置了快捷键。

This option works quite well for me in CLion 218.3 however, it might be included in an early version as well.这个选项在CLion 218.3 中对我来说效果很好,但是,它也可能包含在早期版本中。

The previous answers work well, but do not allow executing clang-format on save without the use of workarounds which don't work with the Vim emulation plugin I am using.以前的答案运行良好,但不允许在不使用不适用于我正在使用的 Vim 仿真插件的变通方法的情况下在保存时执行 clang-format。

Here is a solution which executes clang-format on save and works well in every situation that I have encountered.这是一个解决方案,它在保存时执行 clang-format 并且在我遇到的每种情况下都能很好地工作。

  • First you need to install the File Watchers plugin.首先,您需要安装File Watchers插件。
  • From there you need to go to File->Settings->Tools->File Watchers And create a custom template从那里你需要去File->Settings->Tools->File Watchers并创建一个自定义模板在 clion 设置面板中创建一个新的文件观察器
  • I use the following settings to execute clang-format when one of the currently open files is saved.当保存当前打开的文件之一时,我使用以下设置来执行clang-format You can tweak these settings to match your needs您可以调整这些设置以满足您的需求我的 clang 格式文件观察器设置

    • For example, if you wish to execute clang-format automatically whenever the file changes (without needing to save) you can enable Advanced Options->Auto-save edited files to trigger the watcher例如,如果您希望在文件更改时自动执行clang-format (无需保存),您可以启用Advanced Options->Auto-save edited files to trigger the watcher

Documentation for the File Watchers plugin: https://www.jetbrains.com/help/clion/using-file-watchers.html File Watchers 插件的文档: https : //www.jetbrains.com/help/clion/using-file-watchers.html

由于 CLion 191 EAP ClangFormat支持开箱即用。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM