davidvarga / mbeautifier Goto Github PK
View Code? Open in Web Editor NEWMBeautifier is a MATLAB source code formatter, beautifier. It can be used directly in the MATLAB Editor and it is configurable.
License: GNU General Public License v3.0
MBeautifier is a MATLAB source code formatter, beautifier. It can be used directly in the MATLAB Editor and it is configurable.
License: GNU General Public License v3.0
Function arguments don't get the same spacing as normal brackets if MatrixIndexing_ArithmeticOperatorPadding is 0. I suggest setting the default of this value to 1.
3 * (1 + 2)
trace(1+2)
Triggered by a comment on SO: https://stackoverflow.com/questions/23960822/how-can-i-reformat-code-in-matlab-editor/37362250#37362250
Make the possibility to create shortcuts in Matlab which make able to perform formatting without the need to execute the command in the command widow and to add MBeauty to the Matlab path.
I suggest that power operators ^ and .^ should not be separated by default.
The reason is that in function calls (seems they are affected by matrix indexing settings) it becomes barely readable:
sqrt(a+b^2)
becomes sqrt(a+b ^ 2)
While better variants are:
sqrt(a+b^2)
or sqrt(a + b^2)
I sometimes see pieces of code of the form
figure; plot(...)
Honestly, I don't like to have more than one command per line. Does this tool provide a way to insert new lines after every semicolon? If yes, what is the option? If not, do you plan to do it?
I assume that in the case of a condition, it is better to align to condition on previous line.
I think this code:
if a + b > 0 && ...
a * b > 0
a = b;
end
better than generated by MBeautifier:
if a + b > 0 && ...
a * b > 0
a = b;
end
So, this is true for while
too.
The latest version with default settings produces this output:
length(x) + 1;
a(length(x)+1);
a(b:b+n-1);
b + n - 1;
So the sameexpressions gets formatted in different ways depending on where it appears. This was not the case in older versions; is this by design now?
The current codebase should be simplified and cleaned-up for the final Matlab based release. As it is planned - to avoid problems between different Matlab releases - a Java based (compilant with Java6) reimplementation will be done and release with version 2...
The performance of the current implementation should be analyzed and the bottlenecks should be removed.
Hi,
That's basically in the title. After each whole file beautifying, the function appends an empty line.
If there is no open m file, i get an error. Maybe we can handle this case with a warning message.
Formatting of arithmetic operators needs to address special cases.
Two cases I have found were:
I have just used it on one file so there might be more cases that have to be considered.
Problems covered in this issue
a(3, 1) = [3, 1]
a(3, :) = [3, 1]
is changed to
a(3, 1) = [3, 1]
a(3,:) = [3, 1]
where a space is missing after the comma.
a = {1,
2};
is formatted as
a = {1; ...
2};
which results in invalid dimensions.
MBeautifier changes
if -1 > -2
end
to
if - 1 > -2
end
. The extra space after if -
is wrong.
x = A\b;
becomes
x = A b;
The purpose of this issue to provide addtional settings for replacing operators when they are used in indexing (matrixes and cell arrays) like:
namesCell{ind+1} = 'Andy';
rather than namesCell{ind + 1} = 'Andy';
Two new configuration rule could be introduced:
Both contains a boolean value, and indicates wheter in case of cell-array/matrix indexing (cellArray{ind+1}
and matrix(ind+1)
) te arithmetic operators (+, -, *, /
) should be padded with white spaces (using the configuration rules of these operators) or not.
Based on these configuration rules, the example output can be
% Both set to false
cellArray{end+1} = 'dog';
matric(end+1) = 11;
% Both set to true
cellArray{end + 1} = 'dog';
matric(end + 1) = 11;
Currently the only stable working mode is to format the current page of Matlab Editor.
Two new working modes could be added:
l = {@(x) strcmp(x, 'True')};
gets turned into
l = {@(x), strcmp(x, 'True')};
notice the incorrect comma added after @(x)
. This is quite a showstopper :]
Getting tokens for String, Continuosment is implemented really badly now.
Think it over and implement a faster, more intelligent way.
It would be nice if there was a command to format all files matching a given pattern. E.g., MBeautify.formatFiles('**/*.m').
I'm new to Matlab, how can I use them?
Is there any "Scripts" (or "Plugins") section in Matlab where there scripts should placed there?
Currently, if matrices and cell arrays are declared using multiple lines, but no ellipsis line continuation indicator (...) is used, the elements of these containsers dont get formatted.
In these cases the ellipsis line continuation indicator (...) should be also inserted.
Example
c = { 1+2+3 1*2 -12 -10/5
-1 -2 -3 -4
10*(6+5) 10 10 10}
b = [1+2 2 3
4 5+4 6
7 8+4 9
10 11+4 12]
The expected formatting is
c = {1 + 2 + 3, 1 * 2, -12, -10 / 5; ...
-1, -2, -3, -4; ...
10 * (6 + 5), 10, 10, 10}
b = [1 + 2, 2, 3; ...
4, 5 + 4, 6; ...
7, 8 + 4, 9; ...
10, 11 + 4, 12]
Hi!
[[I, Z]; [h * (K-L), I]]
changes to [[I, Z]; [h, * (K-L), I]]
while
<SpecialRule>
<Key>AddCommasToMatrices</Key>
<Value>0</Value>
</SpecialRule>
<SpecialRule>
<Key>AddCommasToCellArrays</Key>
<Value>0</Value>
</SpecialRule>
<SpecialRule>
Commit b931478, MATLAB 2016b.
When applied to testfile.m the following errors appear in MATLAB 2016a on Linux:
Index exceeds matrix dimensions.
Error in MBeautify/performFormatting>processContainer (line 559)
indexes = find([containerBorderIndexes{:, 2}] == maxDepth, 2);
Error in MBeautify/performFormatting>performReplacementsSingleLine (line 362)
[data, arrayMapCell] = processContainer(data, settingConf);
Error in MBeautify/performFormatting>performReplacements (line 151)
actCodeTemp = performReplacementsSingleLine(actCodeTemp, settingConf);
Error in MBeautify/performFormatting (line 91)
actCodeFinal = performReplacements(actCode, settingConf);
Error in MBeautify.beautify (line 18)
formattedSource = MBeautify.performFormatting(codeToFormat, settingConf);
In this case
function[a]=f()
beautifier gives
function[a] = f()
I think there should be a space between function
and [
In my code I like to leave spaces after commas in function calls, e.g.:
A = eye(3, 4);
However, in indexing expressions I don't like to leave spaces, e.g.:
b = A(:,1);
Is this feature feasible? Thanks.
I suggest two special rules:
This code
a = 1 + 1;
%% New Matlab Section
b = 2 + 2;
beautified to this code
a = 1 + 1;
%% New Matlab Section
b = 2 + 2;
For example, "1e-6" is added with a space as "1e -6", which is wrong.
This code
a = (k-1):-1:2;
converts to
a = (k - 1): - 1:2;
but better variant:
a = (k - 1):-1:2;
It's probably a quite tricky thing to implement, but it would be nice if there was an option to autowrap long (e.g., > 79 chars) lines. Some sort of heuristics could be used to figure out where to break the lines, e.g., first try breaking at "=". Then if either of parts is still too long, break farthest down the line that puts the line (w/ added ...) under the line limit.
Clean-up means:
This feature will be implemented when the full backend implementation is moved to Java.
[-1, 2, 3, 4] -> [-, 1, 2, 3, 4]
[+1, 2, 3, 4] -> [+, 1, 2, 3, 4]
If the +/- sign is on a later position it behaves fine
[1, -2, 3, 4] -> [1, - 2, 3, 4]
however, there is an additional withespace create between the +/- sign and the number
I think -2 is easier to read than - 2 in this case
Hey, David.
Looks like Beautifier fails on the following code:
[out1, out2] = func([str1, str2], [str3, str4]);
it removes last ");" and code becomes non-compilable.
Denis
Line
Btn.Callback = {errStruct.funhelp, @(o, e) somefunction('somestring'), ''};
is formatted as
Btn.Callback = {errStruct.funhelp, @(o, e), somefunction('somestring'), ''};
I have a hundred files and I want beautify each of them without opening window in editor files on each file.
If possible, it would be great if the method formatFiles could work silently.
Despite having MatrixIndexing_ArithmeticOperatorPadding
and CellArrayIndexing_ArithmeticOperatorPadding
set to 0 in both the m and xml configuration files, code such as
b = a(1:end-2);
c = a(end-1);
e = d{1:end-2};
f = d{end-1};
is converted to
b = a(1:end - 2);
c = a(end - 1);
e = d{1:end - 2};
f = d{end - 1};
The configuration files are the default ones, have not been edited.
Tested on both R2016a and R2017a
Hi,
[[I,Z];[h*(-K-L),I]] * [[I,Z];[h*(K-L),I]]
becomes
[[I, Z]; [h * (-K - L), I]] * [[I, Z]; [h * (K-L), I]]
with spaces added only in the first parenthesis.
Commit b931478, MATLAB 2016b.
Somewhat relevant config:
<SpecialRule>
<Key>CellArrayIndexing_ArithmeticOperatorPadding</Key>
<Value>0</Value>
</SpecialRule>
<SpecialRule>
<Key>MatrixIndexing_ArithmeticOperatorPadding</Key>
<Value>0</Value>
</SpecialRule>
MBeautify add a single indent every line of multiple lines comment after run command MBeautify.formatCurrentEditorPage()
. This isn't happening with single line comment.
Before call command:
%This is one line comment
%{
This is
multiline
comment
%}
After call MBeautify.formatCurrentEditorPage()
10 times.
%This is one line comment
%{
This is
multiline
comment
%}
A declarative, efficient, and flexible JavaScript library for building user interfaces.
๐ Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
An Open Source Machine Learning Framework for Everyone
The Web framework for perfectionists with deadlines.
A PHP framework for web artisans
Bring data to life with SVG, Canvas and HTML. ๐๐๐
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
Some thing interesting about web. New door for the world.
A server is a program made to process requests and deliver data to clients.
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
Some thing interesting about visualization, use data art
Some thing interesting about game, make everyone happy.
We are working to build community through open source technology. NB: members must have two-factor auth.
Open source projects and samples from Microsoft.
Google โค๏ธ Open Source for everyone.
Alibaba Open Source for everyone
Data-Driven Documents codes.
China tencent open source team.