ClangFormat and Notepad ++ Integration
ClangFormat is one of the best tools for automatically formatting source codes in C, C ++, Java, JavaScript, Objective-C, C #. There are plugins for popular development environments (IDEs), but often you need to quickly format a file or part of a source file without launching a cumbersome IDE, experiment with formatting settings and different versions of ClangFormat with the ability to quickly undo changes. Using the console version of ClangFormat for this purpose is inconvenient. A possible solution is to call ClangFormat from a text editor. The official website describes integration methods with Vim, Emacs, and some others, but there is no information on integration with Notepad ++. The following is a simple instruction for Notepad ++ (for Windows).
https://notepad-plus-plus.org
NppExec allows you to call third-party applications from Notepad ++ and interact with the components of the Scintilla library, based on which Notepad ++ is written.
Notepad ++ will restart, after which the directory
To do this, on the page https://llvm.org/builds/ we find and download the installation file for Windows, for example
Configuration files must have the name
For example, we use files from the Linux Kernel and Qt projects .
The files downloaded from GitHub are
Open the window for editing and running NppExec scripts
After starting the script, on the basis of the selected formatting style specified in the variable
In ClangFormat there is no way to specify the path to the configuration file
At all stages of the script, errors are checked, and when they occur, the NppExec console window opens, in which a message is displayed.
Open
We restart Notepad ++, after which the menu item appears
![image](https://habrastorage.org/webt/mm/zr/0o/mmzr0ohwwuitlc-syqrgfaenwmk.png)
We will use a combination
By default, the combination is
Done, we can use it!
Open the source file in Notepad ++, select the desired fragment or all the text and click
![image](https://habrastorage.org/webt/st/td/7d/sttd7dp75prza067taz-ix4h6m0.gif)
To undo changes, use the standard editor tools (
To change formatting rules, edit the configuration files
If you need to use a different version of ClangFormat, then change the path to the executable file in the script.
To select a different formatting style, click
All necessary files are in the archive .
Good formatting!
Initial requirements
- formatting a file opened in Notepad ++ using ClangFormat;
- formatting the selected fragment in the file;
- discard changes;
- switching style (set of rules) formatting;
- Switching to another version of ClangFormat
- use standard tools whenever possible, without rebuilding ClangFormat and without writing a new plugin for Notepad ++.
Installation and setup
1. If Notepad ++ is not already installed, download and install it
https://notepad-plus-plus.org
2. In Notepad ++ install the NppExec plugin
NppExec allows you to call third-party applications from Notepad ++ and interact with the components of the Scintilla library, based on which Notepad ++ is written.
Plugins –> Plugin Admin... –> NppExec –> Install
Notepad ++ will restart, after which the directory
/plugins/NppExec/
and menu item will appearPlugins –> NppExec
3. Download the ClangFormat executable
To do this, on the page https://llvm.org/builds/ we find and download the installation file for Windows, for example
LLVM-X.X.X-rYYYYYY-win64.exe
. You can not install the whole package, just extract the file with the archiver clang-format.exe
. You can use 7-zip: delete the extension from the file .exe
, open the file with 7-zip and extract the bin/
file from the subdirectory clang-format.exe
. We put the file clang-format.exe
in the directory /plugins/NppExec/clang-format/
.4. Add configuration files for ClangFormat
Configuration files must have the name
.clang-format
or _clang-format
. They contain a set of formatting rules (styles), the format of which is described in the ClangFormat Style Options guide . For example, we use files from the Linux Kernel and Qt projects .
The files downloaded from GitHub are
.clang-format
placed in the appropriate directories:/plugins/NppExec/clang-format/LINUX_KERNEL/
/plugins/NppExec/clang-format/QT/
5. Create a script for NppExec
Open the window for editing and running NppExec scripts
Plugins –> NppExec –> Execute...
or click F6
. Copy and paste the script text below into the window and save the script under the name of the clang-format
button Save...
.NppExec script
// 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)"
After starting the script, on the basis of the selected formatting style specified in the variable
style
, selects the desired file .clang-format
, checks the date of its change and, if necessary, copies it to the temporary directory. There, the selected fragment of the source code is copied to the temporary file, and then it is launched clang-format.exe
. The formatted fragment is copied back to the Notepad ++ window. Then the temporary file is deleted. In ClangFormat there is no way to specify the path to the configuration file
.clang-format
. ClangFormat will search for it in the directory of the formatted file and, if it does not find it, it will go to search in the parent directories. After the script finishes, the file .clang-format
will remain in the temporary directory in order not to copy it each time formatting is started.At all stages of the script, errors are checked, and when they occur, the NppExec console window opens, in which a message is displayed.
6. In Notepad ++, add a new menu item to run the script
Open
Plugins –> NppExec –> Advanced Options...
, Associated script
select the script name in the drop-down list clang-format
and click on the button Add/Modify
. We restart Notepad ++, after which the menu item appears
Plugins –> NppExec –> clang-format
.![image](https://habrastorage.org/webt/mm/zr/0o/mmzr0ohwwuitlc-syqrgfaenwmk.png)
7. In Notepad ++, configure the keyboard shortcut for running the script
We will use a combination
Ctrl + I
similar to QtCreator. We open Settings –> Shortcut Mapper
. By default, the combination is
Ctrl + I
busy, so you need to release it with the button Clear
in the tab Main Menu
(line 38 Split Lines
in the current version of Notepad ++). After that, in the tab we Plugin commands
assign a combination Ctrl + I
to the script clang-format
. Done, we can use it!
Instruction for use
Open the source file in Notepad ++, select the desired fragment or all the text and click
Ctrl + I
. If nothing is selected, the current line will be formatted. ![image](https://habrastorage.org/webt/st/td/7d/sttd7dp75prza067taz-ix4h6m0.gif)
To undo changes, use the standard editor tools (
Ctrl + Z
). To change formatting rules, edit the configuration files
.clang-format
in the directory. /plugins/NppExec/clang-format/
If you need to use a different version of ClangFormat, then change the path to the executable file in the script.
set clangformat = "\path\to\clang-format.exe"
To select a different formatting style, click
F6
and clang-format
select the desired style in the script text by uncommenting one of the lines set style = STYLE_NAME
. All necessary files are in the archive .
Good formatting!