MBeautifier is a lightweight M-Script based Matlab source code formatter usable directly in the Matlab Editor.
- Padding operators with white spaces based on the XML configuration file.
- Correction white space padding of keywords
- Correction of indentation using the Smart Indent functionality of the Matlab Editor
- Removal of continuous empty lines (the number can be configured)
- Optionally inserting missing element separators (commas) in matrix and cell array initializations
- Different working modes: format the current page of the Matlab editor, format only a selection in the Matlab Editor or format file(s)
Add containing directory to the Matlab path, then execute: MBeautify.setup()
.
This command will create the standard configuration of formatting stored in MBeautifier\resources\settings\MBeautyConfigurationRules.m
.
This file is used in run-time to gather the configuration rules, therefore when the configuration XML file has been modified, executing this function again will make the rules active.
The configuration can be modified by editing the MBeautifier\resources\settings\MBeautyConfigurationRules.xml
file.
Currently two types of configuration rules are implemented: OperatorPaddingRule
and SpecialRule
.
Each OperatorPaddingRule
represents the formatting rules for one single operator and consists of a key, the string that should be replaced and a string that should be used for the replacement.
<OperatorPaddingRule>
<Key>NotEquals</Key>
<ValueFrom>~=</ValueFrom>
<ValueTo> ~= </ValueTo>
</OperatorPaddingRule>
The above example shows the rule for the "not equals" operator. The ValueFrom
node stores the operator ~=
and the ValueTo
node stores the expected format: the operator should be preceded and followed by a white-space character.
All of the operator padding rules are collected dynamically, therefore adding a new node to this list then executing the setup
command will result in that MBeautifier will replace the currently added node also.
These rules are basically switches for certain functionalities of MBeautifier.
The current list of special rules:
- MaximalNewLines: Integer value. MBeautifier will remove continuous empty lines. This rule can be used to specify the maximal number of maximal continuous empty lines.
- AddCommasToMatrices: [1|0]. Indicates whether the missing element separator commas in matrices should be inserted. For example:
[1 2 3]
will be formatted as[1, 2, 3]
. - AddCommasToCellArrays: [1|0]. Indicates whether the missing element separator commas in cell arrays should be inserted. For example:
{1 2 3}
will be formatted as{1, 2, 3}
. - MatrixIndexing_ArithmeticOperatorPadding: [1|0]. Indicates whether the arithmetic operators should be padded by white spaces (using the operator padding rules), when they are used to index matrices. For example:
matrix(end+1) = 1
can be formatted asmatrix(end+1) = 1
when value is set to 0, or asmatrix(end + 1) = 1
if value is set to 1. - CellArrayIndexing_ArithmeticOperatorPadding: [1|0]. Indicates the same as
MatrixIndexing_ArithmeticOperatorPadding
but for cell arrays.
Currently there are three approaches supported:
- Perform formatting on the currently active page of Matlab Editor. Command:
MBeautify.formatCurrentEditorPage()
. By default the file is not saved, but it remains opened modified in the editor. Optionally the formatted file can be saved using theMBeautify.formatCurrentEditorPage(true)
syntax. - Perform formatting on the currently selected text of the active page of Matlab Editor. Command:
MBeautify.formatEditorSelection()
. An optional saving mechanism as above exists also in this case. Useful in case of large files, but in any caseMBeautify.formatCurrentEditorPage()
is suggested. - Perform formatting on a file. Command:
MBeautify.formatFile(file)
. Can be used with (1)one argument: the input file is formatted and remains open in the Matlab editor unsaved; (2)two arguments asMBeautify.formatFile(file, outFile)
: the formatted file is saved to the specified output file if possible. Output can be the same as input. - Perform formatting on several files in a directory. Command:
MBeautify.formatFiles(directory, fileFilter)
. The first argument is an absolute path to a directory, the second one is a wildcard expression (used fordir
command) to filter files in the target directory. The files will be formatted in-place (overwritten).
As MBeautifier uses the built-in Matlab Editor functionality, it supports Matlab versions from R2011a.
As Matlab does not contain any formatter even in R2017 releases, this project will be maintained until at least R2018 (if that release will contain a formatter).
As it is planned at the moment, the current release is the last one which is M-Script based, and the next release is planned to be implemented in Java with Matlab interface. This is the first step to make MBeautifier also usable in Octave.