feat(coc): auto-register the language server with coc.nvim
CI / cargo fmt --check (push) Successful in 47s
CI / cargo clippy (push) Successful in 46s
CI / cargo test (push) Successful in 56s
CI / editor keymaps (push) Successful in 1m30s

coc users had to hand-maintain a coc-settings.json `languageserver.nuwiki`
entry, which didn't track g:nuwiki_wikis / g:vimwiki_list / the global
shorthand and meant editing JSON per wiki. nuwiki now registers itself
programmatically: on the first .wiki buffer it calls
coc#config('languageserver.nuwiki', {…}) with the s:settings() payload
(same config the vim-lsp path sends). coc reacts to the languageserver
config change (onDidChangeConfiguration → registerClientsByConfig) and
starts the server for the open buffer.

Details:
- Reliable coc detection via :CocConfig (exists('*coc#config') can't be
  trusted — it doesn't trigger autoload in Vim 9.2). The coc#config call
  is wrapped in try/catch and autoloads coc.vim itself; falls back to the
  printed snippet only if coc genuinely isn't there.
- Deferred to User CocNvimInit when coc's node service isn't up yet.
- Opt out with g:nuwiki_no_coc_register (then we just print the snippet).

Dogfooded in start-vim-coc.sh: dropped the manual coc-settings.json
languageserver block; the harness now relies on auto-registration from
g:nuwiki_* (real-user flow). New harness test-coc-register-vim (7 checks,
stubbed coc#config) asserts the injected payload incl. the folded global
shorthand; wired into CI. README coc section rewritten (zero-config).
Rust 573 passed, clippy clean, all harnesses green.

Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
This commit is contained in:
2026-06-05 00:07:50 +00:00
parent a4643bdacb
commit ac14cdd838
6 changed files with 203 additions and 61 deletions
+12 -38
View File
@@ -72,46 +72,14 @@ ensure_coc() {
write_coc_settings() {
mkdir -p "$DEV_DIR"
# Mirror what the vim-lsp client sends (autoload/nuwiki/lsp.vim s:settings()):
# both `initializationOptions` (sent on initialize) and `settings.nuwiki`
# (served via workspace/configuration). Without these the server falls back
# to its own default wiki_root (~/vimwiki) instead of $WIKI_DIR, so it can't
# resolve links (e.g. Notes shows as broken) or create-on-follow correctly.
# toc_header_level / html_header_numbering are set to non-defaults so the
# dev wiki exercises them: :NuwikiTOC writes `== Contents ==` (level 2) and
# HTML export numbers headings. coc reads this file verbatim, so these are
# the values the server actually receives.
# No `languageserver.nuwiki` block here on purpose: this dogfoods nuwiki's
# *programmatic* coc registration (autoload/nuwiki/lsp.vim → coc#config). The
# plugin registers the server itself from g:nuwiki_* (set in the vimrc),
# exactly the "no hand-written coc-settings.json" flow real users get. Only
# the jump-command preference (the <CR>-opens-a-tab reproducer) lives here.
cat > "$COC_SETTINGS" <<EOF
{
"coc.preferences.jumpCommand": "$COC_JUMP",
"languageserver": {
"nuwiki": {
"command": "$REPO_ROOT/bin/nuwiki-ls",
"filetypes": ["vimwiki"],
"initializationOptions": {
"wiki_root": "$WIKI_DIR",
"file_extension": ".wiki",
"syntax": "vimwiki",
"log_level": "info",
"toc_header_level": 2,
"html_header_numbering": 2,
"html_header_numbering_sym": " -",
"diagnostic": { "link_severity": "warn" }
},
"settings": {
"nuwiki": {
"wiki_root": "$WIKI_DIR",
"file_extension": ".wiki",
"syntax": "vimwiki",
"log_level": "info",
"toc_header_level": 2,
"html_header_numbering": 2,
"html_header_numbering_sym": " -",
"diagnostic": { "link_severity": "warn" }
}
}
}
}
"coc.preferences.jumpCommand": "$COC_JUMP"
}
EOF
}
@@ -146,6 +114,12 @@ let g:nuwiki_file_extension = '.wiki'
let g:nuwiki_syntax = 'vimwiki'
let g:nuwiki_diary_rel_path = 'diary'
let g:nuwiki_log_level = 'info'
" nuwiki auto-registers itself with coc from these globals (no coc-settings.json
" languageserver block). Display settings exercise the global shorthand:
" :NuwikiTOC writes `== Contents ==` (level 2) and HTML export numbers headings.
let g:nuwiki_toc_header_level = 2
let g:nuwiki_html_header_numbering = 2
let g:nuwiki_html_header_numbering_sym = ' -'
filetype plugin indent on
syntax enable
+71
View File
@@ -0,0 +1,71 @@
#!/usr/bin/env bash
#
# development/tests/test-coc-register-vim.sh — programmatic coc.nvim
# registration for the Vim client.
#
# nuwiki#lsp#start() registers the language server with coc via coc#config so
# users don't hand-maintain coc-settings.json. This harness stubs coc#config
# (a real autoload/coc.vim on the runtimepath, since Vim's exists('*coc#config')
# goes through the autoload mechanism) to record what nuwiki injects, then runs
# test-coc-register-vim.vim and asserts the payload.
#
# Exit code: 0 when every check passes, 1 on any FAIL or missing output.
# Skips (exit 0) when vim is not installed.
set -euo pipefail
REPO_ROOT="$(cd "$(dirname "${BASH_SOURCE[0]}")/../.." && pwd)"
log() { printf '\033[1;34m[coc-register]\033[0m %s\n' "$*"; }
if ! command -v vim >/dev/null 2>&1; then
log 'vim not installed — skipping'
exit 0
fi
TMP="$(mktemp -d -t nuwiki-coc-test-XXXXXX)"
trap 'rm -rf "$TMP"' EXIT
# Stub coc#config as a real autoload function so exists('*coc#config') resolves
# it. It records the last call into g:_recorded for the test to assert.
mkdir -p "$TMP/stub/autoload"
cat > "$TMP/stub/autoload/coc.vim" <<'EOF'
function! coc#config(section, value) abort
let g:_recorded = {'section': a:section, 'value': deepcopy(a:value)}
endfunction
EOF
OUT="$TMP/coc.out"
VIMRC="$TMP/vimrc"
cat > "$VIMRC" <<EOF
set nocompatible
" Stub dir first so its autoload/coc.vim is the one that resolves.
let &runtimepath = '$TMP/stub' . ',' . '$REPO_ROOT' . ',' . &runtimepath
" coc.nvim defines :CocConfig at startup; nuwiki uses that as the reliable
" 'coc is present' signal. Stub it so the coc branch is taken.
command! -nargs=0 CocConfig echo ''
EOF
log 'running Vim coc-registration harness…'
NUWIKI_COC_OUT="$OUT" \
timeout 30 vim -e -s -u "$VIMRC" \
-c "source $REPO_ROOT/development/tests/test-coc-register-vim.vim" \
</dev/null >"$TMP/vim.log" 2>&1 || true
if [[ ! -f "$OUT" ]]; then
echo 'coc-registration harness produced no output' >&2
echo '--- vim log ---' >&2
cat "$TMP/vim.log" >&2 || true
exit 1
fi
cat "$OUT"
PASSED="$(grep -c '^PASS ' "$OUT" || true)"
FAILED="$(grep -c '^FAIL ' "$OUT" || true)"
echo "SUMMARY: ${PASSED} passed, ${FAILED} failed"
if [[ "$FAILED" -ne 0 ]]; then
exit 1
fi
log 'coc registration OK ✓'
exit 0
@@ -0,0 +1,42 @@
" development/tests/test-coc-register-vim.vim — programmatic coc.nvim
" registration. Relies on a stub autoload/coc.vim (created by the wrapper) that
" records coc#config calls into g:_recorded. Drives nuwiki#lsp#start()'s coc
" branch and asserts the exact languageserver config nuwiki injects (command,
" filetypes, and the s:settings() payload with the global shorthand folded in).
" Writes PASS/FAIL to $NUWIKI_COC_OUT.
let s:out = []
function! s:check(desc, cond) abort
call add(s:out, (a:cond ? 'PASS ' : 'FAIL ') . a:desc)
endfunction
" Pretend coc's node service is already up so registration runs synchronously.
let g:coc_service_initialized = 1
let g:_recorded = {}
" Make sure the vim-lsp branch is NOT taken.
if exists('g:lsp_loaded') | unlet g:lsp_loaded | endif
" A readable, executable 'binary' so nuwiki#lsp#start() gets past its check.
let g:nuwiki_binary_path = executable('true') ? exepath('true') : '/usr/bin/true'
" Config: single-wiki shorthand + a global display setting that must fold into
" the wiki the server sees (the global shorthand).
let g:nuwiki_wiki_root = '/tmp/realwiki'
let g:nuwiki_toc_header_level = 2
call nuwiki#lsp#start()
let s:r = get(g:, '_recorded', {})
call s:check('coc#config was called', !empty(s:r))
call s:check('section is languageserver.nuwiki', get(s:r, 'section', '') ==# 'languageserver.nuwiki')
let s:v = get(s:r, 'value', {})
call s:check('command is the binary', get(s:v, 'command', '') ==# g:nuwiki_binary_path)
call s:check('filetypes = [vimwiki]', get(s:v, 'filetypes', []) == ['vimwiki'])
let s:io = get(s:v, 'initializationOptions', {})
call s:check('initOpts has wiki_root', get(s:io, 'wiki_root', '') ==# '/tmp/realwiki')
call s:check('global toc_header_level folded into payload', get(s:io, 'toc_header_level', 0) == 2)
call s:check('settings.nuwiki mirrors initOpts', get(get(s:v, 'settings', {}), 'nuwiki', {}) ==# s:io)
call writefile(s:out, $NUWIKI_COC_OUT)
qa!