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

Feature request: Add Scala support #188

Open jsatk opened 2 years ago

jsatk commented 2 years ago

Great plugin.

I primarily work in Scala these days however and I miss being able to use this plugin.

Namely I often have cause to turn this:

def f(a: Int, b: Int, c: Int): Int = ???

into...

def f(
  a: Int,
  b: Int,
  c: Int,
): Int = ???

or the reverse.

I forked & cloned this repo myself and have started poking around. I have some very basic Scala support going but still can't quite accomplish this. I'm okay at regex but I don't know I quite have the chops for some of what you do in here.

Would it be possible for me to open a WIP PR and get feedback from you or other contributors to realize some basic Scala support?

Thanks Andrew!

jsatk commented 2 years ago

To expand on the above. I've been reading through your files for various other languages I know but am still a bit stumped. I think the = in function definitions is throwing things off in my regex.

AndrewRadev commented 2 years ago

If you'd like to submit a WIP PR, I'd be happy to take a look and suggest fixes :+1:

I think the = in function definitions is throwing things off in my regex.

For something like splitting the contents of a function definition, I think you could use an argparser instead of a single regex, something like this: https://github.com/AndrewRadev/splitjoin.vim/blob/dbcd3069fb2b4ecfdd964c1e93aa59fcf7f850b6/autoload/sj/js.vim#L164-L175

So, something like "search backwards for a def \k\+(, then save the starting point of the ( and normal! % to the closing )". With a start and end position, you could try parsing the individual arguments via sj#ParseJsonObjectBody.

There might be complications with types, not sure, you could also take a look at https://github.com/AndrewRadev/splitjoin.vim/blob/main/autoload/sj/argparser/rust.vim for a custom parser implementation used for Rust struct definitions.

jsatk commented 2 years ago

Thanks so much @AndrewRadev. I'll try to get a WIP PR out this week for basic support. Appreciate the kind reply.