From 17738caeb4e8a669d003e43792419f4dc9aefeda Mon Sep 17 00:00:00 2001 From: Price Hiller Date: Sat, 7 Sep 2024 13:34:40 -0500 Subject: [PATCH] refactor(nvim): extract `Z` cmd to plugin --- .../price/dots/.config/nvim/lua/core/cmds.lua | 81 ------------------- .../.config/nvim/lua/plugins/configs/z.lua | 7 ++ 2 files changed, 7 insertions(+), 81 deletions(-) create mode 100644 users/price/dots/.config/nvim/lua/plugins/configs/z.lua diff --git a/users/price/dots/.config/nvim/lua/core/cmds.lua b/users/price/dots/.config/nvim/lua/core/cmds.lua index d79722e1..6618f94a 100644 --- a/users/price/dots/.config/nvim/lua/core/cmds.lua +++ b/users/price/dots/.config/nvim/lua/core/cmds.lua @@ -120,87 +120,6 @@ M.setup = function() nargs = "*", desc = "Create tempfile and cd to its directory", }) - - local config_home = vim.env.XDG_CONFIG_HOME or vim.env.HOME .. "/.config" - local z_lua_path = config_home .. "/zsh/config/plugins/z.lua/z.lua" - local cached_z_listing = {} - vim.api.nvim_create_user_command("Z", function(opts) - cached_z_listing = {} - local cmd = { "lua", z_lua_path, "-e", opts.args } - local cmd_out = vim.system(cmd, { text = true, env = { _ZL_MATCH_MODE = "1", PWD = tostring(vim.uv.cwd()) } }) - :wait() - if cmd_out.code > 0 then - vim.notify( - "Failed with code `" .. cmd_out.code .. "`\nSTDERR: " .. (cmd_out.stderr or ""), - vim.log.levels.WARN, - { - title = "z.lua", - ---@param win integer The window handle - on_open = function(win) - vim.api.nvim_set_option_value("filetype", "markdown", { buf = vim.api.nvim_win_get_buf(win) }) - end, - } - ) - elseif cmd_out.stdout == "" then - vim.notify("Did not receive a match from `z.lua`!", vim.log.levels.WARN, { - title = "z.lua", - ---@param win integer The window handle - on_open = function(win) - vim.api.nvim_set_option_value("filetype", "markdown", { buf = vim.api.nvim_win_get_buf(win) }) - end, - }) - else - local stripped_stdout = cmd_out.stdout:gsub("\n$", "") - vim.cmd("silent! cd " .. stripped_stdout) - vim.notify("Chdir to `" .. stripped_stdout .. "`", vim.log.levels.INFO, { - title = "z.lua", - ---@param win integer The window handle - on_open = function(win) - vim.api.nvim_set_option_value("filetype", "markdown", { buf = vim.api.nvim_win_get_buf(win) }) - end, - }) - end - end, { - nargs = "+", - complete = function(_, _, _) - local cmd = { "lua", z_lua_path, "--complete" } - local cmd_out - if #cached_z_listing == 0 then - cmd_out = vim.system(cmd, { text = true }):wait() - if cmd_out.code == 0 and cmd_out.stdout then - cached_z_listing = vim.split(cmd_out.stdout, "\n") - end - end - return cached_z_listing - end, - desc = "Invoke `z.lua`", - }) - - vim.api.nvim_create_autocmd("DirChanged", { - callback = function(args) - vim.system({ "lua", z_lua_path, "--add", args.file }, { text = true }, function(out) - if out.code ~= 0 then - vim.notify( - "Failed to regiser directory with `z.lua`!\n====STDERR====\n" - .. out.stderr - .. "\n====STDOUT====\n" - .. out.stdout, - vim.log.levels.WARN, - { - title = "z.lua", - on_open = function(win) - vim.api.nvim_set_option_value( - "filetype", - "markdown", - { buf = vim.api.nvim_win_get_buf(win) } - ) - end, - } - ) - end - end) - end, - }) end return M diff --git a/users/price/dots/.config/nvim/lua/plugins/configs/z.lua b/users/price/dots/.config/nvim/lua/plugins/configs/z.lua new file mode 100644 index 00000000..50aefb31 --- /dev/null +++ b/users/price/dots/.config/nvim/lua/plugins/configs/z.lua @@ -0,0 +1,7 @@ +return { + { + "PriceHiller/z.nvim", + cmd = { "Z" }, + config = true, + }, +}