darkrc/vim/plugin/stacc.lua

72 lines
1.9 KiB
Lua
Raw Normal View History

2024-10-29 10:56:27 +00:00
local Stacc = require "stacc"
2024-10-29 10:56:27 +00:00
local function staccmap()
return setmetatable({}, {__index=function(self, key)
self[key] = Stacc:new {}
return self[key]
end})
end
local windows = staccmap()
local buffers = staccmap()
local augroup = vim.api.nvim_create_augroup("stacc", {})
vim.api.nvim_create_autocmd({"WinClosed"}, {
group = augroup;
callback = function(arguments)
windows[arguments.match] = nil
end
})
local handler = function(params)
local command = params.name
local stacc if command:find("^G") then
stacc = Stacc
elseif command:find("^W") then
stacc = windows[vim.fn.win_getid()]
elseif command:find("^B") then
stacc = buffers[vim.fn.bufnr()]
else -- Default to per-window stack
stacc = windows[vim.fn.win_getid()]
end
2024-07-10 19:57:34 +00:00
if params.args == "" then
local count = #stacc.locations
2024-10-29 10:10:59 +00:00
if count == 0 then
2024-10-29 10:56:27 +00:00
print("Stacc is empty. Push with `:"..command.." description of current task`")
2024-10-29 11:08:25 +00:00
print("Manipulate with :"..command.." pop|drop|clear")
2024-10-29 10:10:59 +00:00
else
for i, location in ipairs(stacc.locations) do
local bufnr, id, description = unpack(location)
--- @type number[]
local mark = vim.api.nvim_buf_get_extmark_by_id(bufnr, stacc.namespace, id, {})
print(string.format("%s %s:%i -- %s",
string.format("%" .. math.floor(math.log(count, 10)+1) .. "i", i),
vim.api.nvim_buf_get_name(bufnr):match("[^/]*$"),
mark[1] + 1,
description
))
end
2024-07-10 19:57:34 +00:00
end
2024-10-29 11:08:25 +00:00
elseif params.args == "drop" then
stacc:drop(1)
2024-07-10 19:57:34 +00:00
elseif params.args == "clear" then
2024-10-29 10:56:27 +00:00
stacc:clear()
2024-07-10 19:57:34 +00:00
elseif params.args == "pop" then
2024-10-29 10:56:27 +00:00
local message, bufnr = stacc:pop(1)
2024-07-10 19:57:34 +00:00
print(string.format("[%s] %s",
vim.api.nvim_buf_get_name(bufnr):match("[^/]*$"),
message
))
else
2024-10-29 10:56:27 +00:00
stacc:push(params.args)
end
2024-10-29 10:56:27 +00:00
end
vim.api.nvim_create_user_command("GStacc", handler, {nargs="?"})
vim.api.nvim_create_user_command("WStacc", handler, {nargs="?"})
vim.api.nvim_create_user_command("BStacc", handler, {nargs="?"})
vim.api.nvim_create_user_command("Stacc", handler, {nargs="?"})