Skip to content

Commit 947e24c

Browse files
jgdaveyvim-scripts
authored andcommitted
Version 0.1: Initial upload
0 parents  commit 947e24c

File tree

3 files changed

+200
-0
lines changed

3 files changed

+200
-0
lines changed

README

+24
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
This is a mirror of http://www.vim.org/scripts/script.php?script_id=3373
2+
3+
This plugin allows rapid toggling between the two different styles of ruby
4+
blocks, namely do/end and brackets {}. To use, simply move the cursor to the
5+
beginning or end of a block, and type <Leader>b. As a mnemonic, remember 'b'
6+
for 'block'.
7+
8+
Note: You must have your cursor on the do, end, {, or } for the plugin to work
9+
correctly.
10+
11+
When moving from a do/end to a bracket-style block, the plugin with attempt to
12+
move to a one-liner if appropriate:
13+
14+
For example, if have the following ruby code:
15+
16+
['one', 'two'].each do |number|
17+
puts number + "!"
18+
end
19+
20+
After invoking <Leader>b, the resulting code would be:
21+
22+
['one', 'two'].each { |number| puts number + "!" }
23+
24+

doc/blockle.txt

+41
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
*blockle.txt* Plugin for toggling ruby block styles
2+
3+
Author: Joshua Davey <[email protected]> *blockle-author*
4+
License: Same terms as Vim itself (see |license|)
5+
6+
This plugin is only available if 'compatible' is not set, and will only work
7+
correctly if matchit.vim is available.
8+
9+
INTRODUCTION *blockle*
10+
11+
This plugin allows rapid toggling between the two different styles of ruby
12+
blocks, namely do/end and brackets {}. To use, simply move the cursor to the
13+
beginning or end of a block, and type <Leader>b. As a mnemonic, remember 'b'
14+
for 'block'.
15+
16+
Note: You must have your cursor on the do, end, {, or } for the plugin to work
17+
correctly.
18+
19+
When moving from a do/end to a bracket-style block, the plugin with attempt to
20+
move to a one-liner if appropriate:
21+
22+
For example, if have the following ruby code (* indicates cursor position):
23+
>
24+
['one', 'two'].each d*o |number\
25+
puts number + "!"
26+
end
27+
28+
After invoking <Leader>b, the resulting code would be:
29+
>
30+
['one', 'two'].each *{ |number\ puts number + "!" }
31+
>
32+
33+
MAPPINGS *blockle-mappings*
34+
35+
<Leader>b or <Plug>BlockToggle
36+
Toggle ruby block style
37+
38+
*blockle-settings*
39+
This plugin has no settings.
40+
41+
vim:tw=78:et:ft=help:norl:

plugin/blockle.vim

+135
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,135 @@
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

Comments
 (0)