Improve nvim stacc command
This commit is contained in:
parent
41592c57d2
commit
c1d76da6ac
3 changed files with 95 additions and 49 deletions
|
@ -1,4 +1,7 @@
|
||||||
{
|
{
|
||||||
"runtime.version": "Lua 5.1",
|
"runtime.version": "Lua 5.1",
|
||||||
"diagnostics.globals": ["vim"]
|
"diagnostics.globals": ["vim"],
|
||||||
|
"workspace.library": [
|
||||||
|
"/home/darkwiiplayer/darkrc/vim/lua"
|
||||||
|
]
|
||||||
}
|
}
|
||||||
|
|
66
vim/lua/stacc.lua
Normal file
66
vim/lua/stacc.lua
Normal file
|
@ -0,0 +1,66 @@
|
||||||
|
-- A simple per-session task stack.
|
||||||
|
-- Allows pushing locations with a note and returning to them later.
|
||||||
|
|
||||||
|
--- @type number Namespace id for the plugin
|
||||||
|
local ns = vim.api.nvim_create_namespace("stacc")
|
||||||
|
|
||||||
|
local locations = {}
|
||||||
|
|
||||||
|
--- @class Location
|
||||||
|
--- @field [1] number Buffer Number
|
||||||
|
--- @field [2] number ExtMark ID
|
||||||
|
--- @field [3] string Description
|
||||||
|
--- @field [4] number Current line number
|
||||||
|
|
||||||
|
--- @class stacc
|
||||||
|
--- @field locations Location[] Array listing all the locations with newest ones having the highest indices
|
||||||
|
--- @field namespace number Namespace ID used for ExtMarks
|
||||||
|
local stacc = {
|
||||||
|
locations = locations;
|
||||||
|
namespace = ns;
|
||||||
|
}
|
||||||
|
|
||||||
|
--- Pushes a new location to the stack
|
||||||
|
--- @param description string A message to display when returning to this location
|
||||||
|
function stacc.push(description)
|
||||||
|
local bufnr = vim.fn.bufnr()
|
||||||
|
local line, column = unpack(vim.api.nvim_win_get_cursor(0))
|
||||||
|
line = line - 1 -- 0-based :help api-indexing
|
||||||
|
local id = vim.api.nvim_buf_set_extmark(bufnr, ns, line, column, {})
|
||||||
|
table.insert(locations, {bufnr, id, description, vim.fn.line(".")})
|
||||||
|
end
|
||||||
|
|
||||||
|
--- Pops a given number of items from the task stack
|
||||||
|
--- Jumps to the last removed item and displays its description
|
||||||
|
--- @param count number How many items to pop 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.pop(count)
|
||||||
|
if count > #locations then
|
||||||
|
error("stack only has "..#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(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
|
||||||
|
final.bufnr = bufnr
|
||||||
|
end
|
||||||
|
vim.api.nvim_set_current_buf(final.bufnr or 0)
|
||||||
|
final.bufnr = nil
|
||||||
|
vim.api.nvim_win_set_cursor(0, final)
|
||||||
|
return description, bufnr, unpack(final)
|
||||||
|
end
|
||||||
|
|
||||||
|
--- Clears the entire stack
|
||||||
|
function stacc.clear()
|
||||||
|
stacc.pop(#locations)
|
||||||
|
end
|
||||||
|
|
||||||
|
return stacc
|
|
@ -1,51 +1,28 @@
|
||||||
-- A simple per-session task stack.
|
local stacc = require "stacc"
|
||||||
-- Allows pushing locations with a note and returning to them later.
|
|
||||||
|
|
||||||
local ns = vim.api.nvim_create_namespace("stacc")
|
vim.api.nvim_create_user_command("Stacc", function(params)
|
||||||
|
if params.args == "" then
|
||||||
local locations = {}
|
local count = #stacc.locations
|
||||||
|
for i, location in ipairs(stacc.locations) do
|
||||||
local stacc = {}
|
local bufnr, id, description = unpack(location)
|
||||||
|
--- @type number[]
|
||||||
--- Pushes a new location to the stack
|
local mark = vim.api.nvim_buf_get_extmark_by_id(bufnr, stacc.namespace, id, {})
|
||||||
--- @param description string A message to display when returning to this location
|
print(string.format("%s %s:%i -- %s",
|
||||||
function stacc.push(description)
|
string.format("%" .. math.floor(math.log(count, 10)+1) .. "i", i),
|
||||||
local bufnr = vim.fn.bufnr()
|
vim.api.nvim_buf_get_name(bufnr):match("[^/]*$"),
|
||||||
local line, column = unpack(vim.api.nvim_win_get_cursor(0))
|
mark[1] + 1,
|
||||||
line = line - 1 -- 0-based :help api-indexing
|
description
|
||||||
local id = vim.api.nvim_buf_set_extmark(bufnr, ns, line, column, {})
|
))
|
||||||
table.insert(locations, {bufnr, id, description, vim.fn.line(".")})
|
end
|
||||||
end
|
elseif params.args == "clear" then
|
||||||
|
stacc.clear()
|
||||||
--- Pops a given number of items from the task stack
|
elseif params.args == "pop" then
|
||||||
--- Jumps to the last removed item and displays its description
|
local message, bufnr = stacc.pop(1)
|
||||||
--- @param count number How many items to pop from the stack
|
print(string.format("[%s] %s",
|
||||||
--- @return string description The description of the current task
|
vim.api.nvim_buf_get_name(bufnr):match("[^/]*$"),
|
||||||
function stacc.pop(count)
|
message
|
||||||
if count > #locations then
|
))
|
||||||
error("stack only has "..#locations.." elements.")
|
else
|
||||||
|
stacc.push(params.args)
|
||||||
end
|
end
|
||||||
local description
|
|
||||||
local final = vim.api.nvim_win_get_cursor(0)
|
|
||||||
for _=1, count do
|
|
||||||
local bufnr, id
|
|
||||||
bufnr, id, description = unpack(table.remove(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.bufnr = bufnr
|
|
||||||
end
|
|
||||||
vim.api.nvim_set_current_buf(final.bufnr or 0)
|
|
||||||
final.bufnr = nil
|
|
||||||
vim.api.nvim_win_set_cursor(0, final)
|
|
||||||
return description
|
|
||||||
end
|
|
||||||
|
|
||||||
vim.api.nvim_create_user_command("StaccPush", function(params)
|
|
||||||
stacc.push(params.args)
|
|
||||||
end, {nargs="?"})
|
|
||||||
|
|
||||||
vim.api.nvim_create_user_command("StaccPop", function(params)
|
|
||||||
local message = stacc.pop(tonumber(params.args) or 1)
|
|
||||||
print(message)
|
|
||||||
end, {nargs="?"})
|
end, {nargs="?"})
|
||||||
|
|
Loading…
Reference in a new issue