Change to code formatting behaviour in editor in R2021b?

Does anyone know if this is a change in default formatting behaviour for indents and formatting behaviour or whether I am just missing some setting that I had in R2019b? (I have compared my settings between R2019b and R2021b and they look the same and were presumably imported on installing r2021b)
Below is the same file in R2019b (top) and R2021b (bottom). I did what I always do when creating a new function, which is to type the signature line, a blank line and then the 'end' line, then after typing 'end' I hit up arrow to take me to the previous line to start typing my function body.
This has always been my method for creating new functions. I have all auto-completion things turned off and always have, I like to type my own 'end', close parentheses, quotes, etc. But while in R2019b this used to put my cursor exactly where I want to type, at the correct indent I now find in R2021b that pressing up arrow after typing 'end' takes me to the start of the line, which is just really annoying as I clearly don't want to start typing there.
This has only changed in behaviour since I updated to R2021b.

 Accepted Answer

Hi Adam,
I can see two possible options here:
1. Enable automatic completions for block statements in the preferences. This can be done through the preference panel (MATLAB > Editor/Debugger > Automatic Completions) and checking the boxes under "Autocomplete block endings". Setting these preferences will cause MATLAB to automatically insert the "end" after you type the starting keywords (i.e. "if" or "function") and then insert a return.
2. Alternatively, you should be able to regain the old behavior by modifying the following setting:
s = settings;
s.matlab.editor.indent.RemoveAutomaticWhitespace.PersonalValue = 0;
This should no longer remove whitespaces as you continue to type.
Please note that this setting is currently not documented, and is therefore subject to potential change in future releases of MATLAB.
Do either of these help with this?

7 Comments

Hi Duncan,
Sorry, I haven't checked back in here for a few days. Your 2nd option did fix the problem, thank you.
I am programming on two machines currently so I had actually setup one to try using auto-completion previously, like your 1st suggestion, but I am just so used to typing my own function end lines that I still do it half the time anyway and just end up with two of them, have to delete one, etc! I'll persevere with it on that machine for a while to see if I get used to it, but I'm happy with the 2nd solution and just doing my own typing :)
Thanks for the question and answer Adam and Duncan. I've also noticed this difference. Note that "indent" is not a SettingsGroup property prior to R2021b (tested in 21a) so if this setting is applied programmatically it should include verLessThan check.
This under-the-hood tweak should really be an official preference. Hopefully it will get added in a later release.
For what it's worth, this settings tweak has been very helpful. But, it does not completely revert the functionality. When applying smart indent to highlighted text, blank lines still lose all white space, which is annoying.
I agree, when smart indenting is "on" then blank lines lose all white space -- this is killing me! Have you found a solution? I'm searching the "settings" structure and haven't found anything yet.
I would have hoped this setting would also affect the smart indentation. Now I will have to write my own indentor, or leave a copy of R2021a around to loop through my functions to fix the indentation style.
Luckily I already have a comment stripper to make it easier to determine the correct level of indentation.
The second option works and this is the behavior I expected. I didn't meet this problem in any other code editor I used. It's so annoying. Please make this an option and don't remove this setting.

Sign in to comment.

More Answers (1)

I don't have anything newer than R2019b, but the documentation suggests that the same should apply.
The SmartIndentWhileTyping setting under the Editor>Language tab is likely what you want to enable. The documentation indicates that this is disabled by default.

4 Comments

Yeah, I do have this option selected, and it does take me to the correct indent when I hit enter after typing the function signature line, it's just when I leave that blank and create my 'end' line as I always do first then it doesn't return to the indented location by default. I don't like creating function bodies before I have the end in place though, it just doesn't feel natural to me!
I probably shouldn't have made an answer out of this without a way to test it.
Does it insert the tabs/spaces and then just not navigate correctly to the end of the line?
Yeah, you can move back to the correctly indented point with arrows (I always have tabs as tabs, not spaces, but either should work the same) rather than having to tab it again yourself.
DGM
DGM on 19 Nov 2021
Edited: DGM on 19 Nov 2021
I am not looking forward to this new editor at all. It really sounds like all the lag and flakiness of the online version -- on the desktop.
Here's to hoping someone knows of a way around this. I can delete my answer if you think it would get more views in the 'unanswered' queue; but if so, this clarification should be moved to the question.

Sign in to comment.

Categories

Products

Release

R2021b

Asked:

on 17 Nov 2021

Commented:

on 14 Apr 2023

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!