mirror of
https://github.com/ThePrimeagen/harpoon.git
synced 2025-07-14 01:50:27 +00:00
Merge pull request #34 from asbjornhaland/set_current_at
feat(mark)!: add set_current_at
This commit is contained in:
commit
61aff8c523
@ -1,45 +1,12 @@
|
||||
local Path = require('plenary.path')
|
||||
local harpoon = require('harpoon')
|
||||
local utils = require('harpoon.utils')
|
||||
|
||||
local M = {}
|
||||
|
||||
local function valid_index(idx)
|
||||
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 function filter_empty_string(list)
|
||||
local next = {}
|
||||
for idx = 1, #list do
|
||||
if list[idx] ~= nil then
|
||||
if list[idx] ~= "" then
|
||||
table.insert(next, list[idx])
|
||||
end
|
||||
end
|
||||
@ -54,8 +21,8 @@ local function get_buf_name(id)
|
||||
return utils.normalize_path(id)
|
||||
end
|
||||
|
||||
local idx = get_index_of(id)
|
||||
if valid_index(idx) then
|
||||
local idx = M.get_index_of(id)
|
||||
if M.valid_index(idx) then
|
||||
return harpoon.get_mark_config().marks[idx]
|
||||
end
|
||||
--
|
||||
@ -64,20 +31,44 @@ local function get_buf_name(id)
|
||||
return ""
|
||||
end
|
||||
|
||||
M.get_index_of = get_index_of
|
||||
M.valid_index = valid_index
|
||||
M.get_index_of = function(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 function swap(a_idx, b_idx)
|
||||
local config = harpoon.get_mark_config()
|
||||
local tmp = config.marks[a_idx]
|
||||
config.marks[a_idx] = config.marks[b_idx]
|
||||
config.marks[b_idx] = tmp
|
||||
if type(item) == 'string' then
|
||||
local relative_item = utils.normalize_path(item)
|
||||
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
|
||||
|
||||
M.add_file = function(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.
|
||||
return
|
||||
end
|
||||
@ -88,115 +79,88 @@ M.add_file = function(file_name_or_buf_id)
|
||||
end
|
||||
|
||||
local config = harpoon.get_mark_config()
|
||||
for idx = 1, #config.marks do
|
||||
if config.marks[idx] == nil then
|
||||
for idx = 1, M.get_length() do
|
||||
if config.marks[idx] == "" then
|
||||
config.marks[idx] = buf_name
|
||||
M.remove_empty_tail()
|
||||
return
|
||||
end
|
||||
end
|
||||
|
||||
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
|
||||
|
||||
M.store_offset = function()
|
||||
local buf_name = get_buf_name()
|
||||
local idx = get_index_of(buf_name)
|
||||
if not valid_index(idx) then
|
||||
local idx = M.get_index_of(buf_name)
|
||||
if not M.valid_index(idx) then
|
||||
return
|
||||
end
|
||||
|
||||
local line = vim.api.nvim_eval("line('.')");
|
||||
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()
|
||||
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
|
||||
end
|
||||
|
||||
harpoon.get_mark_config().marks[idx] = nil
|
||||
end
|
||||
|
||||
M.trim = function()
|
||||
M.shorten_list(idx)
|
||||
harpoon.get_mark_config().marks[idx] = ""
|
||||
M.remove_empty_tail()
|
||||
end
|
||||
|
||||
M.clear_all = function()
|
||||
harpoon.get_mark_config().marks = {}
|
||||
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)
|
||||
return harpoon.get_mark_config().marks[idx]
|
||||
end
|
||||
|
||||
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
|
||||
|
||||
M.to_quickfix_list = function()
|
||||
local config = harpoon.get_mark_config()
|
||||
local file_list = filter_nulls(config.marks)
|
||||
local file_list = filter_empty_string(config.marks)
|
||||
local qf_list = {}
|
||||
for idx = 1, #file_list do
|
||||
qf_list[idx] = {
|
||||
|
@ -58,7 +58,11 @@ M.toggle_quick_menu = function()
|
||||
bufh = win_info.bufh
|
||||
|
||||
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
|
||||
|
||||
vim.api.nvim_buf_set_lines(bufh, 0, #contents, false, contents)
|
||||
|
Loading…
x
Reference in New Issue
Block a user