MATLAB-extension-for-vscode icon indicating copy to clipboard operation
MATLAB-extension-for-vscode copied to clipboard

Feature Request - More Code Formatting Authority / Options

Open berge459 opened this issue 1 year ago • 11 comments

It would be incredibly useful for teams to be able to enforce code style guidelines a la something like clang-format. Additionally, features like those provided in clang-tidy for variable/function/class naming conventions would help teams to produce more uniform MATLAB codebases with minimal manual effort.

A simpler start to this could just include a "comfortable" vs. "compact" preference on auto formatting: Comfortable: matlab_function(arg1, arg2, arg3); Compact: matlab_function(arg1,arg2,arg3);

or Comfortable: var = x * y; Compact: var=x*y;

berge459 avatar Apr 28 '23 12:04 berge459

It would also be nice to have some control over the indentation. Currently when I save a file it flattens everything within a function that isn't part of a code block to have no indentation. I can imagine someone preferring this, but indenting functions makes it much easier for me to see where it starts and ends, and I'd prefer not to have the formatter "fix" it.

Whyhow314 avatar May 03 '23 15:05 Whyhow314

Thanks for this suggestion! We will consider adding greater configurability in the future.

dklilley avatar May 09 '23 15:05 dklilley

Currently when I save a file it flattens everything within a function that isn't part of a code block to have no indentation.

@Whyhow314 - Document formatting currently listens to the preferences defined in MATLAB. If you open MATLAB, you should be able to modify the Function indenting format preference under "MATLAB > Editor/Debugger > Language". Perhaps the behavior with Indent all functions is closer to what you would prefer?

dklilley avatar May 09 '23 15:05 dklilley

@dklilley Looks like that fixed it, thanks! I didn't realize that it respected the preferences from my matlab installation.

Whyhow314 avatar May 09 '23 15:05 Whyhow314

Does this extension include auto indenting?

tongyaop avatar Jul 12 '23 23:07 tongyaop

@tongyaop this extension provides both format document capability as well as indenting as you type. Is this what you mean by auto indenting?

dklilley avatar Jul 13 '23 14:07 dklilley

@dklilley Thank you for the reply. The code does indent when I type. However, I am looking for a command that is similar to cmd + i (or control + i) in MATLAB desktop that auto indents a piece of already-typed code. When pieces of code are copied from somewhere else, they are not always in the right indentation.

Edit: In MATLAB editor tab, it's called "smart indent"

tongyaop avatar Jul 13 '23 14:07 tongyaop

@tongyaop You can use Format Document for this. I believe the keyboard shortcut for this is shift + option + F (or shift + alt + F). There is not currently support in the extension for Format Selection, although this is something we hope to add in the future.

dklilley avatar Jul 13 '23 14:07 dklilley

Thank you! This works for me

tongyaop avatar Jul 13 '23 14:07 tongyaop

We are also desperately waiting for auto-formatting functionality as mentioned by the original feature requestor (not just indentation, but code formatting - the same actually holds for the MATLAB IDE). Our dream would be a Matlab counterpart to Python's black code formatter.

It is often not software developers (but engineers or similar) that use Matlab, and hence auto-formatting of Matlab code can result in massive improvements of code quality, without the need to bother the contributing engineers with coding style rules.

@dklilley @nickchoi-mw The request has been open since April, any estimate on when this will be released?

dfkoza-vf avatar Dec 19 '23 08:12 dfkoza-vf

This is an area we want to expand upon and are looking into as part of a multi-team effort. I am not able to provide any estimates at this time.

dklilley avatar Dec 20 '23 16:12 dklilley