Smart indent and comments
68 次查看(过去 30 天)
显示 更早的评论
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?
采纳的回答
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.
0 个评论
更多回答(4 个)
Lockywolf
2019-6-20
Three years, no definitive answer. Sad.
1 个评论
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
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!
0 个评论
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
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
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
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
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.
另请参阅
产品
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!