C++ 在 CLion 中使用 clang-format

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/34648255/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-28 14:24:41  来源:igfitidea点击:

Using clang-format in CLion

c++clangclionclang-format

提问by Klemen Forstneri?

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?

我想在 CLion 中使用 clang-format 格式化我的代码。我已经完成了设置,但没有找到任何允许我运行外部命令来转换代码的设置。这些事情通常在 CLion 中是如何完成的?

回答by Allan Hasegawa

You can use External Toolsin CLion.

您可以在 CLion 中使用外部工具

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

转到File->Settings->Tools->External Tools并单击加号。

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

应该会弹出一个窗口。使用您选择的名称。

For the Tool settingstab I'm using this configuration:

对于Tool settings选项卡,我正在使用此配置:

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

  • Parameters: -i $FileName$

  • Working directory: $FileDir$

  • 程序:(clang-format-3.7您应该在此处使用可执行文件的名称)

  • 参数: -i $FileName$

  • 工作目录: $FileDir$

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

现在,打开你的文件,你可以去Tools->External tools运行上面的配置。它基本上调用clang-format并进行就地格式化。

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

您还可以为其设置自定义键盘映射,只需在“设置”菜单中搜索外部工具的名称即可。

回答by frido

The lates version of CLion 2019.1has native support for ClangFormat.

CLion 2019.1的最新版本原生支持 ClangFormat。

For previous version go to File->Preferences->Pluginsand search for ClangFormatIJ. Install this plugin.

对于以前的版本,请转到File->Preferences->Plugins并搜索ClangFormatIJ. 安装这个插件。

It installs the context menu to invoke locally installed clang-formatfor a file or for a selection. It also set up the key shortcut for 'Reformat current Statement with clang-format' action.

它安装上下文菜单以调用本地安装clang-format的文件或选择。它还为“使用 clang-format 重新格式化当前语句”操作设置了快捷键。

This option works quite well for me in CLion 218.3however, it might be included in an early version as well.

这个选项在CLion 218.3 中对我来说效果很好,但是,它也可能包含在早期版本中。

回答by uta

Since CLion 191 EAP ClangFormat is supported from the box.

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

回答by Chris Frank

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 Watchersplugin.
  • From there you need to go to File->Settings->Tools->File WatchersAnd create a custom template Creating a new file watcher in the clion settings panel
  • I use the following settings to execute clang-formatwhen one of the currently open files is saved. You can tweak these settings to match your needs My clang-format file watcher settings

    • For example, if you wish to execute clang-formatautomatically whenever the file changes (without needing to save) you can enable Advanced Options->Auto-save edited files to trigger the watcher
  • 首先你需要安装File Watchers插件。
  • 从那里你需要去File->Settings->Tools->File Watchers创建一个自定义模板 Creating a new file watcher in the clion settings panel
  • clang-format当保存当前打开的文件之一时,我使用以下设置来执行。您可以调整这些设置以满足您的需求 My clang-format file watcher settings

    • 例如,如果您希望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