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') return exists('*lsp#send_request')
endfunction endfunction
function! s:exec(command, arguments, ...) abort function! s:has_coc() abort
if !s:has_vimlsp() 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 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 echohl None
return return
endif endif
let l:opts = { call a:upstream({ 'response': { 'result': a:result } })
\ 'method': 'workspace/executeCommand', endfunction
\ 'params': { 'command': a:command, 'arguments': a:arguments },
\ } function! s:exec(command, arguments, ...) abort
if a:0 >= 1 if s:has_vimlsp()
let l:opts['on_notification'] = a:1 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 endif
call lsp#send_request(s:server, l:opts)
endfunction endfunction
function! s:open_uri_from(response, ...) abort function! s:open_uri_from(response, ...) abort
@@ -105,15 +159,27 @@ endfunction
" `:VimwikiBaddLink` — add the link target file to the buffer list " `:VimwikiBaddLink` — add the link target file to the buffer list
" without focus. Routes through vim-lsp's textDocument/definition. " without focus. Routes through vim-lsp's textDocument/definition.
function! nuwiki#commands#badd_link() abort function! nuwiki#commands#badd_link() abort
if !s:has_vimlsp() if s:has_vimlsp()
echohl ErrorMsg | echom 'nuwiki: vim-lsp not loaded' | echohl None call lsp#send_request(s:server, {
\ 'method': 'textDocument/definition',
\ 'params': s:cursor_position(),
\ 'on_notification': function('s:badd_from_response'),
\ })
return return
endif endif
call lsp#send_request(s:server, { if s:has_coc()
\ 'method': 'textDocument/definition', let l:locs = CocAction('getDefinition')
\ 'params': s:cursor_position(), if type(l:locs) == type([]) && !empty(l:locs)
\ 'on_notification': function('s:badd_from_response'), 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 endfunction
function! s:badd_from_response(notification) abort function! s:badd_from_response(notification) abort
@@ -188,23 +254,15 @@ endfunction
" 3. Press inside an existing `[[…]]` follows immediately. " 3. Press inside an existing `[[…]]` follows immediately.
function! nuwiki#commands#follow_link_or_create() abort function! nuwiki#commands#follow_link_or_create() abort
if s:cursor_inside_wikilink() if s:cursor_inside_wikilink()
if exists(':LspDefinition') == 2 call s:jump_definition()
LspDefinition
else
echohl WarningMsg
echom 'nuwiki: :LspDefinition unavailable; install vim-lsp'
echohl None
endif
return return
endif endif
if s:wrap_cword_as_wikilink() if s:wrap_cword_as_wikilink()
" Wrapped — wait for the user's second <CR> to follow. " Wrapped — wait for the user's second <CR> to follow.
return return
endif endif
" Nothing to wrap — fall through. " Nothing to wrap — fall through to plain definition.
if exists(':LspDefinition') == 2 call s:jump_definition()
LspDefinition
endif
endfunction endfunction
" Pure-VimL bullet-continuation for `o` / `O` — mirrors the Lua side " Pure-VimL bullet-continuation for `o` / `O` — mirrors the Lua side
@@ -535,11 +593,13 @@ function! nuwiki#commands#delete_file() abort
endfunction endfunction
function! nuwiki#commands#rename_file() abort function! nuwiki#commands#rename_file() abort
if exists('*lsp#rename') if exists(':LspRename') == 2
LspRename LspRename
elseif s:has_coc()
call CocActionAsync('rename')
else else
echohl WarningMsg echohl WarningMsg
echom 'nuwiki: :LspRename unavailable; install vim-lsp' echom 'nuwiki: no LSP client available for rename'
echohl None echohl None
endif endif
endfunction endfunction
+5 -5
View File
@@ -39,9 +39,9 @@ if !has('nvim')
command! -buffer VimwikiDiaryNextDay call nuwiki#commands#diary_next() command! -buffer VimwikiDiaryNextDay call nuwiki#commands#diary_next()
command! -buffer VimwikiDiaryPrevDay call nuwiki#commands#diary_prev() command! -buffer VimwikiDiaryPrevDay call nuwiki#commands#diary_prev()
command! -buffer VimwikiDiaryGenerateLinks call nuwiki#commands#diary_generate_index() command! -buffer VimwikiDiaryGenerateLinks call nuwiki#commands#diary_generate_index()
command! -buffer VimwikiFollowLink LspDefinition command! -buffer VimwikiFollowLink call nuwiki#commands#follow_link_or_create()
command! -buffer VimwikiBacklinks LspReferences command! -buffer VimwikiBacklinks call nuwiki#commands#backlinks()
command! -buffer VWB LspReferences command! -buffer VWB call nuwiki#commands#backlinks()
command! -buffer VimwikiNextLink call nuwiki#commands#link_next() command! -buffer VimwikiNextLink call nuwiki#commands#link_next()
command! -buffer VimwikiPrevLink call nuwiki#commands#link_prev() command! -buffer VimwikiPrevLink call nuwiki#commands#link_prev()
command! -buffer VimwikiBaddLink call nuwiki#commands#badd_link() 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 NuwikiDiaryTomorrow call nuwiki#commands#diary_tomorrow()
command! -buffer NuwikiDiaryNext call nuwiki#commands#diary_next() command! -buffer NuwikiDiaryNext call nuwiki#commands#diary_next()
command! -buffer NuwikiDiaryPrev call nuwiki#commands#diary_prev() command! -buffer NuwikiDiaryPrev call nuwiki#commands#diary_prev()
command! -buffer NuwikiFollowLink LspDefinition command! -buffer NuwikiFollowLink call nuwiki#commands#follow_link_or_create()
command! -buffer NuwikiBacklinks LspReferences command! -buffer NuwikiBacklinks call nuwiki#commands#backlinks()
command! -buffer NuwikiDeleteFile call nuwiki#commands#delete_file() command! -buffer NuwikiDeleteFile call nuwiki#commands#delete_file()
command! -buffer NuwikiRenameFile call nuwiki#commands#rename_file() command! -buffer NuwikiRenameFile call nuwiki#commands#rename_file()
command! -buffer NuwikiNextTask call nuwiki#commands#next_task() command! -buffer NuwikiNextTask call nuwiki#commands#next_task()