AndrewRadev/splitjoin.vim

Feature request: Add Scala support

jsatk opened this issue · 3 comments

jsatk commented

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

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.

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

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:

let items = sj#ParseJsonObjectBody(from + 1, to - 1)
if empty(items)
return 0
endif
if sj#settings#Read('trailing_comma')
let body = start."\n".join(items, ",\n").",\n".end
else
let body = start."\n".join(items, ",\n")."\n".end
endif
call sj#ReplaceMotion('Va'.start, body)

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

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