remyferre / comment-dwim-2

A replacement for the emacs' built-in command `comment-dwim'
GNU General Public License v3.0
101 stars 5 forks source link

Change the cycling to include re-indenting an existing inline comment #1

Closed darcamo closed 10 years ago

darcamo commented 10 years ago

Currently if there is already an inline comment and M-; is pressed multiple times the inline comment will be killed at some point and the cursor will be at the end of the line. I understand that this is how it was designed to behave.

However, in my common usage re-indenting an inline comment is something I need more commonly than killing the inline comment. Would it be possible to have the cycling to instead of killing the comment just realigned it and put the cursor at the end of the code (before any white-space preceding the inline comment)? With the cursor at before the inline comment it would be easy to kill the comment with 'C-k' if that was the intention and comment-dwim-2 would have the extra feature of re-indenting the comment to the correct position.

Ex: Using 'M-;' twice in the second line of the python code below would fix the start of the inline comment, which will often get wrong after refactoring the code, for instance.

some_var = 2              # largest diagonal element
some_other_var = 3             # smallest diagonal element

ps: Thanks for this nice package.

remyferre commented 10 years ago

IIRC my initial implementation of comment-dwim-2 was pretty much what you described, so I guess it wasn't such a bad idea after all :-)

I didn't like the reindent feature (I don't use it) so I chose to put comment-kill when encountering an inline comment instead but I don't think it'll be hard to add a flag to choose between the two features.

Just to be sure, the reindent behavior you want is what comment-dwim (the emacs function) does when called with a line which have an inline comment ?


Maybe it can give you some ideas so I will explain what comment-dwim-2 did when I first programmed it.

Let's say we have this:

some_var = 2 # largest diagonal element

Calling comment-dwim-2 a first time does as expected :

# some_var = 2 # largest diagonal element

Calling comment-dwim-2 a second time reindents the comment (what you want, it uses comment-dwim to do so):

some_var = 2             # largest diagonal element

Calling comment-dwim-2 a third time kills the comment:

some_var = 2

This way to kill the comment we don't have to care about the point or hitting "C-k", calling comment-dwim-2 repetitively is enough.

What do you think of this ? Would it suits your needs ?

darcamo commented 10 years ago

Yes, the indent behavior is what comment-dwim does. With comment-dwim you can also delete an inline comment if you pass the 'C-u' prefix. That is, with 'C-u M-;'. I didn't know about this before posting the issue and that is why I mentioned the point position. No need for that.

The best thing about comment-dwim-2 is definitely not having to mark the line first to comment/uncomment it. Losing the ability to reindent the inline comment, however, means that I have to choose to either stick with the standard comment-dwim for that or use comment-dwim-2 for the easier comment/uncomment feature.

What you propose does suits my needs, although as a matter of personal preference I would prefer if the killing part was not in the cycling and was instead consistent with the standard comment-dwim (only kill with the C-u prefix). That is just personal preference and it is up to you.

remyferre commented 10 years ago

Hmm interesting, I didn't think of using the C-u prefix.

I need to think about that, I will choose the more interoperable solution when implementing.

remyferre commented 10 years ago

Done! You can upgrade the package from MELPA.

In the end I used the C-u prefix to kill comment as you proposed. Thanks for the idea.

Since they are two possible modes now you need to add the following line in your dotemacs to have the behavior you want:

(setq comment-dwim-2--inline-comment-behavior 'reindent-comment)

FYI the default value is 'kill-comment, in that case C-u will reindent the comment.

darcamo commented 10 years ago

Thanks for this! It works as intended, except for a small caveat when comment-dwim-2--inline-comment-behavior is set to reindent-comment.

Consider the code below

a = 10 # Set the value of a

Pressing M-; once will comment as desired. Pressing a second time will uncomment, reindent the inline comment and put the cursor after the '#' of the inline comment. The problem is that pressing M-; again will do nothing, instead of commenting the whole line again. Only if I move the cursor and then press M-; is that the whole line will be commented.

remyferre commented 10 years ago

I propose a WONTFIX for this one.

Cycling isn't really an intended feature of comment-dwim-2, it does work with the other behavior but only because killing the comment ends up with a fortunate clean state without comment which causes comment-dwim-2 to loop.

I understand it can be confusing because I add a gif of comment-dwim-2 cycling in the README, but only because it was a good way to see more features at once (and because it feels fancy :) ). But cycling by itself does not provide any features/use cases.

Cycling for 'reindent-comment is actually implementable, but it would need to add a global variable to keep track of the last action, which isn't really maintainable, so I don't think it is worth it.

darcamo commented 10 years ago

It makes sense. Thank for your work.