main
1function! s:AddParameter()
2 let name = input("parameter name: ")
3 let cursor_position = getpos(".")
4
5 if empty(matchstr(getline("."), '\<def\>'))
6 exec "?\\<def\\>"
7 endif
8
9 let closing_bracket_index = stridx(getline("."), "(")
10 if closing_bracket_index == -1
11 execute "normal A()\<Esc>"
12 endif
13 exec ':s/)/, ' . name . ')/'
14 exec ':silent! %s/(, /(/'
15
16 call setpos(".", cursor_position)
17endfunction
18
19function! s:InlineTemp()
20 let cursor_position = getpos(".")
21 normal "ayiw
22 normal $"byiW
23 normal dd
24 exec ":.,/end/ s/\\<" . @a . "\\>/" . @b . "/gI"
25 call setpos(".", cursor_position)
26endfunction
27
28function! s:ExtractLet()
29 let cursor_position = getpos(".")
30 normal 0
31 normal! dd
32 exec "?^\\s*\\<\\(describe\\|context\\|let\\)\\>"
33 normal! $p
34 exec 's/\v([a-z_][a-zA-Z0-9_]*) \= (.+)/let(:\1) { \2 }'
35 normal V=
36 call setpos(".", cursor_position)
37 normal jw
38endfunction
39
40function! s:RenameVariable()
41 let cursor_position = getpos(".")
42 let name = input("new name: ")
43 normal "ayiw
44 exec ":.,/end/ s/\\<" . @a . "\\>/" . name . "/gI"
45 call setpos(".", cursor_position)
46endfunction
47
48command! RAddParameter call s:AddParameter()
49command! RInlineTemp call s:InlineTemp()
50command! RExtractLet call s:ExtractLet()
51command! RRenameVariable call s:RenameVariable()
52
53" run rspec test
54nnoremap <leader>r :!ruby %:.<cr>
55nnoremap <leader>t :!bundle exec rspec %:.<cr>
56
57" ruby refactorings
58nnoremap <leader>rap :RAddParameter<cr>
59nnoremap <leader>rit :RInlineTemp<cr>
60nnoremap <leader>rel :RExtractLet<cr>
61nnoremap <leader>rrlv :RRenameVariable<cr>