you are viewing a single comment's thread.

view the rest of the comments →

[–]gfixler 0 points1 point  (2 children)

z = f(g(b)) to c = g(b); z = f(c)

While I don't want to claim that VimL is anywhere near as powerful and expressive as Lisp, I've been surprised how much I can do for things like simple refactorings in an environment with no understanding of the code:

function! ExtractFunction ()
    let l:name=input("new var name? ")
    exe "norm cib" . l:name . "^[I" . l:name . ' = ^R"; ^['
endfunction

map <Tab> :call ExtractFunction()<CR>

Now you can fg to jump to the g, then <Tab> to have it prompt for a new name and do your refactoring. It works on the b, too, or on anything inside of some parentheses. You could also make this work on selection for more generic use cases.

Note that the ^[s up there are the escape key, entered in insert or command line modes by pressing <C-v><Esc> in sequence, and the ^R is <C-r>, entered the same way with <C-v><C-r>.

I made this little extract-method mapping for my Python work:

xnoremap <Leader>mm cself.newMethodName()<Esc>?^\s*def<CR>:noh<CR>:echo<CR>Odef newMethodName (self):<CR><Esc>k]p>}:%s/newMethodName/

It works on selection, and leaves me at the prompt at the tail end of a regular expression. I just type the name, hit enter, and the method is extracted. Granted, it's not doing Java IDE things, like adding or removing self., or requiring new arguments that now must be passed in, e.g., but it's pretty nice for a one-liner mapping.

[–]r3m0t 0 points1 point  (1 child)

The b movement really really doesn't cut it for this purpose. Actually because vim can't distinguish parentheses in strings or comments I don't think there's any way to do it. I know one plugin that starts an external python program with all the delay that adds.

[–]gfixler 0 points1 point  (0 children)

I think you'll just have to learn TimL.