Skip to content

Commit b9b5339

Browse files
jgdaveyvim-scripts
authored andcommitted
Version 0.3: Better detection when inside of a block
1 parent 5323ab8 commit b9b5339

File tree

2 files changed

+24
-18
lines changed

2 files changed

+24
-18
lines changed

doc/blockle.txt

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,13 @@ MAPPINGS *blockle-mappings*
3535
<Leader>b or <Plug>BlockToggle
3636
Toggle ruby block style
3737

38+
To add your own mapping, add something like the following to your vim configuration:
39+
>
40+
map <D-j> <Plug>BlockToggle
41+
<
42+
which would map Super-J (Cmd-J on Mac OS) to the toggling function.
43+
44+
3845
*blockle-settings*
3946
This plugin has no settings.
4047

plugin/blockle.vim

Lines changed: 17 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,17 @@
11
" blockle.vim - Ruby Block Toggling
22
" Author: Joshua Davey <[email protected]>
3-
" Version: 0.2
3+
" Version: 0.3
44
"
55
" Licensed under the same terms as Vim itself.
66
" ============================================================================
77

88
" Exit quickly when:
99
" - this plugin was already loaded (or disabled)
1010
" - when 'compatible' is set
11-
if (exists("g:loaded_blockle") && g:loaded_blockle) || &cp
12-
finish
13-
endif
14-
let g:loaded_blockle = 1
11+
" if (exists("g:loaded_blockle") && g:loaded_blockle) || &cp
12+
" finish
13+
" endif
14+
" let g:loaded_blockle = 1
1515

1616
let s:cpo_save = &cpo
1717
set cpo&vim
@@ -96,32 +96,29 @@ endfunction
9696

9797
function! s:goToNearestBlockBounds()
9898
let char = getline('.')[col('.')-1]
99-
if char =~ '[{}]'
99+
if char == '{' || char == '}'
100100
return char
101101
endif
102-
103102
let word = expand('<cword>')
104-
if word =~ '\vdo|end'
103+
if (word == 'do' || word == 'end') && char != ' '
105104
return word
106-
endif
107-
108-
let endline = line('.')+5
109-
if search('\vend|}', 'cs', endline)
105+
elseif searchpair('{', '', '}', 'bcW') > 0
106+
return getline('.')[col('.')-1]
107+
elseif searchpair('\<do\>', '', '\<end\>\zs', 'bcW',
108+
\ 'synIDattr(synID(line("."), col("."), 0), "name") =~? "string"') > 0
110109
return expand('<cword>')
111110
endif
112111

113112
return ''
114113
endfunction
115114

116115
function! s:ToggleDoEndOrBrackets()
117-
if &ft!='ruby' | return | endif
118-
119116
let block_bound = s:goToNearestBlockBounds()
120117

121118
if block_bound =='{' || block_bound == '}'
122-
call <SID>ConvertBracketsToDoEnd()
119+
call s:ConvertBracketsToDoEnd()
123120
elseif block_bound ==# 'do' || block_bound ==# 'end'
124-
call <SID>ConvertDoEndToBrackets()
121+
call s:ConvertDoEndToBrackets()
125122
else
126123
echo 'Cannot toggle block: cursor is not on {, }, do or end'
127124
endif
@@ -131,8 +128,10 @@ endfunction
131128

132129
nnoremap <silent> <Plug>BlockToggle :<C-U>call <SID>ToggleDoEndOrBrackets()<CR>
133130
134-
map <leader>b <Plug>BlockToggle
135-
131+
augroup blockle
132+
autocmd!
133+
autocmd FileType ruby map <buffer> <leader>b <Plug>BlockToggle
134+
augroup END
136135

137136
let &cpo = s:cpo_save
138137

0 commit comments

Comments
 (0)