Open purcell opened 3 years ago
I've been running into this for a while. Created a workaround that seems to work for now.
(defun heartman/comment-whole-line-or-region (prefix)
"Override comment-whole-line-or-region.
The original function doesn't work well with empty lines, so let's change create a wrapper."
(interactive "*p")
(if (heartman/current-line-empty-p)
(funcall-interactively 'comment-dwim prefix)
(funcall-interactively 'whole-line-or-region-comment-dwim-2 prefix)))
(defun heartman/current-line-empty-p ()
"Check whether the current line is empty."
(save-excursion
(beginning-of-line)
(looking-at-p "[[:space:]]*$")))
There may be easier ways to do it and this may not cover everything, but it seems to work right now, at least.
When on an empty line with no region active,
comment-dwim
would normally insert the comment prefix before point. But with a region active - selecting the empty line - it does nothing. This means that it doesn't work as expected in this situation when wrapped bywhole-line-or-region
.