Skip to content
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
50 changes: 16 additions & 34 deletions autoload/xolox/lua.vim
Original file line number Diff line number Diff line change
Expand Up @@ -197,52 +197,34 @@ function! xolox#lua#jumpblock(forward) " {{{1
return searchpair(start, middle, end, flags, '!xolox#lua#tokeniscode()')
endfunction

function! s:getfunscope()
let firstpos = [0, 1, 1, 0]
let lastpos = getpos('$')
while search('\<function\>', 'bW')
function! s:jumpthisfuncstart()
while search('\<function\>\|\%^', 'bcW')
if xolox#lua#tokeniscode()
let firstpos = getpos('.')
break
endif
endwhile
if xolox#lua#jumpblock(1)
let lastpos = getpos('.')
endif
return [firstpos, lastpos]
endfunction

function! xolox#lua#jumpthisfunc(forward) " {{{1
let cpos = [line('.'), col('.')]
let fpos = [1, 1]
let lpos = [line('$'), 1]
while search('\<function\>', a:forward ? 'W' : 'bW')
function! xolox#lua#jumpfuncstart(forward) " {{{1
while search('\<function\>\|\%^\|\%$', a:forward ? 'W' : 'bW')
if xolox#lua#tokeniscode()
break
endif
endwhile
let cursorline = line('.')
let [firstpos, lastpos] = s:getfunscope()
if cursorline == (a:forward ? lastpos : firstpos)[1]
" make the mapping repeatable (line wise at least)
execute a:forward ? (lastpos[1] + 1) : (firstpos[1] - 1)
let [firstpos, lastpos] = s:getfunscope()
endif
call setpos('.', a:forward ? lastpos : firstpos)
endfunction

function! xolox#lua#jumpotherfunc(forward) " {{{1
let view = winsaveview()
" jump to the start/end of the function
call xolox#lua#jumpthisfunc(a:forward)
" search for the previous/next function
while search('\<function\>', a:forward ? 'W' : 'bW')
" ignore strings and comments containing 'function'
if xolox#lua#tokeniscode()
return 1
endif
endwhile
call winrestview(view)
function! xolox#lua#jumpfuncend(forward) " {{{1
let origline = line('.')
call s:jumpthisfuncstart()
call xolox#lua#jumpblock(1)
if a:forward && line('.') > origline || !a:forward && line('.') < origline
return
endif
call xolox#lua#jumpfuncstart(a:forward)
if !a:forward
call xolox#lua#jumpfuncstart(a:forward)
endif
call xolox#lua#jumpblock(1)
endfunction

function! xolox#lua#tokeniscode() " {{{1
Expand Down
8 changes: 4 additions & 4 deletions ftplugin/lua.vim
Original file line number Diff line number Diff line change
Expand Up @@ -60,10 +60,10 @@ call add(s:undo_ftplugin, 'nunmap <buffer> K')
" Define custom text objects to navigate Lua source code. {{{1
noremap <buffer> <silent> [{ m':call xolox#lua#jumpblock(0)<Cr>
noremap <buffer> <silent> ]} m':call xolox#lua#jumpblock(1)<Cr>
noremap <buffer> <silent> [[ m':call xolox#lua#jumpthisfunc(0)<Cr>
noremap <buffer> <silent> ][ m':call xolox#lua#jumpthisfunc(1)<Cr>
noremap <buffer> <silent> [] m':call xolox#lua#jumpotherfunc(0)<Cr>
noremap <buffer> <silent> ]] m':call xolox#lua#jumpotherfunc(1)<Cr>
noremap <buffer> <silent> [[ m':call xolox#lua#jumpfuncstart(0)<Cr>
noremap <buffer> <silent> ][ m':call xolox#lua#jumpfuncend(1)<Cr>
noremap <buffer> <silent> [] m':call xolox#lua#jumpfuncend(0)<Cr>
noremap <buffer> <silent> ]] m':call xolox#lua#jumpfuncstart(1)<Cr>
call add(s:undo_ftplugin, 'unmap <buffer> [{')
call add(s:undo_ftplugin, 'unmap <buffer> ]}')
call add(s:undo_ftplugin, 'unmap <buffer> [[')
Expand Down