ClangFormat和记事本++集成

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.org

2.在记事本++中安装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 KernelQt项目中的文件。

从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

所有必需的文件都在存档中

格式好!

Source: https://habr.com/ru/post/zh-CN457972/


All Articles