Automatically align comments to the right hand side of the editor

39 visualizaciones (últimos 30 días)
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
Daniel Shub
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
Jan el 8 de Oct. de 2011
I've edited the title: "left" -> "right". It was too confusing...

Iniciar sesión para comentar.

Respuesta aceptada

Gabriele Bedon
Gabriele Bedon el 31 de Oct. de 2018
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 comentarios
darova
darova el 1 de Abr. de 2020
Your code works beautifully. Thank you
Abbot Von
Abbot Von el 22 de Abr. de 2020
Your code works well. Thank you very much.

Iniciar sesión para comentar.

Más respuestas (2)

Jan
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
Jan
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
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?

Iniciar sesión para comentar.


Daniel Shub
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
Daniel Shub
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
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.

Iniciar sesión para comentar.

Categorías

Más información sobre Characters and Strings en Help Center y File Exchange.

Etiquetas

Community Treasure Hunt

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

Start Hunting!

Translated by