Unwanted editor behavior: whitespace only lines trimmed
13 vues (au cours des 30 derniers jours)
Afficher commentaires plus anciens
I'm using MATLAB 2023a. Sometimes, when I have a line in an m-file with only whitespace then whitespace characters are removed from the line (not by me). I want this editor behavior to stop. In other languages/editors, I can set something like "remove trailing whitespace" to false to get the desired behavior. How do I do this in MATLAB?
What I get (no whitespace):
if
code;
code;
end
What I want (whitespace):
if
code;
code;
end
Edit:
From @DGM's comment, I understand the smart indenting behavior changed in R2021 (as pointed out in Smart Indenting now Removes Whitespace - MATLAB Answers - MATLAB Central (mathworks.com)). I would like to leave smart indenting on and for it to leave whitespace on otherwise blank lines. How can I acheive this via setting preferences? (This assumes MathWorks would not change a behavior so prominently linked to line-by-line workflows without providing an option to revert the change!!!)
3 commentaires
Réponses (2)
goc3
le 22 Mar 2024
Modifié(e) : goc3
le 1 Avr 2024
Release 2024a includes a setting for this!! The following code will stop smart indenting from removing white space:
s = settings;
s.matlab.editor.indent.PadEmptyLines.PersonalValue = 1;
You may also need to set the following:
s.matlab.editor.indent.RemoveAutomaticWhitespace.PersonalValue = 0;
0 commentaires
Pratyush
le 20 Oct 2023
Hi Jonathan,
I understand you want to maintain the whitespaces manually placed in a M file and do not want the MATLAB editor to remove those whitespaces atomatically.
Change the following setting in MATLAB to achieve this:
Go to Home > Preferences > Editor/ Debugger > Indenting.
Uncheck the check box "Apply smart indenting while typing".
Hope this helps.
2 commentaires
DGM
le 20 Oct 2023
Modifié(e) : DGM
le 20 Oct 2023
How is that a solution (or workaround) to the problem?
Despite the misleading cursor position after creating a new auto-indented line, tabs are no longer actually retained on a line unless some text is inserted before hitting enter again (or unless you play some ridiculous games with editing and cursor motion sequences or held keys). In affected versions, if the smart indenter is manually invoked, it will actively remove any manually inserted indentation on blank lines.
This behavior is a counterproductive annoyance regardless of whether it's applied manually or while typing. Disabling the automated use only results in the user needing to place all indentation manually. It doesn't insert tabs in empty lines where the user would rightfully expect tabs to be automatically inserted. It doesn't force the smart indenter to preserve tabs on lines that have had tabs manually inserted.
There is one mostly-useless thing this answer does for us. If a superfluous tab were manually inserted on a new auto-indented line, the smart indenter would normally discard it, just the same as it discards the automatically inserted tabs. Disabling the automatic smart indenter prevents it from deleting tabs created manually via an identical sequence of keystrokes (at least automatically). Of course, if the smart indenter worked, we wouldn't be trying to manually add those tabs in the first place.
Disclosure: My only familiarity with these recent versions is via MATLAB online, so I'm never really sure how much is MATLAB or some difference inherent to the web-interface. Either way, that's what I've observed.
Voir également
Catégories
En savoir plus sur Environment and Settings dans Help Center et File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!