Skip to content

Instantly share code, notes, and snippets.

@Proful
Last active February 3, 2025 01:55
Show Gist options
  • Save Proful/359af29cb0f65e131f3f115d2b91cc52 to your computer and use it in GitHub Desktop.
Save Proful/359af29cb0f65e131f3f115d2b91cc52 to your computer and use it in GitHub Desktop.
---@diagnostic disable: missing-fields, inject-field
---@type wk.Plugin
local M = {}
M.name = "jumplist"
M.mappings = {
icon = { icon = "󰸕 ", color = "orange" },
plugin = "jumplist",
{ "~", desc = "jumplist" },
}
M.cols = {
{ key = "lnum", hl = "Number", align = "right" },
-- { key = "file", hl = "Directory" },
{ key = "value", hl = "Comment" },
}
function convert(i)
if i >= 0 and i <= 9 then
return tostring(i) -- Convert numbers 0-9 to their string representation
elseif i >= 10 and i <= 37 then
return string.char(97 + (i - 10)) -- Convert 10-37 to 'a'-'z'
else
return nil -- Handle invalid input
end
end
function M.expand()
local items = {} ---@type wk.Plugin.item[]
local output = vim.fn.execute("jumps")
local lines = vim.split(output, "\n")
-- Reverse the lines
local reversed_lines = {}
for i = #lines, 1, -1 do
local line = lines[i]
-- Skip empty lines and the header line
if line ~= "" and not line:match("^jump%s+line%s+col%s+file/text") then
table.insert(reversed_lines, line)
end
end
local x = 0
local y = 0
for i, line in ipairs(reversed_lines) do
-- Skip the header line
if i > 0 then
-- Extract jump index, line number, column, and file path/text
local jump, line_num, col, file = string.match(line, "(%d+)%s+(%d+)%s+(%d+)%s*(.*)")
if jump and line_num and col then
local expanded_file = ""
-- Ensure file is a valid string before expanding
if file and file ~= "" then
local success, result = pcall(vim.fn.expand, file)
expanded_file = success and result or file -- Fallback to original if expand fails
else
expanded_file = vim.api.nvim_buf_get_name(0) -- Use current buffer name if empty
end
local file_name = vim.fn.fnamemodify(expanded_file, ":t") -- Extract filename
local file_text = ""
-- Check if 'expanded_file' is a real file
if vim.fn.filereadable(expanded_file) == 1 then
local file_lines = vim.fn.readfile(expanded_file)
local line_idx = tonumber(line_num)
if file_lines and line_idx > 0 and line_idx <= #file_lines then
file_text = file_lines[line_idx]
end
else
expanded_file = vim.api.nvim_buf_get_name(0) -- Use current buffer name if empty
file_name = vim.fn.fnamemodify(expanded_file, ":t") -- Extract filename
file_text = file
x = x + 1
if x < 36 then
y = convert(x)
table.insert(items, {
key = tostring(y),
desc = "",
value = vim.trim(file_text),
highlights = {
{ 1, #jump, "Number" },
{ #jump + 1, #jump + #col + 1, "Comment" },
},
jump = tonumber(jump),
col = tonumber(col),
file = file_name,
-- file = vim.fn.fnamemodify(file, ":p:~:."),
lnum = tonumber(line_num),
x = x,
y = y,
action = function()
vim.cmd("norm! " .. line_num .. "G")
end,
})
end
end
-- Print jump information
-- print(
-- string.format(
-- "Jump: %s, Line: %s, Column: %s, File: %s (Filename: %s) %s",
-- jump,
-- line_num,
-- col,
-- expanded_file,
-- file_name,
-- file_text ~= "" and ("-> " .. file_text) or ""
-- )
-- )
end
end
end
-- Sort jumps in descending order
-- table.sort(items, function(a, b)
-- return a.x > b.x
-- end)
return items
end
return M
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment