Switch {} and do end in ruby files
Closed this issue · 7 comments
Hi,
Is it possible to add switch between {}
and do end
in ruby files? I am actively refactoring code all the time and cleaning up using rubocop advise. Changing block syntax is not very fun.
Thanks.
This plugin is useful for limited changes on the current line. The transformation you're talking about is already implemented in a different plugin of mine, splitjoin.
I want to do that on separate lines. Splitjoin changes these, but also the code inside.
Like this:
map ▐{ |m|
...
}
:Switch
map ▐do |m|
...
end
Can we please re-open this? I am bumping into that again and again when refactoring long lines. Yes, splitjoin does that if you do that upfront, but if you already have lines separated you can't simply change {}
. Or if you have lines joined you can't change do end
easily.
Okay, maybe I am using it wrong. How one would go about changing ->{}
to lambda do end
here?
-> {
post '/api/v1/inquiries', data
post '/api/v1/inquiries', data_body
expect(response.status).to eq 201
}
Sorry for taking a while, I've been a bit busy these days.
Right now, it's impossible to do this with switch.vim, since it only works within the limits of a single line. I can't really imagine how it would be possible to fit this into the plugin, simply in terms of interface. Finding the matching }
would be perfectly possible, but it's not really something that I can describe with the existing switch.vim machinery.
However, you could get this example done with Tim Pope's surround.vim. You'd need to add something like this to your ftplugin/ruby.vim
file:
let b:surround_{char2nr('d')} = "do\n \r end"
This defines a do
/end
surrounding bound to the d
key, which means you can wrap lines in do-end "brackets", but it also means you can change an existing pair of brackets to it. In your lambda example, simply typing cs{d
would "change the surrounding { to do-end", effectively doing what you want to do, except for changing the ->
to a lambda. For that one, you could use switch.vim, or maybe just record a macro with the combination. I've been planning to implement macros in switch.vim for a while now (instead of performing a regex substitution, just run a series of vim keys), but let's see.
Thank you for taking time to explain things.
I am actively using surround
, switch
and splitjoin
. Just wanted to have some streamlined code refactoring workflow. But it didn't occured to me previously that switch
is a single-line plugin.
Maybe it is time to look into compiling some sort of 'refactor-ruby.vim' or something. But my knowledge of Vim innards is pretty basic at the moment.
Well, I think the best way to get to know Vimscript is to try to solve your own problem :). That's how I did it, anyway. Vim has a pretty extensive help system, and as long as you follow the tracks, you'll be able to figure out how to use it in no time. Something like LVSTHW would definitely help as well.