Add stacc drop command

This commit is contained in:
Talia 2024-10-29 12:08:25 +01:00
parent f88e714758
commit 7915594904
2 changed files with 29 additions and 3 deletions

View file

@ -29,6 +29,29 @@ function stacc:push(description)
table.insert(self.locations, {bufnr, id, description, vim.fn.line(".")}) table.insert(self.locations, {bufnr, id, description, vim.fn.line(".")})
end end
--- Silently pops/drops a given number of items from the task stack
--- @param count number How many items to drop from the stack
--- @return string description The description of the current task
--- @return number bufnr Buffer number of the stack entry
--- @return number row Line number (1-indexed)
--- @return number col Column number (1-indexed)
function stacc:drop(count)
if count > #self.locations then
error("stack only has "..#self.locations.." elements.")
end
local bufnr, description
local final = vim.api.nvim_win_get_cursor(0)
for _=1, count do
local id
bufnr, id, description = unpack(table.remove(self.locations))
final = vim.api.nvim_buf_get_extmark_by_id(bufnr, ns, id, {})
vim.api.nvim_buf_del_extmark(bufnr, ns, id)
final[1] = final[1] + 1 -- Make line 1-indexed
final[2] = final[2] + 1 -- Make column 1-indexed
end
return description, bufnr, final[1], final[2]
end
--- Pops a given number of items from the task stack --- Pops a given number of items from the task stack
--- Jumps to the last removed item and displays its description --- Jumps to the last removed item and displays its description
--- @param count number How many items to pop from the stack --- @param count number How many items to pop from the stack
@ -54,14 +77,15 @@ function stacc:pop(count)
vim.api.nvim_set_current_buf(final.bufnr or 0) vim.api.nvim_set_current_buf(final.bufnr or 0)
final.bufnr = nil final.bufnr = nil
vim.api.nvim_win_set_cursor(0, final) vim.api.nvim_win_set_cursor(0, final)
return description, bufnr, unpack(final) return description, bufnr, final[1], final[2]
end end
--- Clears the entire stack --- Clears the entire stack
function stacc:clear() function stacc:clear()
stacc.pop(#self.locations) self:pop(#self.locations)
end end
--- @return Stacc new
function stacc:new(new) function stacc:new(new)
new = new or {} new = new or {}
new.locations = new.locations or {} new.locations = new.locations or {}

View file

@ -35,6 +35,7 @@ local handler = function(params)
local count = #stacc.locations local count = #stacc.locations
if count == 0 then if count == 0 then
print("Stacc is empty. Push with `:"..command.." description of current task`") print("Stacc is empty. Push with `:"..command.." description of current task`")
print("Manipulate with :"..command.." pop|drop|clear")
else else
for i, location in ipairs(stacc.locations) do for i, location in ipairs(stacc.locations) do
local bufnr, id, description = unpack(location) local bufnr, id, description = unpack(location)
@ -47,8 +48,9 @@ local handler = function(params)
description description
)) ))
end end
print(":"..command.." pop|clear")
end end
elseif params.args == "drop" then
stacc:drop(1)
elseif params.args == "clear" then elseif params.args == "clear" then
stacc:clear() stacc:clear()
elseif params.args == "pop" then elseif params.args == "pop" then