mirror of
https://github.com/ThePrimeagen/harpoon.git
synced 2025-07-13 17:40:25 +00:00
Refactor store_offset to avoid JSON reads. Read marks once and pass them through as a variable
This commit is contained in:
parent
b220d3056b
commit
61b24a7ce9
@ -136,7 +136,7 @@ local function filter_filetype()
|
|||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
function M.get_index_of(item)
|
function M.get_index_of(item, marks)
|
||||||
log.trace("get_index_of():", item)
|
log.trace("get_index_of():", item)
|
||||||
if item == nil then
|
if item == nil then
|
||||||
log.error(
|
log.error(
|
||||||
@ -150,8 +150,10 @@ function M.get_index_of(item)
|
|||||||
|
|
||||||
if type(item) == "string" then
|
if type(item) == "string" then
|
||||||
local relative_item = utils.normalize_path(item)
|
local relative_item = utils.normalize_path(item)
|
||||||
local marks = harpoon.get_mark_config().marks
|
if marks == nil then
|
||||||
for idx = 1, M.get_length() do
|
marks = harpoon.get_mark_config().marks
|
||||||
|
end
|
||||||
|
for idx = 1, M.get_length(marks) do
|
||||||
if marks[idx] and marks[idx].filename == relative_item then
|
if marks[idx] and marks[idx].filename == relative_item then
|
||||||
return idx
|
return idx
|
||||||
end
|
end
|
||||||
@ -191,13 +193,13 @@ function M.status(bufnr)
|
|||||||
return ""
|
return ""
|
||||||
end
|
end
|
||||||
|
|
||||||
function M.valid_index(idx)
|
function M.valid_index(idx, marks)
|
||||||
log.trace("valid_index():", idx)
|
log.trace("valid_index():", idx)
|
||||||
if idx == nil then
|
if idx == nil then
|
||||||
return false
|
return false
|
||||||
end
|
end
|
||||||
|
|
||||||
local file_name = M.get_marked_file_name(idx)
|
local file_name = M.get_marked_file_name(idx, marks)
|
||||||
return file_name ~= nil and file_name ~= ""
|
return file_name ~= nil and file_name ~= ""
|
||||||
end
|
end
|
||||||
|
|
||||||
@ -246,9 +248,10 @@ end
|
|||||||
function M.store_offset()
|
function M.store_offset()
|
||||||
log.trace("store_offset()")
|
log.trace("store_offset()")
|
||||||
local ok, res = pcall(function()
|
local ok, res = pcall(function()
|
||||||
|
local marks = harpoon.get_mark_config().marks
|
||||||
local buf_name = get_buf_name()
|
local buf_name = get_buf_name()
|
||||||
local idx = M.get_index_of(buf_name)
|
local idx = M.get_index_of(buf_name, marks)
|
||||||
if not M.valid_index(idx) then
|
if not M.valid_index(idx, marks) then
|
||||||
return
|
return
|
||||||
end
|
end
|
||||||
|
|
||||||
@ -260,8 +263,8 @@ function M.store_offset()
|
|||||||
cursor_pos[2]
|
cursor_pos[2]
|
||||||
)
|
)
|
||||||
)
|
)
|
||||||
harpoon.get_mark_config().marks[idx].row = cursor_pos[1]
|
marks[idx].row = cursor_pos[1]
|
||||||
harpoon.get_mark_config().marks[idx].col = cursor_pos[2]
|
marks[idx].col = cursor_pos[2]
|
||||||
end)
|
end)
|
||||||
|
|
||||||
if not ok then
|
if not ok then
|
||||||
@ -301,15 +304,23 @@ function M.get_marked_file(idxOrName)
|
|||||||
return harpoon.get_mark_config().marks[idxOrName]
|
return harpoon.get_mark_config().marks[idxOrName]
|
||||||
end
|
end
|
||||||
|
|
||||||
function M.get_marked_file_name(idx)
|
function M.get_marked_file_name(idx, marks)
|
||||||
local mark = harpoon.get_mark_config().marks[idx]
|
local mark
|
||||||
|
if marks ~= nil then
|
||||||
|
mark = marks[idx]
|
||||||
|
else
|
||||||
|
mark = harpoon.get_mark_config().marks[idx]
|
||||||
|
end
|
||||||
log.trace("get_marked_file_name():", mark and mark.filename)
|
log.trace("get_marked_file_name():", mark and mark.filename)
|
||||||
return mark and mark.filename
|
return mark and mark.filename
|
||||||
end
|
end
|
||||||
|
|
||||||
function M.get_length()
|
function M.get_length(marks)
|
||||||
|
if marks == nil then
|
||||||
|
marks = harpoon.get_mark_config().marks
|
||||||
|
end
|
||||||
log.trace("get_length()")
|
log.trace("get_length()")
|
||||||
return table.maxn(harpoon.get_mark_config().marks)
|
return table.maxn(marks)
|
||||||
end
|
end
|
||||||
|
|
||||||
function M.set_current_at(idx)
|
function M.set_current_at(idx)
|
||||||
|
Loading…
x
Reference in New Issue
Block a user