florianschanda / miss_hit

MATLAB Independent, Small & Safe, High Integrity Tools - code formatter and more
GNU General Public License v3.0
158 stars 21 forks source link

Disable indentation on the root-level of a function #255

Closed fangq closed 2 years ago

fangq commented 2 years ago

What kind of feature is this?

I have been following the default MATLAB formatting styles to format my own toolboxes/functions, something looks like this:

function res = myfun(a, b, c)
%
% res = myfun(a, b, c)
% help info
%
statement1;
statement2;
...

I just tried miss_hit, and found that the formatted function has indentations for all docstring and command lines

function res = myfun(a, b, c)
    %
    % res = myfun(a, b, c)
    % help info
    %
    statement1;
    statement2;
    ...

I am wondering if there is an option to disable this leading indentation for the root-level of a function, so that the formatted file follows MATLAB's style?

thanks

Your MATLAB/Octave environment

florianschanda commented 2 years ago

There is no way to do this right now. Let me see how hard it would be to add...

florianschanda commented 2 years ago

So I think this is really specific, right? You just want this for top-level functions in function files. All other functions (nested, in class files, etc.) would be indentend normally? At least that is what a cursory survey of the various coding snippets on the mathworks website suggests...

fangq commented 2 years ago

thanks for looking into this - only disabling the indentation for the main/top function is fine for me. because most of my toolboxes have a single function per unit/file. having a leading indentation for every line looks a little bit clunky and redundant for such format.

but of course, if it takes more efforts to separately handle top function and embedded functions, it is also fine to have a flag to disable both. whichever is easier to implement.

florianschanda commented 2 years ago

I have pushed a proposal fix for this. Could you try it please and let me know if it meets your needs?

florianschanda commented 2 years ago

The CHANGELOG entry should explain how to activate it:

fangq commented 2 years ago

thank you so much! works like a charm! feel free to close the issue.

I noticed one thing: when I set redundant_brackets: true in the cfg file, mh_style failed to recognize it

miss_hit.cfg: error: config file contains errors
In miss_hit.cfg, line 3
| redundant_brackets: true
| ^^^^^^^^^^^^^^^^^^ error: expected valid style configuration name (did you mean align_round_brackets?)
.: error: cannot find project root because the config file contains errors: please add a config file with the 'project_root' directive
MISS_HIT Style Summary: 2 file(s) analysed, 3 error(s)

I wonder if this is the right place to set this flag?

fangq commented 2 years ago

never mind. I figured out, need to use suppress_rule: "redundant_brackets" to disable this rule (which does not have a configuration).

fangq commented 2 years ago

I know this might belong to a separate issue - would it be possible to add an option to only remove indentation of the docstrings (the block of commented lines right before or after the function declaration) of each top level function? sorry for asking too many wishes.

florianschanda commented 2 years ago

@fangq please make that a separate ticket with an example of what you need.