Automatically align comments to the right hand side of the editor

56 visualizzazioni (ultimi 30 giorni)
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 Commenti
Daniel Shub
Daniel Shub il 21 Set 2011
You might want to change left to right in your question title since the answers are very different.
Jan
Jan il 8 Ott 2011
I've edited the title: "left" -> "right". It was too confusing...

Accedi per commentare.

Risposta accettata

Gabriele Bedon
Gabriele Bedon il 31 Ott 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 :)

Più risposte (2)

Jan
Jan il 20 Set 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 Commenti
Jan
Jan il 21 Set 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 il 16 Ago 2018
Is there a way to do this automatically such that the comments are all aligned on the same column on the right?

Accedi per commentare.


Daniel Shub
Daniel Shub il 21 Set 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 Commenti
Daniel Shub
Daniel Shub il 22 Set 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 il 22 Set 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.

Accedi per commentare.

Categorie

Scopri di più su Entering Commands in Help Center e File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by