Automatically align comments to the right hand side of the editor
57 次查看(过去 30 天)
显示 更早的评论
Hello here
Does anybody has a workaround whereby you can automatically align the comments inserted after the code to the right hand side of the text editor? To clarify, something like
if called % first comment aligned to the right
do this
else % second comment aligned too
do nothing
end % need to align automatically
Any ideas? Many thanks
6 个评论
Daniel Shub
2011-9-21
You might want to change left to right in your question title since the answers are very different.
采纳的回答
Gabriele Bedon
2018-10-31
I had the same issue, I modified the code from Alessandro Masullo to do the task (attached). I know it's an old question, but it will be maybe useful in the future :)
2 个评论
更多回答(2 个)
Jan
2011-9-20
Not an answer, but code formatting is needed for this comment:
I prefer this, because I think it is easier to read:
if called % first comment aligned to the right
do this
else % second comment aligned too
do nothing
end % need to align automatically
4 个评论
Jan
2011-9-21
@Javer: I do not understand. In the above example the commands have different length and all comments do appear in the same column.
Due to the difficulties in recogizing all comments with 100% perfection, I hesitate to program an automatic source-code layouter. It might be extremly hard to debug. Imagine that the modified code joins a line such that the right part is far beyond the visible area in the window. You will not find this by going line by line through the code, even not if you use the debugger:
disp('This is a friendly command') <end of window> quit
I do have a stable "isQuoted" function and a simple "findComment" function (which does not find %{ and ... comments and fails for strange but valid EVAL commands). But I would not allow them to modify my holy source code.
Andrew Sanchez
2018-8-16
Is there a way to do this automatically such that the comments are all aligned on the same column on the right?
Daniel Shub
2011-9-21
If you do not need to do it online, you might be able to do it. First, you could parse the file to find end of line comments. This is going to be hard:
sprintf('Bill''s score of %d%%is less than 50%%which is %s.', 10, 'sad'); % Where does the end of line comment start?
That said, the MATLAB editor can do it, so you should be able to also. Once you know where the end of line comments start, you can figure out how long the comments are. Then you can adjust the comment starting position appropriately.
EDIT 22/9/11 Thinking about it a little more, you may want to type your end of line comments as
x = 10;%[eolc] This is an end of line comment
Then you could try and convince the syntax highlighting that %[eolc] is special ...
5 个评论
Daniel Shub
2011-9-22
Yair has something on his blog:
http://undocumentedmatlab.com/blog/syntax-highlighted-labels-panels/
that makes me think you could do it. He shows how to take a string (which I am guessing could be an entire file) and can display it with syntax highlighting. It is based on the java com.mathworks.widgets.SyntaxTextPane method.
Jan
2011-9-22
@Daniel: This Java methods highlights the text correctly *on the screen*, but there is no output, which can be used programmatically. You could create a copy of the screen and use some OCD methods to parse the pixel-image.
As I said already, I think I have an M-function which identifies comments. But I will not promise that it works 100% perfectly. And therefore I will not allow it to modify my source code.
E.g. UTF8-Unicode characters or special ASCII characters with code 0 to 31 might have unexpected side-effects.
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Entering Commands 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!