feat(folding): open every heading fold by default on file open
CI / cargo fmt --check (push) Failing after 32s
CI / cargo clippy (push) Successful in 15s
CI / cargo test (push) Successful in 21s
CI / editor keymaps (push) Successful in 1m6s

Heading-block folds were computed but all collapsed on file open,
so users had to `zR` after every `:edit`. Set `foldlevel=99` once at
ftplugin attach so the fold structure is still there (closeable with
`zc`/`zM`) but the buffer renders fully expanded.

`foldlevel` is window-local; we set it on initial attach only — not
in the Lua BufWinEnter re-apply — so closing folds with `zc` in a
window survives navigating away and back to that buffer. Split
windows still get the same value because Vim copies window-local
options to the new window when splitting.

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
This commit is contained in:
2026-05-26 23:02:17 -03:00
parent 0dfe0910fc
commit 59b48b4786
3 changed files with 15 additions and 1 deletions
+5 -1
View File
@@ -221,7 +221,11 @@ if !has('nvim')
setlocal foldmethod=expr
setlocal foldexpr=nuwiki#folding#expr()
setlocal foldtext=nuwiki#folding#foldtext()
let b:undo_ftplugin .= ' | setlocal foldmethod< foldexpr< foldtext<'
" Start with every heading-block fold open; the user can still close
" them with `zc` and that state survives because `foldlevel` only
" controls the initial state, not subsequent manual fold operations.
setlocal foldlevel=99
let b:undo_ftplugin .= ' | setlocal foldmethod< foldexpr< foldtext< foldlevel<'
endif
" Text objects — vimwiki parity (matches lua/nuwiki/textobjects.lua).
+4
View File
@@ -35,6 +35,10 @@ local function setup_folding(bufnr, folding_mode)
-- 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 }),
+6
View File
@@ -300,6 +300,12 @@ call s:record(
\ foldlevel(2) > 0 ? 1 : 0,
\ 'folding.body_inherits_fold',
\ 'foldlevel(2)=' . foldlevel(2))
" ftplugin sets `foldlevel=99` so every heading-block fold starts open
" rather than collapsed on file open.
call s:record(
\ &l:foldlevel == 99 ? 1 : 0,
\ 'folding.starts_with_all_folds_open',
\ '&l:foldlevel=' . &l:foldlevel)
" ===== Wrap up =====