Custom MQL code styling

10 April 2019, 13:42
Stanislav Korotky
9
966

One of important aspects of programming is code styling. Properly formatted code is easy to read, understand, and modify. Moreover unified coding standards become more significant when it comes to incorporation of 3-rd party source codes into a large project with consistent formatting.

MetaEditor provides a built-in code styler, but it lacks style customization. And this is a problem especially because the only available (default) MQL code style is a bit clumsy. After decades of work in the IT, in various software developing companies, I never saw such code style. It's even hard to find such codes in the Internet. 99% of codes are formatted differently, using one of flavours of 2 main styles, let's call them Java-like and C++-like styles.

Since MetaEditor does not allow us to style MQL in a standard (de facto) way, we need to find a workaround. It would be great to have a standalone command-line tool which supports batch file processing.

Fortunately, there is one. This is a clang-format tool coming with LLVM compiler. The complete LLVM installation is a bit large and full of useless stuff for MQL developers, but there exists a more suitable option. On the page with LLVM builds one can find a link to clang-format Visual Studio plugin (at the bottom), specifically ClangFormat-r357435.vsix file (at the time of writing). Download this file, rename it to zip, and expand to a folder.

Then run the command:

clang-format -style=Microsoft -dump-config > .clang-format

This will save styling settings according to Microsoft standards in the file .clang-format. This is a text file, which you can edit according to your preferences. All supported settings are listed on this page. I'm attaching my personal .clang-format file below.

Complete list of command line options for clang-format is available on this page.

It's recommended to place the file .clang-format in the root MQL5 folder - this way it will be reachable (and take effect) by clang-format.exe without any additional tricks, if you run formatting for files in any subfolder of MQL5.

To start code formatting use the command like this:

clang-format -style=file -i *.mqh

This will reformat all header files in current directory. Unfortunately, clang-format does not support nested folders (does not look into subfolders recursively), that is if you need to reformat codes in many subfolders, you need to run the command for every subfolder. Something like this (in a bat-file):

clang-format -style=file -i include/Arrays/*.mqh
clang-format -style=file -i include/ChartObjects/*.mqh
clang-format -style=file -i include/Charts/*.mqh
clang-format -style=file -i include/Controls/*.mqh
clang-format -style=file -i include/Expert/*.mqh
clang-format -style=file -i include/Files/*.mqh
clang-format -style=file -i include/Indicators/*.mqh
clang-format -style=file -i include/Strings/*.mqh
clang-format -style=file -i include/Tools/*.mqh
clang-format -style=file -i include/Trade/*.mqh
clang-format -style=file -i include/Canvas/*.mqh
clang-format -style=file -i include/Generic/*.mqh
clang-format -style=file -i include/Graphics/*.mqh
clang-format -style=file -i include/OpenCL/*.mqh

The key -i means that the files will be updated inplace. Make sure you have a backup in case that some MQL-specific syntax will be unrecognized by C++ styler and altered incorrectly. For example, one of known problems arises when clang-format sees color constant like this - C'128,128,128'. In such cases one may have to make minor edits in reformatted files to fix MQL-specific inconsistencies. But such cases are minor.

After the procedure you'll get codes formatted according to approved and widely-adopted standards.

The attached file has the txt-extension just to make it possible to attach it here. After download just remove the extension and rename it to .clang-format



Files:
Share it with friends: