vim-commentary icon indicating copy to clipboard operation
vim-commentary copied to clipboard

Commenting visual block with an embedded empty comment line.

Open dvogel opened this issue 4 years ago • 1 comments

If I have an "empty" comment line inside a visual block, gc eats the space preceding %s in my commentstring (or b:commentary_format). I've captured this in a gist but, I will illustrate the before and after here.

Visually selecting this entire block and commenting (with commentstring=#\ %s):

resource aws_subnet a-subnet {
  # this line is awesome:
  cidr_block = "10.0.0.0/24"
}

Yields the properly indented block:

# resource aws_subnet a-subnet {
#   # this line is awesome:
#   cidr_block = "10.0.0.0/24"
# }

However, if I introduce an empty comment line like so:

resource aws_subnet a-subnet {
  # this is the first line of a comment
  # 
  # this line is awesome:
  cidr_block = "10.0.0.0/24"
}

Then visually selecting the entire block and commenting it yields:

#resource aws_subnet a-subnet {
#  # this is the first line of a comment
#  # 
#  # this line is awesome:
#  cidr_block = "10.0.0.0/24"
#}

This is true regardless of whether that "empty" comment line has a trailing space.

dvogel avatar Apr 29 '21 20:04 dvogel

I tracked down the issue to these two pull requests:

  • https://github.com/tpope/vim-commentary/commit/e0f4850d78137a35b9461c530078bd77f39e4dce
  • https://github.com/tpope/vim-commentary/commit/89f43af18692d22ed999c3097e449f12fdd8b299

which stem from the initially contentious issue, #40

Of course, the issue is a result of this line: https://github.com/tpope/vim-commentary/blob/349340debb34f6302931f0eb7139b2c11dfdf427/plugin/commentary.vim#L19

Although, I must say it's weird that it does not happen in visual-block, and I have not fully investigated that aspect.

Maybe:

  1. b:commentary_strip_empty option (like b:commentary_startofline) or ..
  2. fix root of the problem and check if line is "equal" to just an "empty comment" or something along those lines

If you want me to try my hand at a pull req, I can. If you can spot an easy solution, then please do fix.

novasenco avatar Apr 29 '21 20:04 novasenco