Smart indent and comments

조회 수: 110 (최근 30일)
Ryan
Ryan 2016년 6월 2일
답변: Xinyu 2024년 1월 22일
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?
  댓글 수: 2
Philipp Glira
Philipp Glira 2020년 10월 5일
+1
Stephen23
Stephen23 2023년 4월 30일
+1 Still waiting for this.

댓글을 달려면 로그인하십시오.

채택된 답변

Xinyu
Xinyu 2024년 1월 22일
The behavior of the CTRL + R shortcut, which comments out a line, was updated in MATLAB R2023a. Consider the code snippet from the Question section:
if true
a = 1
a = a+1
disp(a)
end
When you use the CTRL + R shortcut to comment out a line:
if true
a = 1
% a = a+1
disp(a)
end
And then use the CTRL + T shortcut to uncomment the same line:
if true
a = 1
a = a+1
disp(a)
end
This update eliminates the need to perform additional indenting and unindenting operations to keep the lines aligned. If this functionality aligns with your needs, please update MATLAB release to MATLAB R2023a or a newer version.

추가 답변 (4개)

Lockywolf
Lockywolf 2019년 6월 20일
Three years, no definitive answer. Sad.
  댓글 수: 1
Steven Lord
Steven Lord 2019년 6월 20일
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.

댓글을 달려면 로그인하십시오.


Felipe Jiménez Hernández
Felipe Jiménez Hernández 2019년 9월 3일
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!

Ryan Mott
Ryan Mott 2022년 1월 4일
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
Stephen23
Stephen23 2022년 1월 4일
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.

댓글을 달려면 로그인하십시오.


Ahmet Cecen
Ahmet Cecen 2016년 6월 2일
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
KJ N
KJ N 2018년 11월 5일
I actually disagree that the `%` at the beginning of the line help the comments look better. If you place the comment at the start of the line your eyes have to go left and right to see which lines are commented while if the `%` is similarly indented you can see clearly while going through the code at the point it is commented (which is when you care the most about which lines are commented).
What's more, if the default behavior of `Ctrl-R` (auto-comment by adding a `%` to the beginning of the line) truly is the customary way to comment lines then the default behavior of `Ctrl-I` (auto indent) should ignore lines that start with `%`.
Ryan Mott
Ryan Mott 2022년 1월 4일
편집: Ryan Mott 2022년 1월 4일
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.

댓글을 달려면 로그인하십시오.

카테고리

Help CenterFile Exchange에서 Get Started with MATLAB에 대해 자세히 알아보기

제품

Community Treasure Hunt

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

Start Hunting!

Translated by