Automatically align comments to the right hand side of the editor
Mostrar comentarios más antiguos
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 comentarios
Sean de Wolski
el 20 de Sept. de 2011
Huh. You could probably do it with a complicated textscan/regexp/fwrite scheme. I know of no trivial solution.
Good luck!
Walter Roberson
el 20 de Sept. de 2011
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?
Jason Ross
el 20 de Sept. de 2011
What would happen when you re-sized the editor?
Javer
el 21 de Sept. de 2011
Daniel Shub
el 21 de Sept. de 2011
You might want to change left to right in your question title since the answers are very different.
Jan
el 8 de Oct. de 2011
I've edited the title: "left" -> "right". It was too confusing...
Respuesta aceptada
Más respuestas (2)
Jan
el 20 de Sept. de 2011
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 comentarios
Javer
el 21 de Sept. de 2011
Jan
el 21 de Sept. de 2011
@Javer: I do not understand. In the above example the commands have different length and all comments do appear in the same column.
Jan
el 21 de Sept. de 2011
@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
el 16 de Ag. de 2018
Is there a way to do this automatically such that the comments are all aligned on the same column on the right?
Daniel Shub
el 21 de Sept. de 2011
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 comentarios
Jan
el 21 de Sept. de 2011
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!
Daniel Shub
el 22 de Sept. de 2011
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?
Jan
el 22 de Sept. de 2011
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.
Daniel Shub
el 22 de Sept. de 2011
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
el 22 de Sept. de 2011
@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.
Categorías
Más información sobre Environment and Settings en Centro de ayuda y File Exchange.
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!