|
| 1 | +" blockle.vim - Ruby Block Toggling |
| 2 | +" Author: Joshua Davey <[email protected]> |
| 3 | +" Version: 0.1 |
| 4 | +" |
| 5 | +" Licensed under the same terms as Vim itself. |
| 6 | +" ============================================================================ |
| 7 | + |
| 8 | +" Exit quickly when: |
| 9 | +" - this plugin was already loaded (or disabled) |
| 10 | +" - when 'compatible' is set |
| 11 | +if (exists("g:loaded_blockle") && g:loaded_blockle) || &cp |
| 12 | + finish |
| 13 | +endif |
| 14 | +let g:loaded_blockle = 1 |
| 15 | + |
| 16 | +let s:cpo_save = &cpo |
| 17 | +set cpo&vim |
| 18 | + |
| 19 | +function! s:ConvertBracketsToDoEnd() |
| 20 | + let char = getline('.')[col('.')-1] |
| 21 | + if char=='}' |
| 22 | + norm % |
| 23 | + endif |
| 24 | + norm h |
| 25 | + " Bracket touching previous word |
| 26 | + if getline('.')[col('.')-1] =~ '[^ ;]' |
| 27 | + exe "norm! a " |
| 28 | + endif |
| 29 | + norm l |
| 30 | + let begin_pos = getpos('.') |
| 31 | + let begin_num = line('.') |
| 32 | + let begin_line = getline('.') |
| 33 | + norm % |
| 34 | + let end_pos = getpos('.') |
| 35 | + let end_num = line('.') |
| 36 | + |
| 37 | + call setpos('.', begin_pos) |
| 38 | + norm! sdo |
| 39 | + call setpos('.', end_pos) |
| 40 | + if getline('.')[col('.')-1] != 'e' |
| 41 | + norm! l |
| 42 | + endif |
| 43 | + norm! send |
| 44 | + call setpos('.', begin_pos) |
| 45 | + " Still need to fix indentation |
| 46 | + |
| 47 | + if begin_num == end_num " Was a one-liner |
| 48 | + " Has block parameters |
| 49 | + if search('\vdo *\|', 'c', begin_num) |
| 50 | + let end_of_line = '2f|' |
| 51 | + else |
| 52 | + let end_of_line = 'e' |
| 53 | + endif |
| 54 | + call setpos('.', end_pos) |
| 55 | + exe "norm! i\<cr>" |
| 56 | + let end_pos = getpos('.') |
| 57 | + call setpos('.', begin_pos) |
| 58 | + exe "norm! ".end_of_line."a\<cr>" |
| 59 | + call setpos('.', begin_pos) |
| 60 | + if search('do|', 'c', begin_num) | :.s/do|/do |/ | endif |
| 61 | + exe begin_num.','.end_num.'Trim' |
| 62 | + call setpos('.', begin_pos) |
| 63 | + endif |
| 64 | +endfunction |
| 65 | + |
| 66 | +function! s:ConvertDoEndToBrackets() |
| 67 | + let char = getline('.')[col('.')-1] |
| 68 | + let w = expand('<cword>') |
| 69 | + if w=='end' |
| 70 | + norm % |
| 71 | + elseif char == 'o' |
| 72 | + norm! h |
| 73 | + endif |
| 74 | + let do_pos = getpos('.') |
| 75 | + let begin_num = line('.') |
| 76 | + norm % |
| 77 | + let end_pos = getpos('.') |
| 78 | + let end_num = line('.') |
| 79 | + |
| 80 | + norm ciw} |
| 81 | + call setpos('.', do_pos) |
| 82 | + norm ciw{ |
| 83 | + |
| 84 | + if (end_num-begin_num) == 2 |
| 85 | + norm! JJ |
| 86 | + " Remove extraneous spaces |
| 87 | + if search(' \+', 'c', begin_num) | :.s/\([^ ]\) \+/\1 /g | endif |
| 88 | + call setpos('.', do_pos) |
| 89 | + endif |
| 90 | +endfunction |
| 91 | + |
| 92 | +function! s:goToNearestBlockBounds() |
| 93 | + let char = getline('.')[col('.')-1] |
| 94 | + if char =~ '[{}]' |
| 95 | + return char |
| 96 | + endif |
| 97 | + |
| 98 | + let word = expand('<cword>') |
| 99 | + if word =~ '\vdo|end' |
| 100 | + return word |
| 101 | + endif |
| 102 | + |
| 103 | + let endline = line('.')+5 |
| 104 | + echo endline |
| 105 | + if search('\vend|}', 'cs', endline) |
| 106 | + return expand('<cword>') |
| 107 | + endif |
| 108 | + |
| 109 | + return '' |
| 110 | +endfunction |
| 111 | + |
| 112 | +function! s:ToggleDoEndOrBrackets() |
| 113 | + if &ft!='ruby' | return | endif |
| 114 | + |
| 115 | + let block_bound = s:goToNearestBlockBounds() |
| 116 | + |
| 117 | + if block_bound =~ '[{}]' |
| 118 | + call <SID>ConvertBracketsToDoEnd() |
| 119 | + elseif block_bound =~ '\vdo|end' |
| 120 | + call <SID>ConvertDoEndToBrackets() |
| 121 | + else |
| 122 | + throw 'Cannot toggle block: cursor is not on {, }, do or end' |
| 123 | + endif |
| 124 | + |
| 125 | + silent! call repeat#set("\<Plug>BlockToggle", -1) |
| 126 | +endfunction |
| 127 | + |
| 128 | +nnoremap <silent> <Plug>BlockToggle :<C-U>call <SID>ToggleDoEndOrBrackets()<CR> |
| 129 | +
|
| 130 | +map <leader>b <Plug>BlockToggle |
| 131 | +
|
| 132 | + |
| 133 | +let &cpo = s:cpo_save |
| 134 | + |
| 135 | +" vim:set ft=vim ff=unix ts=4 sw=2 sts=2: |
0 commit comments