Smart indent and comments

118 views (last 30 days)
Ryan
Ryan on 2 Jun 2016
Commented: Stephen23 on 4 Jan 2022
If I have some code such as the following:
if true
a = 1
a = a+1
disp(a)
end
then I use the Ctrl+R shortcut to comment a line I get
if true
a = 1
% a = a+1
disp(a)
end
then if I press Ctrl+I for smart indent I get
if true
a = 1
% a = a+1
disp(a)
end
This is really annoying; since using Ctrl+T to uncomment leaves the line unaligned, requiring another alignment. Is there a way to change the behaviour to add the comments at the start of the first non-whitespace character on the line so that alignment is not affected between smart indents? If not is there a way to request this change?

Answers (4)

Lockywolf
Lockywolf on 20 Jun 2019
Three years, no definitive answer. Sad.
  1 Comment
Steven Lord
Steven Lord on 20 Jun 2019
To officially request this functionality, contact Technical Support using the telephone handset icon in the upper-right corner of this page and ask them to enter this in the enhancement database.

Sign in to comment.


Ryan Mott
Ryan Mott on 4 Jan 2022
I will sometimes repeatedly Shift+Tab to unindent the text, comment with Ctrl+R, and then Tab back.
But that's awkward, so Ctrl+R has largely fallen out of my day-to-day usage, except for the somewhat rare cases when I write an unscoped script. I usually type the '%' by hand.
Ahmet Cecen's support for the current Ctrl+R does not justify the status quo - if Ctrl+R's behavior is OK, then Ctrl+I should change, as KJ N observes.
I also contest Ahmet's statement that the current Ctrl+R behavior is customary. Compare with Microsoft's IDE's: Visual Studio's Ctrl+K+C and Visual Studio Code's Ctrl+/ both place comment characters at the beginning of the text, not at the beginning of the line. My preference is the former behavior.
  1 Comment
Stephen23
Stephen23 on 4 Jan 2022
Re "customary": I often get the feeling that many "features" like this are due to computer-programmer tradition more than anything else: perhaps in the old days of black-and-white (or even green) screens displaying a maximum of 80 characters, this way of commenting made sense (i.e. to make the lines stand out). But seriously this is now the 21st century, if I want those lines to stand out then I would select a nice bold color to make them stand out, rather than completely ruin the indentation of my code and making it harder to get an overview of the code blocks.
I completely agree that apart from occasional use when testing code, this "feature" makes ctrl+R ... less useful.
At the very least, commenting at the indentation level/start of text needs to be an option.

Sign in to comment.


Felipe Jiménez Hernández
Hi!
I agree that Ctrl-R should place the % at the beginning of indentation, not on the very left.
Or at least there should be a preference option for that behavior.
Thank you!

Ahmet Cecen
Ahmet Cecen on 2 Jun 2016
if true
a = 1
% a = a+1
disp(a)
end
Is the customary way to comment a line, so that you can see a line is comment right at the beginning. Maybe this is easier to see in a larger and deeper code.
Consider and excerpt from MultiPolyRegress. Tell me in 1 second which lines are commented in the style you suggested:
Style 1)
% Create a Legend for Coefficient Correspondence
for ii=1:NLegend
currentTerm=find(A(ii,:));
%currentLegend='';
for jj=1:length(currentTerm);
if jj==1;
%currentLegend=[currentLegend,'x',num2str(currentTerm(jj))];
if A(ii,currentTerm(jj)) > 1
%currentLegend=[currentLegend,'.^',num2str(A(ii,currentTerm(jj)))];
end
else
currentLegend=[currentLegend,'.*x',num2str(currentTerm(jj))];
if A(ii,currentTerm(jj)) > 1
currentLegend=[currentLegend,'.^',num2str(A(ii,currentTerm(jj)))];
end
end
end
%Legend{ii,1}=currentLegend;
end
Versus:
Style 2)
% Create a Legend for Coefficient Correspondence
for ii=1:NLegend
currentTerm=find(A(ii,:));
% currentLegend='';
for jj=1:length(currentTerm);
if jj==1;
% currentLegend=[currentLegend,'x',num2str(currentTerm(jj))];
if A(ii,currentTerm(jj)) > 1
% currentLegend=[currentLegend,'.^',num2str(A(ii,currentTerm(jj)))];
end
else
currentLegend=[currentLegend,'.*x',num2str(currentTerm(jj))];
if A(ii,currentTerm(jj)) > 1
currentLegend=[currentLegend,'.^',num2str(A(ii,currentTerm(jj)))];
end
end
end
% Legend{ii,1}=currentLegend;
end
  2 Comments
Ryan Mott
Ryan Mott on 4 Jan 2022
Edited: Ryan Mott on 4 Jan 2022
I also disagree with Ahmet. We could also make comments stand out by doubling comments' font size, but making commented lines stand out that much is not worth the visual disruption.
Perhaps it is just how I am conditioned, but I find Ahmet's preferred sample visually jarring. I have to stop and think to navigate around the comments because of their (for me) unexpected position.
I personally am accustomed to seeing total white space at left of an indented block, and its corners readily mark where the block starts and ends. Characters jutting out of their context into that white space make it more difficult to quickly find where the block ends. I can see '%' characters just fine by following the leftmost edge of the text as usual. Given that fact I prefer to see comments in the same predictable location as other lines of text.
Again, I could probably be conditioned differently, but I still believe I would acclimate more easily to indented comment characters.

Sign in to comment.

Categories

Find more on Historical Contests in Help Center and File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by