davidvarga / MBeautifier

MBeautifier is a MATLAB source code formatter, beautifier. It can be used directly in the MATLAB Editor and it is configurable.

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Ability to disable formatter for certain lines

jmrgibson opened this issue · comments

There are some cases eg having a bunch of values line up by using extra whitespace makes it more readable than the reverse.

% extra whitespace
top_height     = 8
middle_height  = 5
end_height     = 3

% autoformatted
top_height = 8
middle_height = 5
end_height = 3

Other popular formatters for python such as yapf and black give the ability to use a comment specifier, eg % fmt: off and % fmt: on to disable the formatter for that block.

As far as I can tell, MBeautifier doesn't have this, would anyone be opposed to me adding it?

Hi! It's a pretty good idea :)

I would like to propose a construction which is easy-to-implement and extensible:

Let's for example introduce the term "directive" into MBeautifier. One can give directives to MBeutifier as

% MBeautifierDirective:DirectiveName:DirectiveValue

A directive line can contain only the directive comment, nothing else (no preceding code and no trailing comment. so to search for the directive:
isempty(strtrim(codePart)) && numel(regexp('MBeautifierDirective:DirectiveName:DirectiveValue', '^MBeautifierDirective:(\w+):(\w+)$'))

so, group1 contains the directive name, group2 is the directive-value. Then a mapping can be done for directives.

In this particular case I could imagine something like:

% MBeautifierDirective:SuspendFormatting:on
top_height       = 8
middle_height  = 5
end_height       = 3
% MBeautifierDirective:SuspendFormatting:off

When the directive is found, MBeautifier could simply "eat" the lines until the "off" value comes without performing any formatting on them.

Thoughts on it?

Yeah, that certainly works, and yeah, you beat me to getting it done so I'm happy enough with this implementation. My one minor complaint is that % MBeautifierDirective:SuspendFormatting:on is a bit of a mouthful to type. Maybe also allow a shorthand option? MBD:... perhaps?

The final format looks like (from readme):

a =  1;
% MBeautifierDirective:Format:Off
longVariableName = 'where the assigement is';
aligned          = 'with the next assignment';
% MBeautifierDirective:Format:On
someMatrix  =  [1 2 3];

I can introduce the shortcut version % MBD:Format:Off if you like - I have nothing against. :)

Sort of supplemental to this (to some extent)... would you consider adding:

  • a rule for whether to format inside strings + directive to disable it ? In general, having format working inside strings is nice for the look and feel of code, but it can be catastrophic in some situations, e.g., when writing xml files via fprintf commands

  • a rule for directive lines removal ? I ignore if this is the case already, but I think one may want to add directives before formatting and then get them removed after

What do you mean to "Format inside strings": Mbeautify should not format inside any strings by default - if you experience the other way, please send some code.

The directive removal_ I will think about it :)

What do you mean to "Format inside strings": Mbeautify should not format inside any strings by default - if you experience the other way, please send some code.

It doesn't. I just tried to convey that doing so in a controlled manner could be nice.

Seems dangerous in my eyes. IntelliJ refactors also in my comments sometimes if I'm not careful enougj and I hate it :) Replacing in strings is even more dangerous as a global setting. Remove directives: It is also a not a straightforward setting for me. You may create an issue and let the community decide ;)

Seems dangerous in my eyes. IntelliJ refactors also in my comments sometimes if I'm not careful enougj and I hate it :) Replacing in strings is even more dangerous as a global setting.

I totally agree. This should never be a default. I was thinking about advanced users that want to do it in a controlled manner: with a rule+directive, so it can be enabled locally.

Remove directives: It is also a not a straightforward setting for me. You may create an issue and let the community decide ;)

I have the feeling that keeping directives such as % MBeautifierDirective:SuspendFormatting:on as part of a code is odd. Perhaps, it would be nice to have a MBeautifier "release" (or "publish") rule for when one decides that the code is aimed for production, in contrast to debug/testing, or simply work in progress.

I totally agree. This should never be a default. I was thinking about advanced users that want to do it in a controlled manner: with a rule+directive, so it can be enabled locally.

... but maybe it's not worth it

I have the feeling that keeping directives such as % MBeautifierDirective:SuspendFormatting:on as part of a code is odd. Perhaps, it would be nice to have a MBeautifier "release" (or "publish") rule for when one decides that the code is aimed for production, in contrast to debug/testing, or simply work in progress.

If you would think this makes some sense, let me know and I'll open an issue.

@kupiqu I feel like having lint/formatting directives inside comments inside code file is standard practice. Other languages that I'm familiar with (python, Rust, C) use the same practice (examples, https://stackoverflow.com/questions/25877285/how-to-disable-unused-code-warnings-in-rust, https://philwrightdev.wordpress.com/2014/10/01/making-clang-format-ignore-sections/, https://github.com/google/yapf#why-does-yapf-destroy-my-awesome-formatting)

@davidvarga Yes please on the % MBD:Format:Off shortcut! Thanks for being so responsive about this.

@jmrgibson , @kupiqu : I close the issue for now. Feel free to open anytime another one if you would like to have another directive (I added a label for it).

@kupiqu I feel like having lint/formatting directives inside comments inside code file is standard practice. Other languages that I'm familiar with (python, Rust, C) use the same practice (examples, https://stackoverflow.com/questions/25877285/how-to-disable-unused-code-warnings-in-rust, https://philwrightdev.wordpress.com/2014/10/01/making-clang-format-ignore-sections/, https://github.com/google/yapf#why-does-yapf-destroy-my-awesome-formatting)

I was not aware of it, thanks @jmrgibson