fix(lsp): add coc.nvim dispatch path to all Vim-side LSP calls
CI / cargo fmt --check (push) Successful in 27s
CI / cargo clippy (push) Successful in 37s
CI / cargo test (push) Successful in 33s
CI / editor keymaps (push) Successful in 1m18s

Commands were hardwired to vim-lsp (lsp#send_request, :LspDefinition,
:LspReferences, :LspRename). Users running coc.nvim got "vim-lsp not
loaded" on every command despite the plugin correctly detecting coc.

Changes:

autoload/nuwiki/commands.vim
- Add s:has_coc() (checks CocActionAsync exists)
- Add s:coc_wrap() adapter — converts coc's (err, result) callback into
  the vim-lsp notification shape {response:{result:…}} so all existing
  on_notification handlers work unchanged
- s:exec(): try vim-lsp first, fall back to CocActionAsync('runCommand',
  …) with arguments spread as positional params, then error if neither
- Add s:jump_definition() helper — :LspDefinition vs CocActionAsync
- Add nuwiki#commands#backlinks() — :LspReferences vs CocActionAsync
- badd_link(): coc path uses CocAction('getDefinition') to resolve the
  target URI without navigating
- follow_link_or_create(): replace inline :LspDefinition checks with
  s:jump_definition()
- rename_file(): :LspRename vs CocActionAsync('rename')

ftplugin/vimwiki.vim
- VimwikiFollowLink / NuwikiFollowLink: dispatch through
  nuwiki#commands#follow_link_or_create() instead of :LspDefinition
