Home

Awesome

MBeautifier

MBeautifier is a lightweight M-Script based MATLAB source code formatter usable directly in the MATLAB Editor.

Basic working

Main features

Deployment and Configuration

Simply add the root directory to the MATLAB path.

Configuration

The configuration can be modified by editing the MBeautifier\resources\settings\MBeautyConfigurationRules.xml file.

Configuration rules

Currently three types of configuration rules are implemented: Operator padding rule, Keyword padding rule and Special rule.

Operator padding rules

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.

Keyword padding rules

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.

Special rules

These rules are basically switches for certain functionalities of MBeautifier.

The current list of special rules:

Special rules regarding new lines
Special rules regarding matrix and cell array separators
Special rules arithmetic operators
Special rules regarding continous lines
Special rules regarding indentation

Directives

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:

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.

Directive List
Format

Directive to generally control the formatting process. Possible values:

Example: 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];

Usage

From MATLAB Command Window

Currently four approaches are supported:

Shortcuts

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:

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.

Supported Matlab versions

The oldest version of MATLAB to be used to test MBeautifier is R2013b.

Planned future versions

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.).