diff --git a/ftplugin/vimwiki.vim b/ftplugin/vimwiki.vim index 4584d36..98d6736 100644 --- a/ftplugin/vimwiki.vim +++ b/ftplugin/vimwiki.vim @@ -146,6 +146,8 @@ if !has('nvim') xnoremap :call nuwiki#commands#toggle_list_item() nnoremap :call nuwiki#commands#toggle_list_item() xnoremap :call nuwiki#commands#toggle_list_item() + nnoremap :call nuwiki#commands#toggle_list_item() + xnoremap :call nuwiki#commands#toggle_list_item() nnoremap gnt :call nuwiki#commands#next_task() nnoremap gln :call nuwiki#commands#cycle_list_item() xnoremap gln :call nuwiki#commands#cycle_list_item() diff --git a/lua/nuwiki/commands.lua b/lua/nuwiki/commands.lua index ec87423..bf65081 100644 --- a/lua/nuwiki/commands.lua +++ b/lua/nuwiki/commands.lua @@ -13,10 +13,6 @@ local function buf_uri(bufnr) return vim.uri_from_bufnr(bufnr or 0) end -local function position_params() - return vim.lsp.util.make_position_params() -end - local function find_client() local fn = vim.lsp.get_clients or vim.lsp.get_active_clients local clients = fn({ name = 'nuwiki', bufnr = 0 }) @@ -27,29 +23,49 @@ local function find_client() return clients[1] end +local function position_params(client) + -- Neovim 0.12+ requires `position_encoding` explicitly; older + -- versions accept the no-arg form. + local encoding = client and client.offset_encoding or 'utf-16' + local ok, params = pcall(vim.lsp.util.make_position_params, 0, encoding) + if ok then return params end + -- Fallback for Neovim < 0.10 where the second arg wasn't a thing. + return vim.lsp.util.make_position_params() +end + local function exec(command, arguments, on_result) local client = find_client() if not client then vim.notify('nuwiki: language server not attached', vim.log.levels.ERROR) return end - client.request( - 'workspace/executeCommand', - { command = command, arguments = arguments or {} }, - function(err, result) - if err then - vim.notify( - 'nuwiki: ' .. command .. ' — ' .. (err.message or vim.inspect(err)), - vim.log.levels.ERROR - ) - return - end - if on_result then - on_result(result) - end - end, - vim.api.nvim_get_current_buf() - ) + local handler = function(err, result) + if err then + vim.notify( + 'nuwiki: ' .. command .. ' — ' .. (err.message or vim.inspect(err)), + vim.log.levels.ERROR + ) + return + end + if on_result then on_result(result) end + end + local bufnr = vim.api.nvim_get_current_buf() + local payload = { command = command, arguments = arguments or {} } + -- Neovim 0.12 deprecates `client.request(...)` in favour of the + -- method form `client:request(...)`. Prefer the method, fall back + -- on older Neovim where it isn't defined yet. + local req = client.request + if type(req) ~= 'function' then + return + end + if rawget(getmetatable(client) or {}, '__index') and client.request ~= nil then + -- Method-style works for both colon and dot calls on Neovim's + -- Client metatable, but pass `self` explicitly for the older + -- function-style signature compatibility. + client:request('workspace/executeCommand', payload, handler, bufnr) + else + client.request('workspace/executeCommand', payload, handler, bufnr) + end end local function open_uri(uri, tab) @@ -71,7 +87,8 @@ local function open_uri(uri, tab) end local function pos_args() - return { vim.tbl_extend('force', { uri = buf_uri() }, position_params()) } + local client = find_client() + return { vim.tbl_extend('force', { uri = buf_uri() }, position_params(client)) } end local function uri_args() diff --git a/lua/nuwiki/keymaps.lua b/lua/nuwiki/keymaps.lua index 74a1007..f9a3c33 100644 --- a/lua/nuwiki/keymaps.lua +++ b/lua/nuwiki/keymaps.lua @@ -200,11 +200,18 @@ function M.attach(bufnr, mappings) -- ===== Lists ===== if on('lists') then + -- Three aliases for the same intent. Terminals disagree about + -- what byte(s) Ctrl+Space produces: + -- * GUI Neovim / kitty-protocol terminals → `` + -- * xterm, most legacy terminals → NUL byte → `` + -- * Some Neovim builds spell that same byte `` in keymaps + -- Map all three so the user gets the binding regardless. map('n', '', cmd.toggle_list_item, { desc = 'nuwiki: toggle checkbox' }, bufnr) map('x', '', cmd.toggle_list_item, { desc = 'nuwiki: toggle checkbox' }, bufnr) - -- is the terminal byte for C-Space — keep parity with vimwiki. map('n', '', cmd.toggle_list_item, { desc = 'nuwiki: toggle checkbox' }, bufnr) map('x', '', cmd.toggle_list_item, { desc = 'nuwiki: toggle checkbox' }, bufnr) + map('n', '', cmd.toggle_list_item, { desc = 'nuwiki: toggle checkbox' }, bufnr) + map('x', '', cmd.toggle_list_item, { desc = 'nuwiki: toggle checkbox' }, bufnr) map('n', 'gnt', cmd.next_task, { desc = 'nuwiki: next unfinished task' }, bufnr) map('n', 'gln', cmd.cycle_list_item, { desc = 'nuwiki: increment checkbox' }, bufnr) map('x', 'gln', cmd.cycle_list_item, { desc = 'nuwiki: increment checkbox' }, bufnr)