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.91k stars 89 forks source link

Add support for Elixir arrays #163

Closed frm closed 4 years ago

frm commented 4 years ago

This PR adds support for array splitting and joining in Elixir.

Works with the following examples:

[a, b, c]

[
  a,
  b,
  c
]

[a, b, c | d]

[
  a,
  b,
  c 
  | d
]

[a: 1, b: 2, c: %{a: 1, b: 2}]

[
  a: 1,
  b: 2,
  c: %{a: 1, b: 2}
]

[a: 1, b: 2 | [c: %{d | e: 1}]]

[
  a: 1,
  b: 2 
  | [c: %{d | e: 1}]
]

[1, 2, 2 |> Kernel.+(1) | [4]]

[
  1,
  2,
  2 |> Kernel.+(1) 
  | [4]
]

I did manage to find an edge case where this doesn't work:

[a: 1, b: 2, c: %{d | e: 1}]

[
  a: 1,
  b: 2,
  c: %{d 
    | e: 1}
]

However, to handle this it's necessary to build a parser and given it's such an uncommon edge case (updating a map when declaring the last element of an array), I assumed it wouldn't be worth the effort.

frm commented 4 years ago

On a sidenote: as a byproduct of this, I managed to add some missing tests for Elixir and I'm currently working my way through maps and arrays via sigils.

I'll be making a few more PRs adding those.

frm commented 4 years ago

Thanks for reviewing! I'll be handling these later today.

I did have some issues running the tests with indentation (especially adding tests to existing do-blocks on a different branch) because macvim also has some issues with indentation. It'd really help if I could add a submodule for vim-elixir.

In fact, it already depends on it since the delimiters used in sp#elixir#SplitArray() are in fact provided by vim-elixir. I actually forgot to mention in the PR description, but I assumed that dependency since I'm 99.99% certain someone working with Elixir in Vim to the point they'd need to use splitjoin, would be using vim-elixir. I think a similar dependency already exists with the Ruby parser and vim-ruby.

AndrewRadev commented 4 years ago

I assumed that dependency since I'm 99.99% certain someone working with Elixir in Vim to the point they'd need to use splitjoin, would be using vim-elixir. I think a similar dependency already exists with the Ruby parser and vim-ruby.

Yes, there's a lot of dependencies, to Rust as well. A couple of days ago, I decided to vendor these in specs, and this seems to be working fine. Here's the lines that load the plugins: https://github.com/AndrewRadev/splitjoin.vim/blob/99d12007112b63b8e3fd0fcf435471ac63ccf030/spec/spec_helper.rb#L14-L16

And here's the .gitmodules file: https://github.com/AndrewRadev/splitjoin.vim/blob/master/.gitmodules

So, you could do the same for vim-elixir, but I can do that for you as well -- if you'd like, focus on your fix and I'll get the tests running afterwards.

frm commented 4 years ago

Sorry it took me a couple of months but finally got to handle this. I think these scenarios should now all be handled :)