πŸ“œ ⬆️ ⬇️

ClangFormat and Notepad ++ integration

ClangFormat is one of the best tools for automatic formatting of source codes in C, C ++, Java, JavaScript, Objective-C, C # languages. There are plug-ins for popular development environments (IDE), but often you need to quickly format a file or part of a file with source code without launching a cumbersome IDE, experiment with formatting settings and different versions of ClangFormat with the ability to quickly cancel 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 how to integrate with Vim, Emacs and some other editors, but there is no information on integration with Notepad ++. The following is a simple guide for Notepad ++ (for Windows).

Initial requirements



Installation and Setup


1. If Notepad ++ is not installed yet, 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, on the basis of which Notepad ++ is written.

Plugins –> Plugin Admin... –> NppExec –> Install
Notepad ++ will restart, after which the <Notepad++>/plugins/NppExec/ and the Plugins –> NppExec menu item will Plugins –> NppExec
')

3. Download the executable file ClangFormat


To do this, on the page https://llvm.org/builds/ we find and download the installation file for Windows, for example LLVM-XXX-rYYYYYY-win64.exe . The whole package can not be installed, it is enough to extract the file clang-format.exe . You can use 7-zip: delete the .exe extension from the .exe , open the file with 7-zip and extract the clang-format.exe file from the bin/ subdirectory bin/ . We place the file clang-format.exe in the <Notepad++>/plugins/NppExec/clang-format/ directory.

4. Add configuration files for ClangFormat


Configuration files must be named .clang-format or _clang-format . They contain a set of rules (styles) of formatting, the format of which is described in the ClangFormat Style Options manual.

For example, use files from Linux Kernel and Qt projects.

The .clang-format files downloaded from GitHub are placed in the appropriate directories:
<Notepad++>/plugins/NppExec/clang-format/LINUX_KERNEL/
<Notepad++>/plugins/NppExec/clang-format/QT/


5. Create a script for NppExec


Open the window for editing and running scripts NppExec Plugins –> NppExec –> Execute... or press F6 . Copy and paste the script text below into the window and save the script called clang-format with the Save... button.

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 launching the script, based on the selected formatting style specified in the style variable, selects the required .clang-format file, checks the date of its change and, if necessary, copies it to a temporary directory. There, the selected fragment of the source code is copied to the temporary file, after which clang-format.exe . The formatted fragment is copied back to the Notepad ++ window. Then the temporary file is deleted.

In ClangFormat, it is not possible to specify the path to the .clang-format configuration file. ClangFormat will search for it in the directory of the formatted file and, if it does not find it, will go to the search in the parent directories. After the script is .clang-format file will remain in the temporary directory in order not to copy it every time you start formatting.

At all stages of the script, an error check is performed, 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... , select the name of the clang-format script in the drop-down list of the Associated script and click on the Add/Modify button.

Restart Notepad ++, after which the menu item Plugins –> NppExec –> clang-format will appear.

image

7. In Notepad ++, we configure the keyboard shortcut to run the script.


We will use the Ctrl + I combination by analogy with QtCreator. Open Settings –> Shortcut Mapper .

By default, the Ctrl + I combination is busy, so you need to release it with the Clear button in the Main Menu tab (line 38 Split Lines in the current version of Notepad ++). After that, in the tab Plugin commands assign the Ctrl + I combination to the clang-format script.

Done, we can use!

Instructions for use


Open the file with the source code in Notepad ++, select the desired fragment or all the text and press Ctrl + I If nothing is selected, the current line will be formatted.

image

To undo the changes, use the standard editor tools ( Ctrl + Z ).
To change the formatting rules, edit the .clang-format configuration files in the <Notepad++>/plugins/NppExec/clang-format/ directory <Notepad++>/plugins/NppExec/clang-format/
If you need to use another 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, press F6 and in the text of the clang-format script, select the desired style by uncommenting one of the lines
set style = STYLE_NAME .

All necessary files are in the archive .

Successful formatting!

Source: https://habr.com/ru/post/457972/


All Articles