-
Notifications
You must be signed in to change notification settings - Fork 21
Expand file tree
/
Copy pathrubyblock.vim
More file actions
85 lines (69 loc) · 2.1 KB
/
rubyblock.vim
File metadata and controls
85 lines (69 loc) · 2.1 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
if exists('g:loaded_textobj_rubyblock') "{{{1
finish
endif
" Interface "{{{1
call textobj#user#plugin('rubyblock', {
\ '-': {
\ '*sfile*': expand('<sfile>:p'),
\ 'select-a': 'ar', '*select-a-function*': 's:select_a',
\ 'select-i': 'ir', '*select-i-function*': 's:select_i'
\ }
\ })
" Misc. "{{{1
let s:comment_escape = '\v^[^#]*'
let s:block_openers = '\zs(<def>|<if>|<unless>|<do>|<module>|<class>|<case>'
let s:block_openers .= '|<for>|<while>|<until>|<begin>)'
let s:start_pattern = s:comment_escape . s:block_openers
let s:mid_pattern = s:comment_escape
let s:mid_pattern .= '\zs(<else>|<elsif>|<when>|<rescue>|<ensure>)'
let s:end_pattern = s:comment_escape . '\zs<end>'
let s:skip_pattern = 'getline(".") =~ "\\v\\S\\s<(if|unless)>\\s\\S"'
if !exists('g:textobj_rubyblock_mids')
let g:textobj_rubyblock_mids = 0
endif
function! s:select_a()
let s:flags = 'W'
call searchpair(s:start_pattern,'',s:end_pattern, s:flags, s:skip_pattern)
let end_pos = getpos('.')
" Jump to match
normal %
let start_pos = getpos('.')
return ['V', start_pos, end_pos]
endfunction
function! s:select_i()
let s:flags = 'W'
if expand('<cword>') == 'end'
let s:flags = 'cW'
endif
let l:mid = ''
if g:textobj_rubyblock_mids
let l:mid = s:mid_pattern
endif
call searchpair(s:start_pattern,l:mid,s:end_pattern, s:flags, s:skip_pattern)
" Move up one line, and save position
normal k^
let end_pos = getpos('.')
if g:textobj_rubyblock_mids
" Move down again, find match right before, and save position
normal j^
let l:last_position = getpos('.')
let l:start_line = l:last_position[1]
normal %
while getpos('.')[1] != l:start_line
let l:last_position = getpos('.')
normal %
endwhile
call setpos('.', l:last_position)
normal j^
let start_pos = getpos('.')
else
" Move down again, jump to match, then down one line and save position
normal j^%j
let start_pos = getpos('.')
endif
return ['V', start_pos, end_pos]
endfunction
" Fin. "{{{1
let g:loaded_textobj_rubyblock = 1
" __END__
" vim: foldmethod=marker