347e2f02f7
Two missing pieces in the previous "follow creates page" fix:
1. **`wiki_root` never reached the server.** The Lua glue (and the
Vim autoload glue) was sending the config under `settings` — that
field is for `workspace/didChangeConfiguration` notifications, not
for `initialize`. The server's `Config::from_init_params` reads
`initialization_options`, so `wiki_root` arrived as `None`, the
`Wiki` aggregate was empty, and `wiki_for_uri` returned `None` →
`resolve_target_uri` bailed before reaching my synthesise path,
leaving `<CR>` with "No Location Found".
Both glues now send the same payload under both keys:
- `lua/nuwiki/lsp.lua` → adds `init_options = init_options()`
alongside the existing `settings = …`.
- `autoload/nuwiki/lsp.vim` → adds `initialization_options` to
the `lsp#register_server` call.
The server keeps reading from `initializationOptions` at startup
*and* honouring `didChangeConfiguration` later (Phase 11 plumbing).
2. **No-wiki fallback in the server.** Even with the glue fix above,
users who launch nuwiki on an ad-hoc `.wiki` file outside any
workspace folder would still get an empty `wikis` list. The
`LinkKind::Wiki` arm now falls back to a new
`synthesise_page_uri_next_to(source_uri, path)` which builds the
future page next to the current file. `<CR>` on `[[NewPage]]`
opens `./NewPage.wiki`, save creates it.
3. **`<CR>` on a plain word now wraps + follows.** Mirrors vimwiki's
`:VimwikiFollowLink`: when the cursor isn't already inside
`[[…]]`, the new `nuwiki.commands.follow_link_or_create` /
`nuwiki#commands#follow_link_or_create` first wraps the word
under cursor as `[[word]]`, places the cursor inside the link,
then dispatches `vim.lsp.buf.definition()` /
`:LspDefinition`. Both editor paths now bind `<CR>` /
`<S-CR>` / `<C-CR>` / `<C-S-CR>` to this smart variant.
Verified end-to-end:
- `[[NewPage]]` with the rebuilt binary: definition returns
`file:///tmp/wiki/NewPage.wiki` as a Location, so the editor
opens an empty buffer for it.
- A bare `MyPage` word becomes `[[MyPage]]` in the buffer before
the follow request fires.
Total 381 tests still pass; fmt + clippy clean.
Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
77 lines
2.1 KiB
Lua
77 lines
2.1 KiB
Lua
-- lua/nuwiki/lsp.lua — registration of the nuwiki LSP server in Neovim.
|
|
--
|
|
-- Prefers Neovim 0.11+'s declarative `vim.lsp.config{}` + `vim.lsp.enable`.
|
|
-- Falls back to a FileType autocmd that calls `vim.lsp.start` for older
|
|
-- versions (we still officially support only 0.11+; the fallback exists so
|
|
-- the plugin degrades gracefully rather than erroring out).
|
|
|
|
local M = {}
|
|
|
|
local config = require('nuwiki.config')
|
|
local install = require('nuwiki.install')
|
|
|
|
local function command()
|
|
return { install.expected_path() }
|
|
end
|
|
|
|
--- `initializationOptions` payload: the server reads it once at
|
|
--- `initialize` time via `Config::from_init_params`. This is what
|
|
--- delivers `wiki_root`/`wikis`/HTML/diary config to Rust.
|
|
local function init_options()
|
|
return config.options
|
|
end
|
|
|
|
--- `settings` payload: the same shape, but sent via
|
|
--- `workspace/didChangeConfiguration` after startup. Lets the server
|
|
--- pick up config changes without a restart.
|
|
local function server_settings()
|
|
return {
|
|
nuwiki = config.options,
|
|
}
|
|
end
|
|
|
|
local function root_dir_for(buf_file)
|
|
-- Walk up from the buffer until we hit a workspace marker, or fall back
|
|
-- to the configured wiki_root.
|
|
local found = vim.fs.find(
|
|
{ '.git', '.nuwiki' },
|
|
{ upward = true, path = vim.fs.dirname(buf_file) }
|
|
)[1]
|
|
if found then
|
|
return vim.fs.dirname(found)
|
|
end
|
|
return vim.fn.expand(config.options.wiki_root)
|
|
end
|
|
|
|
function M.register()
|
|
if vim.lsp.config and vim.lsp.enable then
|
|
-- Neovim 0.11+: declarative server registration.
|
|
vim.lsp.config('nuwiki', {
|
|
cmd = command(),
|
|
filetypes = { 'vimwiki' },
|
|
root_markers = { '.git', '.nuwiki' },
|
|
init_options = init_options(),
|
|
settings = server_settings(),
|
|
})
|
|
vim.lsp.enable('nuwiki')
|
|
return
|
|
end
|
|
|
|
-- Pre-0.11 fallback path.
|
|
vim.api.nvim_create_autocmd('FileType', {
|
|
pattern = 'vimwiki',
|
|
desc = 'start nuwiki language server',
|
|
callback = function(ev)
|
|
vim.lsp.start({
|
|
name = 'nuwiki',
|
|
cmd = command(),
|
|
root_dir = root_dir_for(ev.file),
|
|
init_options = init_options(),
|
|
settings = server_settings(),
|
|
})
|
|
end,
|
|
})
|
|
end
|
|
|
|
return M
|