AndrewRadev / splitjoin.vim

Switch between single-line and multiline forms of code
http://www.vim.org/scripts/script.php?script_id=3613
MIT License
1.93k stars 90 forks source link

Unexpected modifications when using `gS` / `gJ` #194

Open Kawsay opened 2 years ago

Kawsay commented 2 years ago

Vim version: 9.0 language: Ruby

With a line like:

foo = [ {a: 1, b: 2}, {c: 3, d: 4} ]

placing the cursor on the beginning of the array ([) then pressing gS gives me:

foo = [
  {a: 1, b: 2},
  c: 3,
  d: 4
]

when I'm expecting:

foo = [
  {a: 1, b: 2},
  {c: 3, d: 4}
]

Also, but I guess it's known and it's clearly and edge-case, joining modules with named as a single letter fails:

module A
  module B
  end
end

# cursor on A then gJ
module A module B
  end
end

Both modules need to be named with multiple letters in order to be successfully joined

module Aa
  module Bb
  end
end

# cursor on Aa then gJ
module Aa::Bb
end

# works like a charm, thanks for your wonderful plugin !
AndrewRadev commented 2 years ago

# works like a charm, thanks for your wonderful plugin !

Thank you kindly :)

I've pushed fixes to both issues. For the first one, it was something that worked as a side effect of arrays being essentially split like function calls -- syntactically, expanding the last hash like that is a thing that works and I've had issues and PRs related to this. But it seems I forgot to consider that it makes sense not to do it by default, so I've added a new option that enables it, ruby_expand_options_in_arrays, and your case should work correctly out of the box now.

The second issue just looks like an oversight on my part. I've tweaked the relevant regexes to allow a single capital variable to work as a module name. I don't see any issues in the tests, so I think it should be safe.

Let me know if both of these fixes work for you.