Coc.nvim: How can I use coc and other go to definition engine as fallback?

Created on 5 Jan 2020  路  2Comments  路  Source: neoclide/coc.nvim

As per the example, I can use:

nmap <silent> gd <Plug>(coc-definition)

for go to definition. But I also use GNU Global for some smaller projects, and it's like this:

nmap <silent>gd :cs find g <C-R>=expand("<cword>")<CR><CR>

And there is the dumb VIM built-in gd.

So how can I combine them together, in the sequence of coc -> Global -> built-in? In other words, if the coc-definition finds something, jump to it, otherwise try another command and jump to the definition, and finally fallback to the built-in gq.

Most helpful comment

function! s:GoToDefinition()
  if CocAction('jumpDefinition')
    return v:true
  endif

  let ret = execute("silent! normal \<C-]>")
  if ret =~ "Error" || ret =~ "閿欒"
    call searchdecl(expand('<cword>'))
  endif
endfunction

nmap <silent> gd :call <SID>GoToDefinition()<CR>

I'm using this function, gd will go by coc -> tags -> searchdecl. You can try something like this with global.

All 2 comments

function! s:GoToDefinition()
  if CocAction('jumpDefinition')
    return v:true
  endif

  let ret = execute("silent! normal \<C-]>")
  if ret =~ "Error" || ret =~ "閿欒"
    call searchdecl(expand('<cword>'))
  endif
endfunction

nmap <silent> gd :call <SID>GoToDefinition()<CR>

I'm using this function, gd will go by coc -> tags -> searchdecl. You can try something like this with global.

Thank you.

Was this page helpful?
0 / 5 - 0 ratings