mirror of
https://github.com/ThePrimeagen/harpoon.git
synced 2025-07-16 11:00:23 +00:00
feat(mark)!: add set_current_at
BREAKING CHANGE: - filter_nulls(list) renamed to filter_empty_string(list) - swap(a, b) removed - trim() removed - promote() removed - promote_to_front() removed - remove_nils() removed - shorten_list() removed Fix #10
This commit is contained in:
parent
af09f2c99b
commit
91ffa8ac68
@ -1,45 +1,12 @@
|
|||||||
local Path = require('plenary.path')
|
|
||||||
local harpoon = require('harpoon')
|
local harpoon = require('harpoon')
|
||||||
local utils = require('harpoon.utils')
|
local utils = require('harpoon.utils')
|
||||||
|
|
||||||
local M = {}
|
local M = {}
|
||||||
|
|
||||||
local function valid_index(idx)
|
local function filter_empty_string(list)
|
||||||
return idx ~= nil and harpoon.get_mark_config().marks[idx] ~= nil
|
|
||||||
end
|
|
||||||
|
|
||||||
local function get_index_of(item)
|
|
||||||
if item == nil then
|
|
||||||
error("You have provided a nil value to Harpoon, please provide a string rep of the file or the file idx.")
|
|
||||||
return
|
|
||||||
end
|
|
||||||
local config = harpoon.get_mark_config()
|
|
||||||
if type(item) == 'string' then
|
|
||||||
local relative_item = utils.normalize_path(item)
|
|
||||||
for idx = 1, #config.marks do
|
|
||||||
if config.marks[idx] == relative_item then
|
|
||||||
return idx
|
|
||||||
end
|
|
||||||
end
|
|
||||||
|
|
||||||
return nil
|
|
||||||
end
|
|
||||||
|
|
||||||
if vim.g.manage_a_mark_zero_index then
|
|
||||||
item = item + 1
|
|
||||||
end
|
|
||||||
|
|
||||||
if item <= #config.marks and item >= 1 then
|
|
||||||
return item
|
|
||||||
end
|
|
||||||
|
|
||||||
return nil
|
|
||||||
end
|
|
||||||
|
|
||||||
local function filter_nulls(list)
|
|
||||||
local next = {}
|
local next = {}
|
||||||
for idx = 1, #list do
|
for idx = 1, #list do
|
||||||
if list[idx] ~= nil then
|
if list[idx] ~= "" then
|
||||||
table.insert(next, list[idx])
|
table.insert(next, list[idx])
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
@ -54,8 +21,8 @@ local function get_buf_name(id)
|
|||||||
return utils.normalize_path(id)
|
return utils.normalize_path(id)
|
||||||
end
|
end
|
||||||
|
|
||||||
local idx = get_index_of(id)
|
local idx = M.get_index_of(id)
|
||||||
if valid_index(idx) then
|
if M.valid_index(idx) then
|
||||||
return harpoon.get_mark_config().marks[idx]
|
return harpoon.get_mark_config().marks[idx]
|
||||||
end
|
end
|
||||||
--
|
--
|
||||||
@ -64,20 +31,44 @@ local function get_buf_name(id)
|
|||||||
return ""
|
return ""
|
||||||
end
|
end
|
||||||
|
|
||||||
M.get_index_of = get_index_of
|
M.get_index_of = function(item)
|
||||||
M.valid_index = valid_index
|
if item == nil then
|
||||||
|
error("You have provided a nil value to Harpoon, please provide a string rep of the file or the file idx.")
|
||||||
|
return
|
||||||
|
end
|
||||||
|
|
||||||
local function swap(a_idx, b_idx)
|
|
||||||
local config = harpoon.get_mark_config()
|
local config = harpoon.get_mark_config()
|
||||||
local tmp = config.marks[a_idx]
|
if type(item) == 'string' then
|
||||||
config.marks[a_idx] = config.marks[b_idx]
|
local relative_item = utils.normalize_path(item)
|
||||||
config.marks[b_idx] = tmp
|
for idx = 1, M.get_length() do
|
||||||
|
if config.marks[idx] == relative_item then
|
||||||
|
return idx
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
return nil
|
||||||
|
end
|
||||||
|
|
||||||
|
if vim.g.manage_a_mark_zero_index then
|
||||||
|
item = item + 1
|
||||||
|
end
|
||||||
|
|
||||||
|
if item <= M.get_length() and item >= 1 then
|
||||||
|
return item
|
||||||
|
end
|
||||||
|
|
||||||
|
return nil
|
||||||
|
end
|
||||||
|
|
||||||
|
M.valid_index = function(idx)
|
||||||
|
local config = harpoon.get_mark_config()
|
||||||
|
return idx ~= nil and config.marks[idx] ~= nil and config.marks[idx] ~= ""
|
||||||
end
|
end
|
||||||
|
|
||||||
M.add_file = function(file_name_or_buf_id)
|
M.add_file = function(file_name_or_buf_id)
|
||||||
local buf_name = get_buf_name(file_name_or_buf_id)
|
local buf_name = get_buf_name(file_name_or_buf_id)
|
||||||
|
|
||||||
if valid_index(get_index_of(buf_name)) then
|
if M.valid_index(M.get_index_of(buf_name)) then
|
||||||
-- we don't alter file layout.
|
-- we don't alter file layout.
|
||||||
return
|
return
|
||||||
end
|
end
|
||||||
@ -88,115 +79,88 @@ M.add_file = function(file_name_or_buf_id)
|
|||||||
end
|
end
|
||||||
|
|
||||||
local config = harpoon.get_mark_config()
|
local config = harpoon.get_mark_config()
|
||||||
for idx = 1, #config.marks do
|
for idx = 1, M.get_length() do
|
||||||
if config.marks[idx] == nil then
|
if config.marks[idx] == "" then
|
||||||
config.marks[idx] = buf_name
|
config.marks[idx] = buf_name
|
||||||
|
M.remove_empty_tail()
|
||||||
return
|
return
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
table.insert(config.marks, buf_name)
|
table.insert(config.marks, buf_name)
|
||||||
|
M.remove_empty_tail()
|
||||||
|
end
|
||||||
|
|
||||||
|
M.remove_empty_tail = function()
|
||||||
|
local config = harpoon.get_mark_config()
|
||||||
|
|
||||||
|
for i = M.get_length(), 1, -1 do
|
||||||
|
if config.marks[i] ~= "" then
|
||||||
|
return
|
||||||
|
end
|
||||||
|
|
||||||
|
if config.marks[i] == "" then
|
||||||
|
table.remove(config.marks, i)
|
||||||
|
end
|
||||||
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
M.store_offset = function()
|
M.store_offset = function()
|
||||||
local buf_name = get_buf_name()
|
local buf_name = get_buf_name()
|
||||||
local idx = get_index_of(buf_name)
|
local idx = M.get_index_of(buf_name)
|
||||||
if not valid_index(idx) then
|
if not M.valid_index(idx) then
|
||||||
return
|
return
|
||||||
end
|
end
|
||||||
|
|
||||||
local line = vim.api.nvim_eval("line('.')");
|
local line = vim.api.nvim_eval("line('.')");
|
||||||
end
|
end
|
||||||
|
|
||||||
M.swap = function(a, b)
|
|
||||||
local a_idx = get_index_of(a)
|
|
||||||
local b_idx = get_index_of(get_buf_name(b))
|
|
||||||
|
|
||||||
if not valid_index(a_idx) or not valid_index(b_idx) then
|
|
||||||
return
|
|
||||||
end
|
|
||||||
|
|
||||||
swap(a_idx, b_idx)
|
|
||||||
end
|
|
||||||
|
|
||||||
M.rm_file = function()
|
M.rm_file = function()
|
||||||
local buf_name = get_buf_name()
|
local buf_name = get_buf_name()
|
||||||
local idx = get_index_of(buf_name)
|
local idx = M.get_index_of(buf_name)
|
||||||
|
|
||||||
if not valid_index(idx) then
|
if not M.valid_index(idx) then
|
||||||
return
|
return
|
||||||
end
|
end
|
||||||
|
|
||||||
harpoon.get_mark_config().marks[idx] = nil
|
harpoon.get_mark_config().marks[idx] = ""
|
||||||
end
|
M.remove_empty_tail()
|
||||||
|
|
||||||
M.trim = function()
|
|
||||||
M.shorten_list(idx)
|
|
||||||
end
|
end
|
||||||
|
|
||||||
M.clear_all = function()
|
M.clear_all = function()
|
||||||
harpoon.get_mark_config().marks = {}
|
harpoon.get_mark_config().marks = {}
|
||||||
end
|
end
|
||||||
|
|
||||||
M.promote = function(id)
|
|
||||||
local buf_name = get_buf_name(id)
|
|
||||||
local idx = get_index_of(buf_name)
|
|
||||||
|
|
||||||
if not valid_index(idx) or idx == 1 then
|
|
||||||
return
|
|
||||||
end
|
|
||||||
|
|
||||||
swap(idx - 1, idx)
|
|
||||||
end
|
|
||||||
|
|
||||||
M.promote_to_front = function(id)
|
|
||||||
local buf_name = get_buf_name(id)
|
|
||||||
local idx = get_index_of(buf_name)
|
|
||||||
|
|
||||||
if not valid_index(idx) or idx == 1 then
|
|
||||||
return
|
|
||||||
end
|
|
||||||
|
|
||||||
swap(1, idx)
|
|
||||||
end
|
|
||||||
|
|
||||||
M.remove_nils = function()
|
|
||||||
local config = harpoon.get_mark_config()
|
|
||||||
config.marks = filter_nulls(config.marks)
|
|
||||||
end
|
|
||||||
|
|
||||||
M.shorten_list = function(count)
|
|
||||||
if not count then
|
|
||||||
local buf_name = get_buf_name()
|
|
||||||
local idx = get_index_of(buf_name)
|
|
||||||
|
|
||||||
if not valid_index(idx) then
|
|
||||||
return
|
|
||||||
end
|
|
||||||
|
|
||||||
count = idx
|
|
||||||
end
|
|
||||||
|
|
||||||
local next = {}
|
|
||||||
local config = harpoon.get_mark_config()
|
|
||||||
local up_to = math.min(count, #config.marks)
|
|
||||||
for idx = 1, up_to do
|
|
||||||
table.insert(next, config.marks[idx])
|
|
||||||
end
|
|
||||||
config.marks = next
|
|
||||||
end
|
|
||||||
|
|
||||||
M.get_marked_file = function(idx)
|
M.get_marked_file = function(idx)
|
||||||
return harpoon.get_mark_config().marks[idx]
|
return harpoon.get_mark_config().marks[idx]
|
||||||
end
|
end
|
||||||
|
|
||||||
M.get_length = function()
|
M.get_length = function()
|
||||||
return #harpoon.get_mark_config().marks
|
return table.maxn(harpoon.get_mark_config().marks)
|
||||||
|
end
|
||||||
|
|
||||||
|
M.set_current_at = function(idx)
|
||||||
|
local config = harpoon.get_mark_config()
|
||||||
|
local buf_name = get_buf_name()
|
||||||
|
local current_idx = M.get_index_of(buf_name)
|
||||||
|
|
||||||
|
-- Remove it if it already exists
|
||||||
|
if M.valid_index(current_idx) then
|
||||||
|
config.marks[current_idx] = ""
|
||||||
|
end
|
||||||
|
|
||||||
|
config.marks[idx] = buf_name
|
||||||
|
|
||||||
|
for i = 1, M.get_length() do
|
||||||
|
if not config.marks[i] then
|
||||||
|
config.marks[i] = ""
|
||||||
|
end
|
||||||
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
M.to_quickfix_list = function()
|
M.to_quickfix_list = function()
|
||||||
local config = harpoon.get_mark_config()
|
local config = harpoon.get_mark_config()
|
||||||
local file_list = filter_nulls(config.marks)
|
local file_list = filter_empty_string(config.marks)
|
||||||
local qf_list = {}
|
local qf_list = {}
|
||||||
for idx = 1, #file_list do
|
for idx = 1, #file_list do
|
||||||
qf_list[idx] = {
|
qf_list[idx] = {
|
||||||
|
@ -58,7 +58,11 @@ M.toggle_quick_menu = function()
|
|||||||
bufh = win_info.bufh
|
bufh = win_info.bufh
|
||||||
|
|
||||||
for idx = 1, Marked.get_length() do
|
for idx = 1, Marked.get_length() do
|
||||||
contents[idx] = string.format("%d %s", idx, Marked.get_marked_file(idx))
|
local file = Marked.get_marked_file(idx)
|
||||||
|
if file == "" then
|
||||||
|
file = "(empty)"
|
||||||
|
end
|
||||||
|
contents[idx] = string.format("%d %s", idx, file)
|
||||||
end
|
end
|
||||||
|
|
||||||
vim.api.nvim_buf_set_lines(bufh, 0, #contents, false, contents)
|
vim.api.nvim_buf_set_lines(bufh, 0, #contents, false, contents)
|
||||||
|
Loading…
x
Reference in New Issue
Block a user