diff options
| author | Alexandre Teoi <ateoi@users.noreply.github.com> | 2025-09-06 18:09:34 -0300 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2025-09-06 14:09:34 -0700 |
| commit | 64c07cda6c81ca767b4a58f090096c3e8d5abce5 (patch) | |
| tree | 06f578504566b4e1b9a3e67d603d55bfe2e53479 | |
| parent | docs: update configs.md (diff) | |
| download | nvim-lspconfig-64c07cda6c81ca767b4a58f090096c3e8d5abce5.tar nvim-lspconfig-64c07cda6c81ca767b4a58f090096c3e8d5abce5.tar.gz nvim-lspconfig-64c07cda6c81ca767b4a58f090096c3e8d5abce5.tar.bz2 nvim-lspconfig-64c07cda6c81ca767b4a58f090096c3e8d5abce5.tar.lz nvim-lspconfig-64c07cda6c81ca767b4a58f090096c3e8d5abce5.tar.xz nvim-lspconfig-64c07cda6c81ca767b4a58f090096c3e8d5abce5.tar.zst nvim-lspconfig-64c07cda6c81ca767b4a58f090096c3e8d5abce5.zip | |
feat(zk): implement the zk.tag.list command #4060
* feat(zk): implement the zk.tag.list command
* refactor(zk): use workspace_required config to disable single file support
* fix(zk): remove tbl_map to avoid possible order issue
| -rw-r--r-- | lsp/zk.lua | 110 |
1 files changed, 76 insertions, 34 deletions
@@ -1,15 +1,39 @@ ---@brief --- ---- https://github.com/mickael-menu/zk +--- https://github.com/zk-org/zk --- --- A plain text note-taking assistant -local function find_zk_root(startpath) - for dir in vim.fs.parents(startpath) do - if vim.fn.isdirectory(vim.fs.joinpath(dir, '.zk')) == 1 then - return dir +---List notes +---@param client vim.lsp.Client +---@param bufnr integer +---@param opts table +---@param action fun(path: string, title: string) +local function zk_list(client, bufnr, opts, action) + opts = vim.tbl_extend('keep', { select = { 'path', 'title' } }, opts or {}) + client:exec_cmd({ + title = 'ZkList', + command = 'zk.list', + arguments = { vim.api.nvim_buf_get_name(bufnr), opts }, + }, { bufnr = bufnr }, function(err, result) + if err ~= nil then + vim.api.nvim_echo({ { 'zk.list error\n' }, { vim.inspect(err) } }, true, {}) + return end - end + if result == nil then + return + end + + vim.ui.select(result, { + format_item = function(item) + return item.title + end, + }, function(item) + if item ~= nil then + action(item.path, item.title) + end + end) + end) end ---@type vim.lsp.Config @@ -17,58 +41,76 @@ return { cmd = { 'zk', 'lsp' }, filetypes = { 'markdown' }, root_markers = { '.zk' }, - ---@param bufnr integer - ---@param client vim.lsp.Client + workspace_required = true, on_attach = function(client, bufnr) vim.api.nvim_buf_create_user_command(bufnr, 'LspZkIndex', function() client:exec_cmd({ title = 'ZkIndex', command = 'zk.index', arguments = { vim.api.nvim_buf_get_name(bufnr) }, - }, { bufnr = bufnr }) - end, { - desc = 'ZkIndex', - }) + }, { bufnr = bufnr }, function(err, result) + if err ~= nil then + vim.api.nvim_echo({ { 'zk.index error\n' }, { vim.inspect(err) } }, true, {}) + return + end + if result ~= nil then + vim.api.nvim_echo({ { vim.inspect(result) } }, false, {}) + end + end) + end, { desc = 'ZkIndex' }) vim.api.nvim_buf_create_user_command(bufnr, 'LspZkList', function() - local bufpath = vim.api.nvim_buf_get_name(bufnr) - local root = find_zk_root(bufpath) + zk_list(client, bufnr, {}, function(path) + vim.cmd('edit ' .. path) + end) + end, { desc = 'ZkList' }) + vim.api.nvim_buf_create_user_command(bufnr, 'LspZkTagList', function() client:exec_cmd({ - title = 'ZkList', - command = 'zk.list', - arguments = { root, { select = { 'path' } } }, - }, { bufnr = bufnr }, function(_err, result) - if not result then + title = 'ZkTagList', + command = 'zk.tag.list', + arguments = { vim.api.nvim_buf_get_name(bufnr) }, + }, { bufnr = bufnr }, function(err, result) + if err ~= nil then + vim.api.nvim_echo({ { 'zk.tag.list error\n' }, { vim.inspect(err) } }, true, {}) + return + end + if result == nil then return end - local paths = vim.tbl_map(function(item) - return item.path - end, result) - vim.ui.select(paths, {}, function(choice) - vim.cmd('edit ' .. choice) + + vim.ui.select(result, { + format_item = function(item) + return item.name + end, + }, function(item) + if item ~= nil then + zk_list(client, bufnr, { tags = { item.name } }, function(path) + vim.cmd('edit ' .. path) + end) + end end) end) - end, { - desc = 'ZkList', - }) + end, { desc = 'ZkTagList' }) - vim.api.nvim_buf_create_user_command(bufnr, 'LspZkNew', function(...) + vim.api.nvim_buf_create_user_command(bufnr, 'LspZkNew', function(args) + local title = #args.fargs >= 1 and args.fargs[1] or '' + local dir = #args.fargs >= 2 and args.fargs[2] or '' client:exec_cmd({ title = 'ZkNew', command = 'zk.new', arguments = { vim.api.nvim_buf_get_name(bufnr), - ..., + { title = title, dir = dir }, }, - }, { bufnr = bufnr }, function(_err, result) - if not (result and result.path) then + }, { bufnr = bufnr }, function(err, result) + if err ~= nil then + vim.api.nvim_echo({ { 'zk.new error\n' }, { vim.inspect(err) } }, true, {}) return end + vim.cmd('edit ' .. result.path) end) - end, { - desc = 'ZkNew', - }) + end, { desc = 'ZkNew [title] [dir]', nargs = '*' }) end, } |
