5ee72c40e2
Neovim adds a `diagnostic = { link_severity = 'warn' }` default so the value
is actually included in the payload. Vim reads the flat g:nuwiki_link_severity
global and nests it into the same `diagnostic` dict, keeping the two clients'
server-bound payloads identical.
Co-Authored-By: Claude Opus 4.7 <noreply@anthropic.com>
105 lines
3.5 KiB
VimL
105 lines
3.5 KiB
VimL
" autoload/nuwiki/lsp.vim — Vim (non-Neovim) LSP client wiring.
|
|
"
|
|
" Preference order:
|
|
" 1. vim-lsp (matoto/vim-lsp)
|
|
" 2. coc.nvim
|
|
" 3. Print an error if neither is available.
|
|
|
|
" Resolve the plugin root at script-load time. `<sfile>` inside a
|
|
" function later returns the *calling* script's path, not this file's
|
|
" — capturing it here is the reliable way.
|
|
let s:plugin_root = fnamemodify(resolve(expand('<sfile>:p')), ':h:h:h')
|
|
|
|
let s:started = 0
|
|
|
|
function! nuwiki#lsp#start() abort
|
|
if s:started
|
|
return
|
|
endif
|
|
let s:started = 1
|
|
|
|
let l:bin = s:bin_path()
|
|
if !executable(l:bin)
|
|
echohl ErrorMsg
|
|
echom 'nuwiki: language server not found at ' . l:bin
|
|
echom 'Run :NuwikiInstall to install the binary'
|
|
echohl None
|
|
return
|
|
endif
|
|
|
|
if exists('g:lsp_loaded')
|
|
" vim-lsp — its autoload functions aren't triggered by `exists('*…')`,
|
|
" but its plugin/lsp.vim sets `g:lsp_loaded = 1` once requirements
|
|
" (json_encode + timers + lambda) are met, so that's the reliable
|
|
" presence check.
|
|
call lsp#register_server({
|
|
\ 'name': 'nuwiki',
|
|
\ 'cmd': {server_info -> [l:bin]},
|
|
\ 'allowlist': ['vimwiki'],
|
|
\ 'initialization_options': s:settings(),
|
|
\ 'config': { 'settings': { 'nuwiki': s:settings() } },
|
|
\ })
|
|
" vim-lsp auto-enables servers when 'g:lsp_auto_enable' is on
|
|
" (default) — no manual `lsp#enable()` needed.
|
|
return
|
|
endif
|
|
|
|
if exists(':CocConfig') == 2 || exists('*coc#config')
|
|
" coc.nvim — coc reads its server list from coc-settings.json.
|
|
" We can't inject it dynamically without owning the file, so just point
|
|
" the user at the snippet.
|
|
echohl WarningMsg
|
|
echom 'nuwiki: coc.nvim detected. Add this to your coc-settings.json:'
|
|
echom ' "languageserver": {'
|
|
echom ' "nuwiki": {'
|
|
echom ' "command": "' . l:bin . '",'
|
|
echom ' "filetypes": ["vimwiki"]'
|
|
echom ' }'
|
|
echom ' }'
|
|
echohl None
|
|
return
|
|
endif
|
|
|
|
echohl ErrorMsg
|
|
echom 'nuwiki: no supported LSP client found.'
|
|
echom 'Install vim-lsp (https://github.com/prabirshrestha/vim-lsp)'
|
|
echom 'or coc.nvim (https://github.com/neoclide/coc.nvim).'
|
|
echohl None
|
|
endfunction
|
|
|
|
function! s:bin_path() abort
|
|
if exists('g:nuwiki_binary_path') && filereadable(g:nuwiki_binary_path)
|
|
return g:nuwiki_binary_path
|
|
endif
|
|
let l:suffix = has('win32') ? '.exe' : ''
|
|
return s:plugin_root . '/bin/nuwiki-ls' . l:suffix
|
|
endfunction
|
|
|
|
function! s:settings() abort
|
|
let l:cfg = {
|
|
\ 'wiki_root': get(g:, 'nuwiki_wiki_root', '~/vimwiki'),
|
|
\ 'file_extension': get(g:, 'nuwiki_file_extension', '.wiki'),
|
|
\ 'syntax': get(g:, 'nuwiki_syntax', 'vimwiki'),
|
|
\ 'log_level': get(g:, 'nuwiki_log_level', 'warn'),
|
|
\ 'diagnostic': {
|
|
\ 'link_severity': get(g:, 'nuwiki_link_severity', 'warn'),
|
|
\ },
|
|
\ }
|
|
" Include the per-wiki list when configured via g:nuwiki_wikis so the
|
|
" server knows each wiki's root, diary path, etc. Without this it only
|
|
" sees wiki_root and resolves links relative to that directory instead
|
|
" of the individual wiki's root.
|
|
if exists('g:nuwiki_wikis') && !empty(g:nuwiki_wikis)
|
|
let l:cfg['wikis'] = g:nuwiki_wikis
|
|
endif
|
|
return l:cfg
|
|
endfunction
|
|
|
|
" Public accessor for the `initialization_options` payload. Mirrors the
|
|
" Neovim client's `require('nuwiki.lsp').init_options()` so the config
|
|
" parity harness can inspect the exact dict each client sends to the
|
|
" server.
|
|
function! nuwiki#lsp#settings() abort
|
|
return s:settings()
|
|
endfunction
|