Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Closed
fangq opened this issue May 25, 2022 · 9 comments
Closed

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

fangq opened this issue May 25, 2022 · 9 comments
Labels
tool: mh_style Affects the style checker tool

Comments

@fangq
Copy link

fangq commented May 25, 2022

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

  • MATLAB or Octave: MATLAB
  • Which release/version: 2020a
@florianschanda
Copy link
Owner

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

@florianschanda florianschanda added the tool: mh_style Affects the style checker tool label Jun 4, 2022
@florianschanda
Copy link
Owner

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
Copy link
Author

fangq commented Jun 6, 2022

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 added a commit that referenced this issue Jun 6, 2022
New option to NOT indent the function body of a
top-level function.
@florianschanda
Copy link
Owner

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

@florianschanda
Copy link
Owner

The CHANGELOG entry should explain how to activate it:

  • Add a new configuration option "indent_function_file_body" for MH
    Style. This is true by default. If you set it to false, then you get
    the odd indentation style that somewhat common in the MATLAB world,
    where functions in function files do not have their body indented.
    For example:

    function z = Potato(w)
    z = -w;
    

    Note that this option only affects top-level functions in function
    files. Any other function (e.g. a method, or a nested function) is
    not affected.

@fangq
Copy link
Author

fangq commented Jun 7, 2022

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
Copy link
Author

fangq commented Jun 7, 2022

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

@fangq fangq closed this as completed Jun 7, 2022
@fangq fangq reopened this Jun 7, 2022
@fangq
Copy link
Author

fangq commented Jun 7, 2022

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
Copy link
Owner

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
tool: mh_style Affects the style checker tool
Projects
None yet
Development

No branches or pull requests

2 participants