fix(wiki): build wiki picker list from config so it works before opening a file
CI / cargo fmt --check (push) Successful in 36s
CI / cargo clippy (push) Successful in 21s
CI / cargo test (push) Successful in 27s
CI / editor keymaps (push) Successful in 1m14s

The picker fetched the wiki list via workspace/executeCommand, but the LSP
only starts once a vimwiki buffer exists — so a wiki could not be selected
until one was already open (chicken-and-egg). Read the list straight from
config instead (g:nuwiki_wikis / scalar fallback, the same source as
open_wiki_path) and open the chosen index directly; that auto-starts the
server via the FileType autocmd.

- Vim: new public nuwiki#commands#wiki_list(); wiki_ui_select() uses it +
  inputlist() + :edit. Global :VimwikiUISelect / :NuwikiUISelect commands.
- Neovim: config.wiki_cfg(n) + config.wiki_list() (init.lua delegates);
  commands.wiki_ui_select() uses them + vim.ui.select + :edit; global
  VimwikiUISelect / NuwikiUISelect user commands registered in setup().

Co-Authored-By: Claude Opus 4.7 <noreply@anthropic.com>
This commit is contained in:
2026-05-28 20:04:31 -03:00
parent d6672fe218
commit 7f6d811a47
7 changed files with 156 additions and 64 deletions
+21 -22
View File
@@ -247,30 +247,29 @@ function M.wiki_tab_index(count)
end)
end
-- Pick a wiki and open its index. The list is read straight from config
-- (no LSP round-trip) so this works from any buffer before a wiki file is
-- ever opened; opening the index then auto-starts the server.
function M.wiki_ui_select()
exec('nuwiki.wiki.select', { {} }, function(r)
if type(r) ~= 'table' or #r == 0 then
vim.notify('nuwiki: no wikis configured')
return
local wikis = require('nuwiki.config').wiki_list()
if #wikis == 0 then
vim.notify('nuwiki: no wikis configured', vim.log.levels.WARN)
return
end
vim.ui.select(
wikis,
{
prompt = 'Select a wiki',
format_item = function(w)
return string.format('%s (%s)', w.name, w.root)
end,
},
function(choice)
if not choice then return end
local path = choice.root .. '/' .. choice.idx .. choice.ext
vim.cmd('edit ' .. vim.fn.fnameescape(vim.fn.expand(path)))
end
vim.ui.select(
r,
{
prompt = 'Pick a wiki',
format_item = function(w)
return string.format('%d. %s (%s)', (w.id or 0) + 1, w.name or '?', tostring(w.root))
end,
},
function(choice)
if not choice then return end
exec(
'nuwiki.wiki.openIndex',
{ { wiki = choice.id } },
function(rr) if rr and rr.uri then open_uri(rr.uri) end end
)
end
)
end)
)
end
function M.wiki_goto_page(name)
+35
View File
@@ -59,4 +59,39 @@ function M.apply(user)
M.options = vim.tbl_deep_extend('force', M.defaults, user or {})
end
-- Resolve the config for wiki #n (0-based) without touching the LSP.
-- Priority: setup() opts.wikis → g:nuwiki_wikis (VimL) → scalar opts/g: vars.
-- Returns { name, root, ext, idx, diary_rel, diary_idx }.
function M.wiki_cfg(n)
local opts = M.options
local wikis = opts.wikis or vim.g.nuwiki_wikis or nil
local w = wikis and wikis[(n or 0) + 1] or nil
local root = (w and w.root) or opts.wiki_root or vim.g.nuwiki_wiki_root or '~/vimwiki'
local ext = (w and w.file_extension) or opts.file_extension or vim.g.nuwiki_file_extension or '.wiki'
if not ext:match('^%.') then ext = '.' .. ext end
return {
name = (w and w.name) or vim.fn.fnamemodify(vim.fn.expand(root), ':t'),
root = root,
ext = ext,
idx = (w and w.index) or 'index',
diary_rel = (w and w.diary_rel_path) or vim.g.nuwiki_diary_rel_path or 'diary',
diary_idx = (w and w.diary_index) or 'diary',
}
end
-- The full list of configured wikis as wiki_cfg dicts. Falls back to a single
-- entry built from the scalar wiki_root config when no list is configured.
function M.wiki_list()
local wikis = M.options.wikis or vim.g.nuwiki_wikis or nil
local list = {}
if type(wikis) == 'table' and #wikis > 0 then
for i = 1, #wikis do
list[i] = M.wiki_cfg(i - 1)
end
else
list[1] = M.wiki_cfg(0)
end
return list
end
return M
+13 -24
View File
@@ -18,31 +18,9 @@ local function _open(path, open_cmd)
vim.cmd((open_cmd or 'edit') .. ' ' .. vim.fn.fnameescape(vim.fn.expand(path)))
end
-- Return the config dict for wiki #n (0-based).
-- Priority: setup() opts.wikis → g:nuwiki_wikis (VimL) → scalar opts/g: vars.
-- Return the config dict for wiki #n (0-based). See config.wiki_cfg.
local function _wiki_cfg(n)
local opts = config.options
local wikis = opts.wikis
or (vim.g.nuwiki_wikis and vim.g.nuwiki_wikis or nil)
local w = wikis and wikis[(n or 0) + 1] or nil
local root = (w and w.root)
or opts.wiki_root
or vim.g.nuwiki_wiki_root
or '~/vimwiki'
local ext = (w and w.file_extension)
or opts.file_extension
or vim.g.nuwiki_file_extension
or '.wiki'
if not ext:match('^%.') then ext = '.' .. ext end
return {
root = root,
ext = ext,
idx = (w and w.index) or 'index',
diary_rel = (w and w.diary_rel_path)
or vim.g.nuwiki_diary_rel_path
or 'diary',
diary_idx = (w and w.diary_index) or 'diary',
}
return config.wiki_cfg(n)
end
-- Compute the wiki index path for wiki #n (0-based) from config.
@@ -81,6 +59,16 @@ local function _setup_global_mappings()
kmap('n', '<Leader>w<Leader>i', function() require('nuwiki.commands').diary_generate_index() end, o('rebuild diary index'))
end
-- Global wiki-picker commands so a wiki can be chosen from any buffer before
-- a .wiki file exists. The picker reads its list from config (no LSP); the
-- buffer-local versions in ftplugin/vimwiki.vim shadow these inside wiki
-- buffers but dispatch to the same function.
local function _setup_global_commands()
local function ui_select() require('nuwiki.commands').wiki_ui_select() end
vim.api.nvim_create_user_command('VimwikiUISelect', ui_select, {})
vim.api.nvim_create_user_command('NuwikiUISelect', ui_select, {})
end
function M.setup(opts)
config.apply(opts or {})
@@ -99,6 +87,7 @@ function M.setup(opts)
lsp.register()
_setup_global_mappings()
_setup_global_commands()
end
function M.install()