Automatically align comments to the right hand side of the editor

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 Comments

Huh. You could probably do it with a complicated textscan/regexp/fwrite scheme. I know of no trivial solution.
Good luck!
What should happen with comments too long to fit on the end of the line?
What should happen with lines that are too long to fit any comment on?
What would happen when you re-sized the editor?
Walter: this is just for short comments that I sometimes make about certain lines of my code. Long comments (or the help comments at the beginning of the file) would just go on their own line. I have seen it on somebody else’s code and it looks far clearer than the way I normally do it.
Jason: obviously this is a work-around for me. If I resized the editor, I might not see some of the comments, but I have no reason to do that in the screen I normally use.
You might want to change left to right in your question title since the answers are very different.
I've edited the title: "left" -> "right". It was too confusing...

Sign in to comment.

 Accepted Answer

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 Comments

Your code works beautifully. Thank you
Your code works well. Thank you very much.

Sign in to comment.

More Answers (2)

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 Comments

I have done it that way many times, but as commands’ length varies from line to line I have to keep ‘tabbing’ my comments so they all appear on the same column.
@Javer: I do not understand. In the above example the commands have different length and all comments do appear in the same column.
@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.
Is there a way to do this automatically such that the comments are all aligned on the same column on the right?

Sign in to comment.

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 Comments

It is not trivial to determine if a % is a comment or part of a string. Even the "isquoted" subroutine of Matlab's SYMVAR command fails (at least until R2009a) for the line:
Str = 'This is a quote character: ''' % Comment
In addition there are the new %{ %} comments and everything behind "..." is a comment also - except if the three dots appear inside a string, or inside a comment!
I said it is going to be hard. The MATLAB editor, however, is able to do the parsing of comments correctly, so it means that it is possible. You might even be able to hack the api to get at the syntax highlighting portion of the editor. The editor in r2011a also handles the string part correctly in your example. What does the editor (and mlint) do with your example in r2009a?
The editor and MATLAB find the comment perfectly, but there is no publix API function to do this. Therefore you have to write it by your own.
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.
@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.

Sign in to comment.

Categories

Find more on Environment and Settings in Help Center and File Exchange

Tags

Asked:

on 20 Sep 2011

Commented:

on 22 Apr 2020

Community Treasure Hunt

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

Start Hunting!