4bee216c05
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>
138 lines
4.7 KiB
Lua
138 lines
4.7 KiB
Lua
-- lua/nuwiki/ftplugin.lua — per-buffer hookup invoked by
|
|
-- `ftplugin/nuwiki.vim`. Centralises the Lua side of the per-buffer
|
|
-- glue so the VimL ftplugin stays a one-liner regardless of which subgroups are
|
|
-- enabled.
|
|
|
|
local M = {}
|
|
|
|
local function setup_folding(bufnr, folding_mode)
|
|
if folding_mode == 'off' then
|
|
return
|
|
end
|
|
-- `foldmethod` / `foldexpr` / `foldtext` are window-local options.
|
|
-- The ftplugin runs during BufRead → FileType, by which time the
|
|
-- buffer is displayed in the current window. `vim.opt_local` writes
|
|
-- to the current window's view of the option.
|
|
-- The LSP foldexpr is wrapped via `nuwiki.folding.lsp_expr` so a
|
|
-- transient client failure (e.g. server still starting, buffer not
|
|
-- yet attached) degrades to the regex variant instead of spamming
|
|
-- the user with E5108 errors per line.
|
|
local apply = function()
|
|
vim.opt_local.foldmethod = 'expr'
|
|
vim.opt_local.foldtext = 'v:lua.require("nuwiki.folding").foldtext()'
|
|
-- Min Neovim is 0.11, so vim.lsp.foldexpr is the only capability gate.
|
|
if folding_mode == 'expr' or not vim.lsp.foldexpr then
|
|
vim.opt_local.foldexpr = 'v:lua.require("nuwiki.folding").expr()'
|
|
else
|
|
vim.opt_local.foldexpr = 'v:lua.require("nuwiki.folding").lsp_expr()'
|
|
end
|
|
end
|
|
|
|
-- Set on the current window first (the one that triggered the
|
|
-- ftplugin). Also re-apply on any future split that views this
|
|
-- buffer, so `<C-w>v` / `:split` preserves the fold method.
|
|
apply()
|
|
-- Start with every heading-block fold open. `foldlevel` is window-local
|
|
-- but only sets the initial state; once the user closes folds with `zc`
|
|
-- those stay closed, so we don't re-apply this on BufWinEnter.
|
|
vim.opt_local.foldlevel = 99
|
|
vim.api.nvim_create_autocmd('BufWinEnter', {
|
|
buffer = bufnr,
|
|
group = vim.api.nvim_create_augroup('nuwiki_folding_' .. bufnr, { clear = true }),
|
|
callback = apply,
|
|
})
|
|
end
|
|
|
|
-- vimwiki `autowriteall`: while this wiki buffer is current, mirror the option
|
|
-- into Vim's global 'autowriteall' (auto-save on switch/make), restoring the
|
|
-- prior value on leave.
|
|
local function setup_autowriteall(bufnr, enabled)
|
|
if not enabled then
|
|
return
|
|
end
|
|
if bufnr == 0 then
|
|
bufnr = vim.api.nvim_get_current_buf()
|
|
end
|
|
local grp = vim.api.nvim_create_augroup('nuwiki_awa_' .. bufnr, { clear = true })
|
|
local function enter()
|
|
vim.b[bufnr].nuwiki_awa_saved = vim.o.autowriteall
|
|
vim.o.autowriteall = true
|
|
end
|
|
local function leave()
|
|
local saved = vim.b[bufnr].nuwiki_awa_saved
|
|
if saved ~= nil then
|
|
vim.o.autowriteall = saved
|
|
end
|
|
end
|
|
vim.api.nvim_create_autocmd('BufEnter', { buffer = bufnr, group = grp, callback = enter })
|
|
vim.api.nvim_create_autocmd('BufLeave', { buffer = bufnr, group = grp, callback = leave })
|
|
-- FileType fires after the initial BufEnter, so apply once now too.
|
|
enter()
|
|
end
|
|
|
|
-- vimwiki `table_auto_fmt`: re-align the table under the cursor on InsertLeave.
|
|
local function setup_table_auto_fmt(bufnr, enabled)
|
|
if not enabled then
|
|
return
|
|
end
|
|
if bufnr == 0 then
|
|
bufnr = vim.api.nvim_get_current_buf()
|
|
end
|
|
local grp = vim.api.nvim_create_augroup('nuwiki_taf_' .. bufnr, { clear = true })
|
|
vim.api.nvim_create_autocmd('InsertLeave', {
|
|
buffer = bufnr,
|
|
group = grp,
|
|
callback = function()
|
|
-- Cheap guard: only round-trip to the server on a table row.
|
|
if vim.api.nvim_get_current_line():find('|', 1, true) then
|
|
require('nuwiki.commands').table_align()
|
|
end
|
|
end,
|
|
})
|
|
end
|
|
|
|
-- vimwiki `auto_chdir`: `:lcd` into the owning wiki's root while this buffer
|
|
-- is current.
|
|
local function setup_auto_chdir(bufnr, enabled)
|
|
if not enabled then
|
|
return
|
|
end
|
|
if bufnr == 0 then
|
|
bufnr = vim.api.nvim_get_current_buf()
|
|
end
|
|
local function chdir()
|
|
local file = vim.api.nvim_buf_get_name(bufnr)
|
|
local root = require('nuwiki.config').wiki_root_for(file)
|
|
if root then
|
|
vim.cmd('lcd ' .. vim.fn.fnameescape(root))
|
|
end
|
|
end
|
|
local grp = vim.api.nvim_create_augroup('nuwiki_chdir_' .. bufnr, { clear = true })
|
|
vim.api.nvim_create_autocmd({ 'BufEnter', 'BufWinEnter' }, {
|
|
buffer = bufnr,
|
|
group = grp,
|
|
callback = chdir,
|
|
})
|
|
chdir()
|
|
end
|
|
|
|
function M.attach(bufnr)
|
|
bufnr = bufnr or 0
|
|
local config = require('nuwiki.config')
|
|
local opts = config.options or config.defaults
|
|
|
|
if opts.mappings and opts.mappings.enabled ~= false then
|
|
require('nuwiki.keymaps').attach(bufnr, opts.mappings)
|
|
if opts.mappings.text_objects ~= false then
|
|
require('nuwiki.textobjects').attach(bufnr)
|
|
end
|
|
end
|
|
|
|
setup_folding(bufnr, opts.folding or 'lsp')
|
|
setup_autowriteall(bufnr, opts.autowriteall ~= false)
|
|
setup_table_auto_fmt(bufnr, opts.table_auto_fmt ~= false)
|
|
setup_auto_chdir(bufnr, opts.auto_chdir == true)
|
|
end
|
|
|
|
return M
|