- VimwikiBacklinks / NuwikiBacklinks / VWB: dispatch through
  nuwiki#commands#backlinks() instead of :LspReferences

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
This commit is contained in:
2026-05-21 22:39:27 -03:00
parent b7d2a6c61a
commit b3e2a72023
2 changed files with 95 additions and 35 deletions
+90 -30
View File
@@ -24,21 +24,75 @@ function! s:has_vimlsp() abort
return exists('*lsp#send_request')
endfunction
function! s:exec(command, arguments, ...) abort
if !s:has_vimlsp()
function! s:has_coc() abort
return exists('*CocActionAsync')
endfunction
" Adapt a coc.nvim (err, result) callback into the vim-lsp notification
" shape that existing on_notification handlers expect.
function! s:coc_wrap(upstream, err, result) abort
if a:err isnot v:null && a:err !=# 0
echohl ErrorMsg
echom 'nuwiki: vim-lsp not loaded; cannot run ' . a:command
echom 'nuwiki: ' . (type(a:err) == type('') ? a:err : string(a:err))
echohl None
return
endif
let l:opts = {
\ 'method': 'workspace/executeCommand',
\ 'params': { 'command': a:command, 'arguments': a:arguments },
\ }
if a:0 >= 1
let l:opts['on_notification'] = a:1
call a:upstream({ 'response': { 'result': a:result } })
endfunction
function! s:exec(command, arguments, ...) abort
if s:has_vimlsp()
let l:opts = {
\ 'method': 'workspace/executeCommand',
\ 'params': { 'command': a:command, 'arguments': a:arguments },
\ }
if a:0 >= 1
let l:opts['on_notification'] = a:1
endif
call lsp#send_request(s:server, l:opts)
return
endif
if s:has_coc()
" CocActionAsync('runCommand', cmdid, arg1, arg2, …, callback)
" The arguments list is spread as individual positional args.
let l:args = ['runCommand', a:command] + a:arguments
if a:0 >= 1
call add(l:args, function('s:coc_wrap', [a:1]))
endif
call call('CocActionAsync', l:args)
return
endif
echohl ErrorMsg
echom 'nuwiki: no supported LSP client. Install vim-lsp or configure coc.nvim.'
echohl None
endfunction
" Shared jump-to-definition helper used by follow_link* and the ftplugin.
function! s:jump_definition() abort
if exists(':LspDefinition') == 2
LspDefinition
elseif s:has_coc()
call CocActionAsync('jumpDefinition')
else
echohl WarningMsg
echom 'nuwiki: no LSP client available for jump-to-definition'
echohl None
endif
endfunction
" Shared references helper used by the ftplugin.
function! nuwiki#commands#backlinks() abort
if exists(':LspReferences') == 2
LspReferences
elseif s:has_coc()
call CocActionAsync('references')
else
echohl WarningMsg
echom 'nuwiki: no LSP client available for references'
echohl None
endif
call lsp#send_request(s:server, l:opts)
endfunction
function! s:open_uri_from(response, ...) abort
@@ -105,15 +159,27 @@ endfunction
" `:VimwikiBaddLink` — add the link target file to the buffer list
" without focus. Routes through vim-lsp's textDocument/definition.
function! nuwiki#commands#badd_link() abort
if !s:has_vimlsp()
echohl ErrorMsg | echom 'nuwiki: vim-lsp not loaded' | echohl None
if s:has_vimlsp()
call lsp#send_request(s:server, {
\ 'method': 'textDocument/definition',
\ 'params': s:cursor_position(),
\ 'on_notification': function('s:badd_from_response'),
\ })
return
endif
call lsp#send_request(s:server, {
\ 'method': 'textDocument/definition',
\ 'params': s:cursor_position(),
\ 'on_notification': function('s:badd_from_response'),
\ })
if s:has_coc()
let l:locs = CocAction('getDefinition')
if type(l:locs) == type([]) && !empty(l:locs)
let l:uri = get(l:locs[0], 'uri', get(l:locs[0], 'targetUri', ''))
if !empty(l:uri)
let l:path = substitute(l:uri, '^file://', '', '')
execute 'badd ' . fnameescape(l:path)
echom 'nuwiki: added ' . l:path
endif
endif
return
endif
echohl ErrorMsg | echom 'nuwiki: no supported LSP client for badd_link' | echohl None
endfunction
function! s:badd_from_response(notification) abort
@@ -188,23 +254,15 @@ endfunction
" 3. Press inside an existing `[[…]]` follows immediately.
function! nuwiki#commands#follow_link_or_create() abort
if s:cursor_inside_wikilink()
if exists(':LspDefinition') == 2
LspDefinition
else
echohl WarningMsg
echom 'nuwiki: :LspDefinition unavailable; install vim-lsp'
echohl None
endif
call s:jump_definition()
return
endif
if s:wrap_cword_as_wikilink()
" Wrapped — wait for the user's second <CR> to follow.
return
endif
" Nothing to wrap — fall through.
if exists(':LspDefinition') == 2
LspDefinition
endif
" Nothing to wrap — fall through to plain definition.
call s:jump_definition()
endfunction
" Pure-VimL bullet-continuation for `o` / `O` — mirrors the Lua side
@@ -535,11 +593,13 @@ function! nuwiki#commands#delete_file() abort
endfunction
function! nuwiki#commands#rename_file() abort
if exists('*lsp#rename')
if exists(':LspRename') == 2
LspRename
elseif s:has_coc()
call CocActionAsync('rename')
else
echohl WarningMsg
echom 'nuwiki: :LspRename unavailable; install vim-lsp'
echom 'nuwiki: no LSP client available for rename'
echohl None
endif
endfunction
+5 -5
View File
@@ -39,9 +39,9 @@ if !has('nvim')
command! -buffer VimwikiDiaryNextDay call nuwiki#commands#diary_next()
command! -buffer VimwikiDiaryPrevDay call nuwiki#commands#diary_prev()
command! -buffer VimwikiDiaryGenerateLinks call nuwiki#commands#diary_generate_index()
command! -buffer VimwikiFollowLink LspDefinition
command! -buffer VimwikiBacklinks LspReferences
command! -buffer VWB LspReferences
command! -buffer VimwikiFollowLink call nuwiki#commands#follow_link_or_create()
command! -buffer VimwikiBacklinks call nuwiki#commands#backlinks()
command! -buffer VWB call nuwiki#commands#backlinks()
command! -buffer VimwikiNextLink call nuwiki#commands#link_next()
command! -buffer VimwikiPrevLink call nuwiki#commands#link_prev()
command! -buffer VimwikiBaddLink call nuwiki#commands#badd_link()
@@ -88,8 +88,8 @@ if !has('nvim')
command! -buffer NuwikiDiaryTomorrow call nuwiki#commands#diary_tomorrow()
command! -buffer NuwikiDiaryNext call nuwiki#commands#diary_next()
command! -buffer NuwikiDiaryPrev call nuwiki#commands#diary_prev()
command! -buffer NuwikiFollowLink LspDefinition
command! -buffer NuwikiBacklinks LspReferences
command! -buffer NuwikiFollowLink call nuwiki#commands#follow_link_or_create()
command! -buffer NuwikiBacklinks call nuwiki#commands#backlinks()
command! -buffer NuwikiDeleteFile call nuwiki#commands#delete_file()
command! -buffer NuwikiRenameFile call nuwiki#commands#rename_file()
command! -buffer NuwikiNextTask call nuwiki#commands#next_task()