ClangFormat是自动格式化C,C ++,Java,JavaScript,Objective-C,C#中的源代码的最佳工具之一。 有一些流行的开发环境(IDE)的插件,但是通常您需要快速格式化文件或源文件的一部分而无需启动笨重的IDE,尝试格式化设置和不同版本的ClangFormat并具有快速撤消更改的能力。 为此目的使用控制台版本的ClangFormat是不方便的。 一种可能的解决方案是从文本编辑器调用ClangFormat。
官方站点描述了如何与Vim,Emacs和其他一些集成,但是没有有关与Notepad ++集成的信息。 以下是记事本++(对于Windows)的简单说明。
初始要求
- 使用ClangFormat格式化在记事本++中打开的文件;
- 格式化文件中的所选片段;
- 放弃更改;
- 切换样式(规则集)格式;
- 切换到另一个版本的ClangFormat
- 尽可能使用标准工具,而无需重建ClangFormat且无需为Notepad ++编写新的插件。
安装与设定
1.如果尚未安装Notepad ++,请下载并安装
https://notepad-plus-plus.org2.在记事本++中安装NppExec插件
NppExec允许您从Notepad ++调用第三方应用程序,并与Scintilla库的组件进行交互,并以此为基础编写Notepad ++。
Plugins –> Plugin Admin... –> NppExec –> Install
Notepad ++将重新启动,然后
<Notepad++>/plugins/NppExec/
和
Plugins –> NppExec
菜单项将
Plugins –> NppExec
3.下载ClangFormat可执行文件
为此,请在页面
https://llvm.org/builds/上找到并下载Windows的安装文件,例如
LLVM-XXX-rYYYYYY-win64.exe
。 您不能安装整个软件包,仅可以通过
clang-format.exe
提取
clang-format.exe
文件。 您可以使用7-zip:从
.exe
删除
.exe
扩展名,使用7-zip打开文件,然后从
bin/
子目录中提取
clang-format.exe
。 我们
clang-format.exe
放在
<Notepad++>/plugins/NppExec/clang-format/
目录中。
4.添加ClangFormat的配置文件
配置文件应命名为
.clang-format
或
_clang-format
。 它们包含一组格式规则(样式),其格式在
ClangFormat样式选项指南中进行了描述。
例如,我们使用
Linux Kernel和
Qt项目中的文件。
从GitHub下载的
.clang-format
文件放置在适当的目录中:
<Notepad++>/plugins/NppExec/clang-format/LINUX_KERNEL/
<Notepad++>/plugins/NppExec/clang-format/QT/
5.为NppExec创建脚本
打开用于编辑和运行脚本的窗口NppExec
Plugins –> NppExec –> Execute...
或按
F6
。 将下面的脚本文本复制并粘贴到窗口中,然后使用“
Save...
按钮
Save...
名为
clang-format
的脚本。
NppExec脚本 // Hide console NPP_CONSOLE 0 //------------------------------------------------------------------------------ // Uncomment a line to select a style //set style = LINUX_KERNEL set style = QT //------------------------------------------------------------------------------ //------------------------------------------------------------------------------ set clangformatexe = $(CWD)\plugins\NppExec\clang-format\clang-format.exe set clangformatcfgdir = $(CWD)\plugins\NppExec\clang-format set tmpdir = $(SYS.TEMP) set clangformatcfgfile = $(clangformatcfgdir)\$(style)\.clang-format set srcfiletmp = $(tmpdir)\~src.tmp //------------------------------------------------------------------------------ cmd.exe /c if exist "$(clangformatexe)" (exit 0) else (exit 1) if $(EXITCODE) != 0 then NPP_CONSOLE 1 echo ERROR: "$(clangformatexe)" not found exit endif cmd.exe /c if exist "$(clangformatcfgfile)" (exit 0) else (exit 1) if $(EXITCODE) != 0 then NPP_CONSOLE 1 echo ERROR: "$(clangformatcfgfile)" not found exit endif // Copy $(clangformatcfgfile) to $(tmpdir)\.clang-format if their temestamps are different cmd.exe /v /c " for %i in ("$(clangformatcfgfile)") do set date1="%~ti" && for %i in ("$(tmpdir)\.clang-format") do set date2="%~ti" && if not "!date1!"=="!date2!" ( echo !date1! != !date2! && echo COPYING $(clangformatcfgfile) to $(tmpdir)\ && copy "$(clangformatcfgfile)" "$(tmpdir)\" /Y )" if $(EXITCODE) != 0 then NPP_CONSOLE 1 echo ERROR copying "$(clangformatcfgfile)" to "$(tmpdir)" exit endif // Get selected text length sci_sendmsg SCI_GETSELTEXT // If nothing is selected - select the current line if $(MSG_RESULT) == 1 then sci_sendmsg SCI_VCHOMEWRAP sci_sendmsg SCI_LINEENDWRAPEXTEND endif // Save selected text to $(srcfiletmp) sel_saveto "$(srcfiletmp)" :a cmd.exe /c if exist "$(srcfiletmp)" (exit 0) else (exit 1) if $(EXITCODE) != 0 then NPP_CONSOLE 1 echo ERROR: "$(srcfiletmp)" not found exit endif // Run ClangFormat $(clangformatexe) -i -style=file "$(srcfiletmp)" if $(EXITCODE) != 0 then NPP_CONSOLE 1 echo ERROR running "$(clangformatexe)" exit endif // Replace selected text with $(srcfiletmp) sel_loadfrom "$(srcfiletmp)" // Delete $(srcfiletmp) file cmd.exe /c del "$(srcfiletmp)"
启动脚本后,根据
style
变量中指定的所选格式设置样式,选择所需的
.clang-format
文件,检查其更改日期,并在必要时将其复制到临时目录。 在那里,将源代码的选定片段复制到临时文件,然后
clang-format.exe
。 格式化的片段将复制回Notepad ++窗口。 然后删除临时文件。
在ClangFormat中,无法指定
.clang-format
配置文件的路径。 ClangFormat将在格式化文件的目录中搜索它,如果找不到它,它将在父目录中搜索。 脚本
.clang-format
后
.clang-format
文件将保留在临时目录中,以便在每次格式化时都不会复制该文件。
在脚本的所有阶段,都会检查错误,并在发生错误时打开NppExec控制台窗口,其中显示一条消息。
6.在记事本++中,添加新菜单项以运行脚本
打开“
Plugins –> NppExec –> Advanced Options...
,在“
Associated script
下拉列表中,选择
clang-format
脚本名称,然后单击“
Add/Modify
按钮。
我们重新启动Notepad ++,此后菜单项
Plugins –> NppExec –> clang-format
将出现。

7.在记事本++中,配置用于运行脚本的键盘快捷键
我们将类推
Ctrl + I
与QtCreator结合使用。 打开
Settings –> Shortcut Mapper
。
默认情况下,
Ctrl + I
组合键很忙,因此您需要使用“
Main Menu
选项卡中的“
Clear
按钮将其释放(当前版本的Notepad ++中的第38行“
Split Lines
”)。 之后,在“
Plugin commands
选项卡中,将
Ctrl + I
组合分配给
clang-format
脚本。
完成,我们可以使用它!
使用说明
在记事本++中打开源文件,选择所需的片段或所有文本,然后按
Ctrl + I
如果未选择任何内容,则将格式化当前行。

要撤消更改,请使用标准编辑器工具(
Ctrl + Z
)。
要更改格式设置规则,请在
<Notepad++>/plugins/NppExec/clang-format/
目录中编辑
.clang-format
配置文件。
如果需要使用其他版本的ClangFormat,请在脚本中将路径更改为可执行文件
set clangformat = "\path\to\clang-format.exe"
要选择其他格式,请按
F6
键,然后在
clang-format
脚本的文本中通过取消注释其中一行来选择所需的样式。
set style = STYLE_NAME
。
所有必需的文件都在
存档中 。
格式好!