How do I make vim's method jump commands ']]' and '[[' work with modern method syntax?

Viewed 181

If I use old-school curly brace function block syntax like so:

function()
{
  console.log('this works :)');
}

I can jump between methods quickly with vim's ]] and [[ commands, but if I (or anybody else on my team) use the more modern syntax where the opening curly brace is in-line with the function signature like so:

function() {
  console.log('this doesn't work :(');
}

Vim won't detect the method and will skip it because the command only looks for opening braces on the first column. How can I configure Vim to not ignore this syntax?

I'm not against using either syntax but I don't want to skip over methods when reading other peoples' code.

2 Answers

Thanks for a great answer @MisterBanal

I was able to improve on the above, so that the search context is preserved and ]] [[ will never wrap around.

function s:SetSrchReg(s, w)
    let @/ = a:s
    let &wrapscan = a:w
endfunction
function s:ProtectSrchReg(map)
    " echomsg "ProtectSrchReg "..a:map
    let srchSave = @/
    let wsSave = &wrapscan
    set nowrapscan
    exe timer_start(50, {-> s:SetSrchReg(srchSave, wsSave)})
    return a:map
endfunction

nnoremap <expr> <silent> [[ <SID>ProtectSrchReg('?{<CR>w99[{')
nnoremap <expr> <silent> ][ <SID>ProtectSrchReg('/}<CR>b99]}')
nnoremap <expr> <silent> ]] <SID>ProtectSrchReg('/{<CR>?{<CR>j099[{%/{<CR>')
nnoremap <expr> <silent> [] <SID>ProtectSrchReg('k$99]}%?}<CR>')
Related