Files
nuwiki/lua/nuwiki/init.lua
T
gffranco 21b485c91b Remove stale phase/spec references from code comments
Strip "Phase N", "SPEC §X.Y", and "v1.x" planning labels from comments
across the Rust crates and editor layers now that those tracking
artifacts are gone. Comments only — no logic, strings, or test names
touched.

Co-Authored-By: Claude Opus 4.7 <noreply@anthropic.com>
2026-05-30 15:25:51 -03:00

102 lines
4.0 KiB
Lua

-- lua/nuwiki/init.lua — Neovim entry point.
--
-- Public surface:
-- require('nuwiki').setup(opts) — apply user config + register LSP
-- require('nuwiki').install() — install / build the language server
-- require('nuwiki').health() — :checkhealth nuwiki target
--
-- This layer is wiring only. Everything substantive
-- happens in the Rust language server.
local M = {}
local config = require('nuwiki.config')
local lsp = require('nuwiki.lsp')
-- Open a file directly, expanding ~ / env vars in the path.
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). See config.wiki_cfg.
local function _wiki_cfg(n)
return config.wiki_cfg(n)
end
-- Compute the wiki index path for wiki #n (0-based) from config.
local function _wiki_index_path(n)
local c = _wiki_cfg(n)
return c.root .. '/' .. c.idx .. c.ext
end
-- Compute a diary file path from config. day_offset=0 → today.
-- Pass nil to get the diary index file instead.
local function _diary_path(day_offset)
local c = _wiki_cfg(0)
if day_offset == nil then
return c.root .. '/' .. c.diary_rel .. '/' .. c.diary_idx .. c.ext
end
local t = os.time() + day_offset * 86400
return c.root .. '/' .. c.diary_rel .. '/' .. os.date('%Y-%m-%d', t) .. c.ext
end
-- Register global (non-buffer-local) entry-point keymaps so users can open
-- their wiki from any buffer, not just from inside a .wiki file. Files are
-- opened directly from config — no LSP client required; the server auto-starts
-- once the vimwiki FileType fires on the newly opened buffer.
local function _setup_global_mappings()
if vim.g.nuwiki_no_default_mappings then return end
local kmap = vim.keymap.set
local function o(d) return { silent = true, desc = 'nuwiki: ' .. d } end
kmap('n', '<Leader>ww', function() _open(_wiki_index_path(0)) end, o('wiki index'))
kmap('n', '<Leader>wt', function() _open(_wiki_index_path(0), 'tabedit') end, o('wiki index (tab)'))
kmap('n', '<Leader>ws', function() require('nuwiki.commands').wiki_ui_select() end, o('pick wiki'))
kmap('n', '<Leader>wi', function() _open(_diary_path(nil)) end, o('diary index'))
kmap('n', '<Leader>w<Leader>w', function() _open(_diary_path(0)) end, o('today diary'))
kmap('n', '<Leader>w<Leader>y', function() _open(_diary_path(-1)) end, o('yesterday diary'))
kmap('n', '<Leader>w<Leader>t', function() _open(_diary_path(1)) end, o('tomorrow diary'))
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 {})
-- Neovim 0.7+ resolves filetypes via `vim.filetype.match` before any
-- `ftdetect/*.vim` autocmd fires, and its bundled rule for `*.wiki`
-- returns `mediawiki`. Register an authoritative rule here so this
-- plugin's filetype (vimwiki) wins.
if vim.filetype and vim.filetype.add then
local extensions = { wiki = 'vimwiki' }
local ext = (config.options.file_extension or '.wiki'):gsub('^%.', '')
if ext ~= 'wiki' and ext ~= '' then
extensions[ext] = 'vimwiki'
end
vim.filetype.add({ extension = extensions })
end
lsp.register()
_setup_global_mappings()
_setup_global_commands()
end
function M.install()
return require('nuwiki.install').install()
end
function M.health()
return require('nuwiki.health').check()
end
return M