MBeautifier is a lightweight M-Script based MATLAB source code formatter usable directly in the MATLAB Editor.
Simply add the root directory to the MATLAB path.
The configuration can be modified by editing the MBeautifier\resources\settings\MBeautyConfigurationRules.xml
file.
Currently three types of configuration rules are implemented: Operator padding rule
, Keyword padding rule
and Special rule
.
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 example above 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.
Each KeyworPaddingRule
represents the formatting rules for one single keyword and consists the keyword itself, and a numeric value of the needed white-space padding on the right side.
<KeyworPaddingRule>
<Keyword>properties</Keyword>
<RightPadding>1</RightPadding>
</KeyworPaddingRule>
The example above shows the rule for the keyword "properties". The RightPadding
node stores the expected right padding white space amount: the keyword should be preceded by one white space character.
Note: Not all of the keywords are listed - only the ones where controlling padding makes sense.
These rules are basically switches for certain functionalities of MBeautifier.
The current list of special rules:
%%
). Negative values mean no special formatting is needed (the final format is defined by the input and the MaximalNewLines rule). For any number "X" bigger or equal to zero: section comments will be preceded exactly by X empty lines.%%
). Negative values mean no special formatting is needed (the final format is defined by the input and the MaximalNewLines rule). For any number "X" bigger or equal to zero: section comments will be followed exactly by X empty lines.a = 1; b = 2;
), otherwise it will break every statement into a new line. Defaults to "0".[1 2 3]
will be formatted as [1, 2, 3]
.{1 2 3}
will be formatted as {1, 2, 3}
.matrix(end+1) = 1
can be formatted as matrix(end+1) = 1
when value is set to 0, or as matrix(end + 1) = 1
if value is set to 1.MatrixIndexing_ArithmeticOperatorPadding
but for cell arrays.MBeautifier directives are special constructs which can be used in the source code to control MBeautifier during the formatting process. The example below controls the directive named Format
and sets its value to on
and then later to off
.
a = 1;
% MBeautifierDirective:Format:Off
longVariableName = 'where the assigement is';
aligned = 'with the next assignment';
% MBD:Format:On
someMatrix = [1 2 3];
The standard format of a directive line is:
<ws>%<ws>MBeautifierDirective<ws>:<ws>:NAME<ws>:<ws>VALUE<ws>[NEWLINE]
or : <ws>%<ws>MBD<ws>:<ws>:NAME<ws>:<ws>VALUE<ws>[NEWLINE]
where
<ws>
means zero or more optional white space charactersNAME
means the directive name (only latin letters, case insensitive)VALUE
means the directive value (only latin letters, case insensitive)MBeautifierDirective
is freely interchangable with MBD
Note: Directive names which are not present in the list below, or directive values which are not applicable to the specified directive will be ignored together with a MATLAB warning.
Format
Directive to generally control the formatting process.Possible values:
on
- Enable formattingoff
- Disable formattingExample:In the code-snippet below MBeautifier is formatting the first line using the configuration currently active, but will not format the lines 2,3,4,5. The last line will be beautified again using the current configuration.
a = 1;
% MBeautifierDirective:Format:Off
longVariableName = 'where the assigement is';
aligned = 'with the next assignment';
% MBeautifierDirective:Format:On
someMatrix = [1 2 3];
The formatted code will look like (configuration dependently):
a = 1;
% MBeautifierDirective:Format:Off
longVariableName = 'where the assigement is';
aligned = 'with the next assignment';
% MBeautifierDirective:Format:On
someMatrix = [1, 2, 3];
Currently four approaches are supported:
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 the MBeautify.formatCurrentEditorPage(true)
syntax.MBeautify.formatEditorSelection()
. An optional saving mechanism as above exists also in this case. Useful in case of large files, but in any case MBeautify.formatCurrentEditorPage()
is suggested.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 as MBeautify.formatFile(file, outFile)
: the formatted file is saved to the specified output file if possible. Output can be the same as input.MBeautify.formatFiles(directory, fileFilter)
. The first argument is an absolute path to a directory, the second one is a wildcard expression (used for dir
command) to filter files in the target directory. The files will be formatted in-place (overwritten).There is a possibility to create shortcuts for the first three approaches above, which shortcut buttons will appear under the "Shortcuts" tab of MATLAB's main window below Matlab R2019, and under "Favourites" and on the "Quick Access Toolbar" above.
To create these buttons, the following commands can be used:
MBeautify.createShortcut('editorpage')
: Creates a shortcut for MBeautify.formatCurrentEditorPage()
MBeautify.createShortcut('editorselection')
: Creates a shortcut for MBeautify.formatEditorSelection()
MBeautify.createShortcut('file')
: Creates a shortcut for MBeautify.formatFile(sourceFile, destFile)
These shortcuts will add the MBeautifier root directory to the MATLAB path too, therefore no MATLAB path preparation is needed to use MBeautifier next time when a new Matlab instance is opened.
The oldest version of MATLAB to be used to test MBeautifier is R2013b.
It is planned that the project is maintained until MATLAB is shipped with a code formatter with a similar functionality.
It is planned to make MBeautifier also usable in Octave, by starting a new development branch using Java/Kotlin (versions 2.). The MATLAB based branched will be developed in branch versions (1.).