Files
nuwiki/lua/nuwiki/ftplugin.lua
T
gffranco 8ab6015405
CI / cargo fmt --check (push) Successful in 33s
CI / cargo clippy (push) Successful in 23s
CI / cargo test (push) Successful in 33s
CI / editor keymaps (push) Successful in 1m25s
Major clean up and improvements to vimwiki compatibility and documentation.
Reviewed-on: #1
2026-05-30 18:35:40 +00:00

65 lines
2.4 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()
if folding_mode == 'expr'
or not (vim.fn.has('nvim-0.11') == 1 and vim.lsp.foldexpr)
then
vim.opt_local.foldmethod = 'expr'
vim.opt_local.foldexpr = 'v:lua.require("nuwiki.folding").expr()'
vim.opt_local.foldtext = 'v:lua.require("nuwiki.folding").foldtext()'
else
vim.opt_local.foldmethod = 'expr'
vim.opt_local.foldexpr = 'v:lua.require("nuwiki.folding").lsp_expr()'
vim.opt_local.foldtext = 'v:lua.require("nuwiki.folding").foldtext()'
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
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')
end
return M