tpope / vim-commentary

commentary.vim: comment stuff out
http://www.vim.org/scripts/script.php?script_id=3695
5.9k stars 214 forks source link

Commenting visual block with an embedded empty comment line. #144

Open dvogel opened 3 years ago

dvogel commented 3 years ago

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.

novasenco commented 3 years ago

I tracked down the issue to these two pull requests:

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.