refactor(client): self-review cleanup — dead code, dedup, parity, stale comments
CI / editor tests (push) Successful in 45s

A code-review pass over the VimL + Lua client layers (no behaviour change
beyond the noted parity fixes; full editor harness 10/10):

Lua (lua/nuwiki/):
- commands.lua: drop the dead <0.10 make_position_params pcall/no-arg fallback
  (min is 0.11; no-arg form is itself deprecated) and its stale comment.
- commands.lua: simplify the exec() client dispatch — the metatable/rawget
  dance is gone; always use the non-deprecated colon form client:request() on
  0.11+. (This is the block an external review misread as "critical".)
- commands.lua: extract parse_list_marker() + has_auto_indent() shared by
  smart_return/smart_shift_return; extract word_boundaries() shared by
  wrap_cword_as_wikilink/colorize (and unify the equivalent char classes).
- commands.lua: :Nuwiki search now surfaces real lvimgrep errors instead of
  reporting every failure as "no match" (E480 stays a WARN) — parity with the
  VimL twin's `catch /E480/`.
- keymaps.lua: collapse open_below/above_with_bullet into open_with_bullet(cmd)
  (checkbox prefix preserved for `o` only, as before).
- lsp.lua: normalize the wiki root before the buffer-path prefix compare
  (Windows separators); buf path was already normalized.
- ftplugin.lua: drop the always-true has('nvim-0.11') guard and hoist the
  identical foldmethod/foldtext out of both branches.
- install.lua: use a local `uv` instead of mutating the global vim.uv.

