Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add option to acknowledge middle keywords in ir #30

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,11 @@ Note too that the `ar` and `ir` text objects always position your cursor on
the `end` keyword. If you want to move to the top of the selection, you can do
so with the `o` key.

By default `ir` ignores middle keywords like `else` and `when`, but you can have
`ir` acknowledge them by putting this in your .vimrc

let g:textobj_rubyblock_mids = 1

Limitations
-----------

Expand Down
37 changes: 32 additions & 5 deletions plugin/textobj/rubyblock.vim
Original file line number Diff line number Diff line change
Expand Up @@ -13,11 +13,18 @@ call textobj#user#plugin('rubyblock', {

" Misc. "{{{1
let s:comment_escape = '\v^[^#]*'
let s:block_openers = '\zs(<def>|<if>|<do>|<module>|<class>)'
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'

Expand All @@ -37,15 +44,35 @@ function! s:select_i()
let s:flags = 'cW'
endif

call searchpair(s:start_pattern,'',s:end_pattern, s:flags, s:skip_pattern)
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('.')

" Move down again, jump to match, then down one line and save position
normal j^%j
let start_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
Expand Down
25 changes: 25 additions & 0 deletions t/examples.rb
Original file line number Diff line number Diff line change
Expand Up @@ -65,3 +65,28 @@ def hello
bar
end

def method_with_while
var1 = 1
i = 0
while i < 10
i += 1
end
var2 = 2
end

def method_with_unless
var1 = 1
unless condition
puts 'foo'
end
var2 = 2
end

def method_with_if_else
if condition
foo = 'foo'
bar = 'bar'
else
baz = 'baz'
end
end
41 changes: 41 additions & 0 deletions t/rubyblock_test.vim
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,47 @@ describe '<Plug>(textobj-rubyblock-a)'

end

describe 'nested while and unless blocks'

before
silent tabnew t/examples.rb
end

after
silent tabclose
end

it 'ignores nested while and unless blocks'
Expect SelectAroundFrom(69, '^') ==# [69, 68, 75]
Expect SelectAroundFrom(78, '^') ==# [78, 77, 83]
end

end

describe 'g:textobj_rubyblock_mids'
before
silent tabnew t/examples.rb
end

after
silent tabclose
end

context '1'
it 'selects only between mids'
let g:textobj_rubyblock_mids = 1
Expect SelectInsideFrom(87, '^') ==# [87, 87, 88]
end
end

context '0'
it 'ignores mids'
let g:textobj_rubyblock_mids = 0
Expect SelectInsideFrom(87, '^') ==# [87, 87, 90]
end
end
end

describe '<Plug>(textobj-rubyblock-i)'

before
Expand Down