VimL (autoload/nuwiki/):
- commands.vim: refresh a stale comment pointing at the old in-repo
  nuwiki-lsp/src/commands.rs path → the nuwiki-rs server generally.

Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
This commit is contained in:
2026-06-28 13:59:52 +00:00
parent 82444f5013
commit 4bee216c05
6 changed files with 89 additions and 106 deletions
+1 -1
View File
@@ -1153,7 +1153,7 @@ endfunction
" Mirrors `lua/nuwiki/commands.lua`'s `align_table_at` so plain Vim " Mirrors `lua/nuwiki/commands.lua`'s `align_table_at` so plain Vim
" buffers get the same "tighten columns as cells grow" behaviour the " buffers get the same "tighten columns as cells grow" behaviour the
" Neovim path enjoys, without needing a vim-lsp roundtrip. Algorithm " Neovim path enjoys, without needing a vim-lsp roundtrip. Algorithm
" matches `nuwiki-lsp/src/commands.rs::ops::render_aligned_table` (nuwiki-rs repo). " matches the table-alignment logic in the nuwiki-rs server.
function! s:is_table_row(line) abort function! s:is_table_row(line) abort
return nuwiki#util#is_table_row(a:line) return nuwiki#util#is_table_row(a:line)
+69 -77
View File
@@ -24,13 +24,10 @@ local function find_client()
end end
local function position_params(client) local function position_params(client)
-- Neovim 0.12+ requires `position_encoding` explicitly; older -- Neovim 0.11+ requires the position encoding explicitly (the no-arg form is
-- versions accept the no-arg form. -- deprecated); pass the current window + the client's negotiated encoding.
local encoding = client and client.offset_encoding or 'utf-16' local encoding = client and client.offset_encoding or 'utf-16'
local ok, params = pcall(vim.lsp.util.make_position_params, 0, encoding) return vim.lsp.util.make_position_params(0, encoding)
if ok then return params end
-- Fallback for Neovim < 0.10 where the second arg wasn't a thing.
return vim.lsp.util.make_position_params()
end end
local function exec(command, arguments, on_result) local function exec(command, arguments, on_result)
@@ -51,21 +48,13 @@ local function exec(command, arguments, on_result)
end end
local bufnr = vim.api.nvim_get_current_buf() local bufnr = vim.api.nvim_get_current_buf()
local payload = { command = command, arguments = arguments or {} } local payload = { command = command, arguments = arguments or {} }
-- Neovim 0.12 deprecates `client.request(...)` in favour of the -- The method form `client:request(...)` is the correct, non-deprecated call
-- method form `client:request(...)`. Prefer the method, fall back -- on every supported Neovim (0.11+); the old `client.request(...)` function
-- on older Neovim where it isn't defined yet. -- form was deprecated in its favour.
local req = client.request if type(client.request) ~= 'function' then
if type(req) ~= 'function' then
return return
end end
if rawget(getmetatable(client) or {}, '__index') and client.request ~= nil then client:request('workspace/executeCommand', payload, handler, bufnr)
-- Method-style works for both colon and dot calls on Neovim's
-- Client metatable, but pass `self` explicitly for the older
-- function-style signature compatibility.
client:request('workspace/executeCommand', payload, handler, bufnr)
else
client.request('workspace/executeCommand', payload, handler, bufnr)
end
end end
local function open_uri(uri, tab) local function open_uri(uri, tab)
@@ -128,38 +117,42 @@ local function cursor_inside_wikilink()
return open_at ~= nil return open_at ~= nil
end end
local function wrap_cword_as_wikilink() -- Find the word around 0-based cursor `col` in `line`. Returns `left` (0-based
local cword = vim.fn.expand('<cword>') -- index of the char before the word start) and `right` (1-based index one past
if cword == '' then return false end -- the word's last char), so `word = line:sub(left + 1, right - 1)`. A "word" is
-- Replace just this `<cword>` occurrence using `*` (the start of -- a run of `[%w_-]`.
-- the last cursor match) — `ciw[[<C-r>"]]` style, but we do it local function word_boundaries(line, col)
-- via the API so we don't depend on register state.
local row, col = unpack(vim.api.nvim_win_get_cursor(0))
local line = vim.api.nvim_get_current_line()
-- Locate the word boundaries around cursor (0-based col).
local left = col local left = col
while left > 0 and line:sub(left, left):match('[%w_-]') do while left > 0 and line:sub(left, left):match('[%w_-]') do
left = left - 1 left = left - 1
end end
-- `left` now sits one before the word start (or at -1 / col=0 if at start). -- `left` sits one before the word start; nudge forward if it's not on a
local start_col = left -- word char (cursor was already at the very start).
if line:sub(left + 1, left + 1):match('[%w_-]') then if not line:sub(left + 1, left + 1):match('[%w_-]') then
start_col = left left = left + 1
else
start_col = left + 1
end end
local right = start_col + 1 local right = left + 1
while right <= #line and line:sub(right, right):match('[%w_-]') do while right <= #line and line:sub(right, right):match('[%w_-]') do
right = right + 1 right = right + 1
end end
local stop_col = right - 1 return left, right
local word = line:sub(start_col + 1, stop_col) end
local function wrap_cword_as_wikilink()
local cword = vim.fn.expand('<cword>')
if cword == '' then return false end
-- Replace just this `<cword>` occurrence via the API so we don't depend on
-- register state.
local row, col = unpack(vim.api.nvim_win_get_cursor(0))
local line = vim.api.nvim_get_current_line()
local left, right = word_boundaries(line, col)
local word = line:sub(left + 1, right - 1)
if word == '' then return false end if word == '' then return false end
local new_line = line:sub(1, start_col) .. '[[' .. word .. ']]' .. line:sub(stop_col + 1) local new_line = line:sub(1, left) .. '[[' .. word .. ']]' .. line:sub(right)
vim.api.nvim_set_current_line(new_line) vim.api.nvim_set_current_line(new_line)
-- Put cursor inside the new `[[…]]` so the LSP definition request -- Put cursor inside the new `[[…]]` so the LSP definition request
-- lands on the wikilink. -- lands on the wikilink.
vim.api.nvim_win_set_cursor(0, { row, start_col + 2 }) vim.api.nvim_win_set_cursor(0, { row, left + 2 })
return true return true
end end
@@ -349,9 +342,16 @@ function M.search(args)
end end
if not ext:match('^%.') then ext = '.' .. ext end if not ext:match('^%.') then ext = '.' .. ext end
local glob = root == '' and '**' or (vim.fn.fnameescape(root) .. '**/*' .. ext) local glob = root == '' and '**' or (vim.fn.fnameescape(root) .. '**/*' .. ext)
local ok = pcall(vim.cmd, 'lvimgrep /' .. vim.fn.escape(pat, '/') .. '/j ' .. glob) local ok, err = pcall(vim.cmd, 'lvimgrep /' .. vim.fn.escape(pat, '/') .. '/j ' .. glob)
if not ok then if not ok then
vim.notify('nuwiki: no match for ' .. pat, vim.log.levels.WARN) -- E480 = no match: a normal "nothing found", not an error. Anything else
-- (bad pattern, unreadable dir) is surfaced verbatim. Mirrors the VimL
-- twin's `catch /E480/`.
if type(err) == 'string' and err:match('E480') then
vim.notify('nuwiki: no match for ' .. pat, vim.log.levels.WARN)
else
vim.notify('nuwiki: ' .. tostring(err), vim.log.levels.ERROR)
end
return return
end end
vim.cmd('lopen') vim.cmd('lopen')
@@ -979,6 +979,27 @@ function M._table_insert_row_below(row)
vim.api.nvim_win_set_cursor(0, { row + 1, #indent + 1 }) vim.api.nvim_win_set_cursor(0, { row + 1, #indent + 1 })
end end
-- Parse a list-item line into indent / marker / trailing text. Handles
-- `-`/`*`/`#` bullets and numbered `N.`/`N)` markers (the marker includes the
-- number). `marker` is nil when `line` isn't a list item.
local function parse_list_marker(line)
local indent, marker, after = line:match('^(%s*)([%-%*%#])%s(.*)$')
if not marker then
local n
indent, n, marker, after = line:match('^(%s*)(%d+)([%.%)])%s(.*)$')
if marker then marker = n .. marker end
end
return indent, marker, after
end
-- True when any auto-indent mechanism is active. nvim then re-inserts the
-- previous line's indent after a returned `<CR>`, so we must not also add our
-- own copy (which would push the continuation one level too deep).
local function has_auto_indent()
return vim.bo.autoindent or vim.bo.smartindent or vim.bo.cindent
or (vim.bo.indentexpr ~= nil and vim.bo.indentexpr ~= '')
end
--- Smart `<CR>` for insert mode — vimwiki parity for `:VimwikiReturn`. --- Smart `<CR>` for insert mode — vimwiki parity for `:VimwikiReturn`.
--- Used as an `<expr>` keymap so the function is side-effect-free on --- Used as an `<expr>` keymap so the function is side-effect-free on
--- the buffer (textlock is active). The table-row branch hands off to --- the buffer (textlock is active). The table-row branch hands off to
@@ -1002,12 +1023,7 @@ function M.smart_return()
end end
-- List item with marker? -- List item with marker?
local indent, marker, after = line:match('^(%s*)([%-%*%#])%s(.*)$') local indent, marker, after = parse_list_marker(line)
if not marker then
local n
indent, n, marker, after = line:match('^(%s*)(%d+)([%.%)])%s(.*)$')
if marker then marker = n .. marker end
end
if marker then if marker then
local cb = after:match('^%[[%sxXoO%.%-]%]%s*(.*)$') local cb = after:match('^%[[%sxXoO%.%-]%]%s*(.*)$')
local body = cb or after local body = cb or after
@@ -1017,14 +1033,8 @@ function M.smart_return()
-- "break out of the list" behaviour on an empty bullet). -- "break out of the list" behaviour on an empty bullet).
return esc .. '0DA' .. cr return esc .. '0DA' .. cr
end end
-- nvim defaults `autoindent=on`, which inserts the previous line's -- Skip our own indent copy when auto-indent is active (see has_auto_indent).
-- indent right after the `<CR>` we return. If we then add our own local effective_indent = has_auto_indent() and '' or indent
-- `indent`, the new bullet is pushed one level deeper than the one
-- the user is continuing. Skip our copy whenever any auto-indent
-- mechanism is active and let Vim/Nvim insert the indent for us.
local auto = vim.bo.autoindent or vim.bo.smartindent or vim.bo.cindent
or (vim.bo.indentexpr ~= nil and vim.bo.indentexpr ~= '')
local effective_indent = auto and '' or indent
return cr .. effective_indent .. marker .. ' ' .. (cb and '[ ] ' or '') return cr .. effective_indent .. marker .. ' ' .. (cb and '[ ] ' or '')
end end
return cr return cr
@@ -1037,23 +1047,15 @@ function M.smart_shift_return()
local line = vim.api.nvim_get_current_line() local line = vim.api.nvim_get_current_line()
local cr = vim.api.nvim_replace_termcodes('<CR>', true, false, true) local cr = vim.api.nvim_replace_termcodes('<CR>', true, false, true)
if vim.fn.pumvisible() == 1 then return cr end if vim.fn.pumvisible() == 1 then return cr end
local indent, marker, after = line:match('^(%s*)([%-%*%#])%s(.*)$') local indent, marker, after = parse_list_marker(line)
if not marker then
local n
indent, n, marker, after = line:match('^(%s*)(%d+)([%.%)])%s(.*)$')
if marker then marker = n .. marker end
end
if not marker then if not marker then
return cr return cr
end end
-- Align under the text: marker width + 1 space (+ 4 for a `[ ] ` checkbox). -- Align under the text: marker width + 1 space (+ 4 for a `[ ] ` checkbox).
local has_cb = after:match('^%[[%sxXoO%.%-]%]') ~= nil local has_cb = after:match('^%[[%sxXoO%.%-]%]') ~= nil
local pad = #marker + 1 + (has_cb and 4 or 0) local pad = #marker + 1 + (has_cb and 4 or 0)
-- Mirror smart_return's auto-indent handling: when an auto-indent mechanism -- Mirror smart_return's auto-indent handling (see has_auto_indent).
-- is active, Vim already re-inserts the line's indent after `<CR>`. return cr .. (has_auto_indent() and '' or indent) .. string.rep(' ', pad)
local auto = vim.bo.autoindent or vim.bo.smartindent or vim.bo.cindent
or (vim.bo.indentexpr ~= nil and vim.bo.indentexpr ~= '')
return cr .. (auto and '' or indent) .. string.rep(' ', pad)
end end
-- :VimwikiReturn / :NuwikiReturn — the command form of the smart `<CR>` -- :VimwikiReturn / :NuwikiReturn — the command form of the smart `<CR>`
@@ -1242,17 +1244,7 @@ function M.colorize(color, visual)
local cword = vim.fn.expand('<cword>') local cword = vim.fn.expand('<cword>')
if cword == '' then return end if cword == '' then return end
local col = vim.api.nvim_win_get_cursor(0)[2] local col = vim.api.nvim_win_get_cursor(0)[2]
local left = col local left, right = word_boundaries(line, col)
while left > 0 and line:sub(left, left):match('[%w_%-]') do
left = left - 1
end
if not line:sub(left + 1, left + 1):match('[%w_%-]') then
left = left + 1
end
local right = left + 1
while right <= #line and line:sub(right, right):match('[%w_%-]') do
right = right + 1
end
local before = line:sub(1, left) local before = line:sub(1, left)
local word = line:sub(left + 1, right - 1) local word = line:sub(left + 1, right - 1)
local after = line:sub(right) local after = line:sub(right)
+4 -7
View File
@@ -18,16 +18,13 @@ local function setup_folding(bufnr, folding_mode)
-- yet attached) degrades to the regex variant instead of spamming -- yet attached) degrades to the regex variant instead of spamming
-- the user with E5108 errors per line. -- the user with E5108 errors per line.
local apply = function() local apply = function()
if folding_mode == 'expr' vim.opt_local.foldmethod = 'expr'
or not (vim.fn.has('nvim-0.11') == 1 and vim.lsp.foldexpr) vim.opt_local.foldtext = 'v:lua.require("nuwiki.folding").foldtext()'
then -- Min Neovim is 0.11, so vim.lsp.foldexpr is the only capability gate.
vim.opt_local.foldmethod = 'expr' if folding_mode == 'expr' or not vim.lsp.foldexpr then
vim.opt_local.foldexpr = 'v:lua.require("nuwiki.folding").expr()' vim.opt_local.foldexpr = 'v:lua.require("nuwiki.folding").expr()'
vim.opt_local.foldtext = 'v:lua.require("nuwiki.folding").foldtext()'
else else
vim.opt_local.foldmethod = 'expr'
vim.opt_local.foldexpr = 'v:lua.require("nuwiki.folding").lsp_expr()' vim.opt_local.foldexpr = 'v:lua.require("nuwiki.folding").lsp_expr()'
vim.opt_local.foldtext = 'v:lua.require("nuwiki.folding").foldtext()'
end end
end end
+2 -2
View File
@@ -53,8 +53,8 @@ local function build_from_source(dest)
local root = plugin_root() local root = plugin_root()
vim.notify('nuwiki: building nuwiki-ls from source (cargo build --release) …', vim.log.levels.INFO) vim.notify('nuwiki: building nuwiki-ls from source (cargo build --release) …', vim.log.levels.INFO)
local rs_repo = vim.fn.fnamemodify(root, ':h:h') .. '/nuwiki-rs' local rs_repo = vim.fn.fnamemodify(root, ':h:h') .. '/nuwiki-rs'
if not vim.uv then vim.uv = vim.loop end local uv = vim.uv or vim.loop
local rs_exists = vim.uv.fs_stat(rs_repo) local rs_exists = uv.fs_stat(rs_repo)
if not rs_exists then if not rs_exists then
vim.notify('nuwiki: cloning nuwiki-rs …', vim.log.levels.INFO) vim.notify('nuwiki: cloning nuwiki-rs …', vim.log.levels.INFO)
local _ = vim.fn.system({ local _ = vim.fn.system({
+10 -18
View File
@@ -97,30 +97,22 @@ function link.prev() jump_to_pattern([[\[\[]], -1) end
-- approximate: when the current line starts with a list marker, insert -- approximate: when the current line starts with a list marker, insert
-- the marker on the new line; otherwise fall back to plain o/O. -- the marker on the new line; otherwise fall back to plain o/O.
local function open_below_with_bullet() -- `cmd` is 'o' (open below) or 'O' (open above). Off a list item it's a plain
-- o/O. The below ('o') variant also re-adds a `[ ] ` checkbox prefix when the
-- source item has one, so the continuation is a fresh unchecked item; the
-- above ('O') variant inserts a bare bullet (preserves prior behaviour).
local function open_with_bullet(cmd)
local line = vim.fn.getline('.') local line = vim.fn.getline('.')
local indent, marker = line:match('^(%s*)([%-%*#])%s') local indent, marker = line:match('^(%s*)([%-%*#])%s')
if not marker then if not marker then
vim.api.nvim_feedkeys('o', 'n', false) vim.api.nvim_feedkeys(cmd, 'n', false)
return return
end end
local prefix = indent .. marker .. ' ' local prefix = indent .. marker .. ' '
-- Also preserve checkbox if present if cmd == 'o' and line:match('^%s*[%-%*#]%s+(%[[%sxXoO%.%-]%])') then
local checkbox = line:match('^%s*[%-%*#]%s+(%[[%sxXoO%.%-]%])')
if checkbox then
prefix = prefix .. '[ ] ' prefix = prefix .. '[ ] '
end end
vim.api.nvim_feedkeys('o' .. prefix, 'n', false) vim.api.nvim_feedkeys(cmd .. prefix, 'n', false)
end
local function open_above_with_bullet()
local line = vim.fn.getline('.')
local indent, marker = line:match('^(%s*)([%-%*#])%s')
if not marker then
vim.api.nvim_feedkeys('O', 'n', false)
return
end
local prefix = indent .. marker .. ' '
vim.api.nvim_feedkeys('O' .. prefix, 'n', false)
end end
-- ===== Public attach ===== -- ===== Public attach =====
@@ -253,8 +245,8 @@ function M.attach(bufnr, mappings)
{ desc = 'nuwiki: remove checkbox (current item)' }, bufnr) { desc = 'nuwiki: remove checkbox (current item)' }, bufnr)
map('n', 'gL', cmd.list_remove_checkbox_in_list, map('n', 'gL', cmd.list_remove_checkbox_in_list,
{ desc = 'nuwiki: remove checkboxes (whole list)' }, bufnr) { desc = 'nuwiki: remove checkboxes (whole list)' }, bufnr)
map('n', 'o', open_below_with_bullet, { desc = 'nuwiki: open below + bullet' }, bufnr) map('n', 'o', function() open_with_bullet('o') end, { desc = 'nuwiki: open below + bullet' }, bufnr)
map('n', 'O', open_above_with_bullet, { desc = 'nuwiki: open above + bullet' }, bufnr) map('n', 'O', function() open_with_bullet('O') end, { desc = 'nuwiki: open above + bullet' }, bufnr)
-- Insert-mode bindings (matches upstream vimwiki). The list helpers -- Insert-mode bindings (matches upstream vimwiki). The list helpers
-- mutate the buffer via the LSP / `nvim_buf_set_lines`, both of -- mutate the buffer via the LSP / `nvim_buf_set_lines`, both of
+3 -1
View File
@@ -64,7 +64,9 @@ local function root_dir_for(buf_file)
if opts.wikis then if opts.wikis then
local buf_norm = vim.fs.normalize(buf_file) local buf_norm = vim.fs.normalize(buf_file)
for _, w in ipairs(opts.wikis) do for _, w in ipairs(opts.wikis) do
local root = vim.fn.expand(w.root or '') -- Normalize both sides so the prefix compare survives mixed path
-- separators (e.g. on Windows); buf_norm is already normalized.
local root = vim.fs.normalize(vim.fn.expand(w.root or ''))
if root ~= '' and buf_norm:sub(1, #root) == root then if root ~= '' and buf_norm:sub(1, #root) == root then
return root return root
end end