mirror of
https://github.com/ThePrimeagen/harpoon.git
synced 2025-07-14 01:50:27 +00:00
Add further logging, fix callbacks
The callbacks are in a nested table, so they weren't actually called. If there aren't more events then callbacks for "changed" don't need to be nested in the callbacks table. Alternatively emitChanged() could take an event name: emit("changed") and run callbacks for a specific event or take a table of events and run for all, emit({"changed", "navigate"}).
This commit is contained in:
parent
8a4c443145
commit
02f4e77111
@ -19,7 +19,7 @@ local function set_log_level()
|
|||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
return "warn" -- default, if user hasn't set
|
return "warn" -- default, if user hasn't set to one from log_levels
|
||||||
end
|
end
|
||||||
|
|
||||||
M.log = require("plenary.log").new({
|
M.log = require("plenary.log").new({
|
||||||
|
@ -127,13 +127,14 @@ M.setup = function(config)
|
|||||||
end
|
end
|
||||||
|
|
||||||
local ok, u_config = pcall(read_config, user_config)
|
local ok, u_config = pcall(read_config, user_config)
|
||||||
local ok2, c_config = pcall(read_config, cache_config)
|
|
||||||
|
|
||||||
if not ok then
|
if not ok then
|
||||||
log.debug("setup(): No user config present at", user_config)
|
log.debug("setup(): No user config present at", user_config)
|
||||||
u_config = {}
|
u_config = {}
|
||||||
end
|
end
|
||||||
|
|
||||||
|
local ok2, c_config = pcall(read_config, cache_config)
|
||||||
|
|
||||||
if not ok2 then
|
if not ok2 then
|
||||||
log.debug("setup(): No cache config present at", cache_config)
|
log.debug("setup(): No cache config present at", cache_config)
|
||||||
c_config = {}
|
c_config = {}
|
||||||
@ -156,22 +157,22 @@ M.setup = function(config)
|
|||||||
end
|
end
|
||||||
|
|
||||||
M.get_global_settings = function()
|
M.get_global_settings = function()
|
||||||
log.trace("get_global_settings()")
|
log.debug("get_global_settings()")
|
||||||
return HarpoonConfig.global_settings
|
return HarpoonConfig.global_settings
|
||||||
end
|
end
|
||||||
|
|
||||||
M.get_term_config = function()
|
M.get_term_config = function()
|
||||||
log.trace("get_term_config()")
|
log.debug("get_term_config()")
|
||||||
return ensure_correct_config(HarpoonConfig).projects[vim.loop.cwd()].term
|
return ensure_correct_config(HarpoonConfig).projects[vim.loop.cwd()].term
|
||||||
end
|
end
|
||||||
|
|
||||||
M.get_mark_config = function()
|
M.get_mark_config = function()
|
||||||
-- log.trace("get_mark_config()")
|
log.debug("get_mark_config()")
|
||||||
return ensure_correct_config(HarpoonConfig).projects[vim.loop.cwd()].mark
|
return ensure_correct_config(HarpoonConfig).projects[vim.loop.cwd()].mark
|
||||||
end
|
end
|
||||||
|
|
||||||
M.get_menu_config = function()
|
M.get_menu_config = function()
|
||||||
log.trace("get_menu_config()")
|
log.debug("get_menu_config()")
|
||||||
return HarpoonConfig.menu or {}
|
return HarpoonConfig.menu or {}
|
||||||
end
|
end
|
||||||
|
|
||||||
|
@ -19,7 +19,8 @@ local function emit_changed()
|
|||||||
return
|
return
|
||||||
end
|
end
|
||||||
|
|
||||||
for _, cb in pairs(callbacks) do
|
for idx, cb in pairs(callbacks["changed"]) do
|
||||||
|
log.debug(string.format("emit_changed(): Running callback #%d for 'changed'", idx))
|
||||||
cb()
|
cb()
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
@ -36,6 +37,7 @@ local function filter_empty_string(list)
|
|||||||
end
|
end
|
||||||
|
|
||||||
local function get_first_empty_slot()
|
local function get_first_empty_slot()
|
||||||
|
log.debug("_get_first_empty_slot()")
|
||||||
for idx = 1, M.get_length() do
|
for idx = 1, M.get_length() do
|
||||||
local filename = M.get_marked_file_name(idx)
|
local filename = M.get_marked_file_name(idx)
|
||||||
if filename == "" then
|
if filename == "" then
|
||||||
@ -47,6 +49,7 @@ local function get_first_empty_slot()
|
|||||||
end
|
end
|
||||||
|
|
||||||
local function get_buf_name(id)
|
local function get_buf_name(id)
|
||||||
|
log.debug("get_buf_name():", id)
|
||||||
if id == nil then
|
if id == nil then
|
||||||
return utils.normalize_path(vim.fn.bufname(vim.fn.bufnr()))
|
return utils.normalize_path(vim.fn.bufname(vim.fn.bufnr()))
|
||||||
elseif type(id) == "string" then
|
elseif type(id) == "string" then
|
||||||
@ -72,25 +75,29 @@ local function create_mark(filename)
|
|||||||
end
|
end
|
||||||
|
|
||||||
local function mark_exists(buf_name)
|
local function mark_exists(buf_name)
|
||||||
|
log.debug("_mark_exists()")
|
||||||
for idx = 1, M.get_length() do
|
for idx = 1, M.get_length() do
|
||||||
if M.get_marked_file_name(idx) == buf_name then
|
if M.get_marked_file_name(idx) == buf_name then
|
||||||
|
log.trace("_mark_exists(): Mark exists", buf_name)
|
||||||
return true
|
return true
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
|
log.trace("_mark_exists(): Mark doesn't exist", buf_name)
|
||||||
return false
|
return false
|
||||||
end
|
end
|
||||||
|
|
||||||
local function validate_buf_name(buf_name)
|
local function validate_buf_name(buf_name)
|
||||||
if buf_name == "" or buf_name == nil then
|
if buf_name == "" or buf_name == nil then
|
||||||
log.error("Couldn't find a valid file name to mark, sorry.")
|
log.error("validate_buf_name(): Not a valid name for a mark,", buf_name)
|
||||||
return
|
return
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
M.get_index_of = function(item)
|
M.get_index_of = function(item)
|
||||||
|
log.debug("get_index_of():", item)
|
||||||
if item == nil then
|
if item == nil then
|
||||||
log.error("You have provided a nil value to Harpoon, please provide a string rep of the file or the file idx.")
|
log.error("get_index_of(): You have provided a nil value to Harpoon, please provide a string rep of the file or the file idx.")
|
||||||
return
|
return
|
||||||
end
|
end
|
||||||
|
|
||||||
@ -105,6 +112,7 @@ M.get_index_of = function(item)
|
|||||||
return nil
|
return nil
|
||||||
end
|
end
|
||||||
|
|
||||||
|
-- TODO move this to a "harpoon_" prefix?
|
||||||
if vim.g.manage_a_mark_zero_index then
|
if vim.g.manage_a_mark_zero_index then
|
||||||
item = item + 1
|
item = item + 1
|
||||||
end
|
end
|
||||||
@ -113,10 +121,12 @@ M.get_index_of = function(item)
|
|||||||
return item
|
return item
|
||||||
end
|
end
|
||||||
|
|
||||||
|
log.debug("get_index_of(): No item found,", item)
|
||||||
return nil
|
return nil
|
||||||
end
|
end
|
||||||
|
|
||||||
M.status = function()
|
M.status = function()
|
||||||
|
log.debug("status()")
|
||||||
local idx = M.get_index_of(get_buf_name())
|
local idx = M.get_index_of(get_buf_name())
|
||||||
|
|
||||||
if M.valid_index(idx) then
|
if M.valid_index(idx) then
|
||||||
@ -126,6 +136,7 @@ M.status = function()
|
|||||||
end
|
end
|
||||||
|
|
||||||
M.valid_index = function(idx)
|
M.valid_index = function(idx)
|
||||||
|
log.debug("valid_index():", idx)
|
||||||
if idx == nil then
|
if idx == nil then
|
||||||
return false
|
return false
|
||||||
end
|
end
|
||||||
@ -136,6 +147,7 @@ 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)
|
||||||
|
log.debug("add_file():", buf_name)
|
||||||
|
|
||||||
if M.valid_index(M.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.
|
||||||
@ -150,8 +162,9 @@ M.add_file = function(file_name_or_buf_id)
|
|||||||
emit_changed()
|
emit_changed()
|
||||||
end
|
end
|
||||||
|
|
||||||
-- dont_emit_on_changed should only be used internally
|
-- _emit_on_changed == false should only be used internally
|
||||||
M.remove_empty_tail = function(_emit_on_changed)
|
M.remove_empty_tail = function(_emit_on_changed)
|
||||||
|
log.debug("remove_empty_tail()")
|
||||||
_emit_on_changed = _emit_on_changed == nil or _emit_on_changed
|
_emit_on_changed = _emit_on_changed == nil or _emit_on_changed
|
||||||
local config = harpoon.get_mark_config()
|
local config = harpoon.get_mark_config()
|
||||||
local found = false
|
local found = false
|
||||||
@ -174,18 +187,22 @@ M.remove_empty_tail = function(_emit_on_changed)
|
|||||||
end
|
end
|
||||||
|
|
||||||
M.store_offset = function()
|
M.store_offset = function()
|
||||||
|
log.debug("store_offset()")
|
||||||
local ok, res = pcall(function()
|
local ok, res = pcall(function()
|
||||||
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)
|
||||||
|
|
||||||
if not M.valid_index(idx) then
|
if not M.valid_index(idx) then
|
||||||
return
|
return
|
||||||
end
|
end
|
||||||
|
|
||||||
harpoon.get_mark_config().marks[idx].row = vim.fn.line(".")
|
local line = vim.fn.line(".")
|
||||||
|
harpoon.get_mark_config().marks[idx].row = line
|
||||||
|
log.debug("store_offset(): Stored line:", line)
|
||||||
end)
|
end)
|
||||||
|
|
||||||
if not ok then
|
if not ok then
|
||||||
log.warn("store_offset(): pcall failed:", res)
|
log.warn("store_offset(): Could not store offset:", res)
|
||||||
end
|
end
|
||||||
|
|
||||||
emit_changed()
|
emit_changed()
|
||||||
@ -214,6 +231,7 @@ end
|
|||||||
|
|
||||||
--- ENTERPRISE PROGRAMMING
|
--- ENTERPRISE PROGRAMMING
|
||||||
M.get_marked_file = function(idxOrName)
|
M.get_marked_file = function(idxOrName)
|
||||||
|
log.debug("get_marked_file():", idxOrName)
|
||||||
if type(idxOrName) == "string" then
|
if type(idxOrName) == "string" then
|
||||||
idxOrName = M.get_index_of(idxOrName)
|
idxOrName = M.get_index_of(idxOrName)
|
||||||
end
|
end
|
||||||
@ -222,10 +240,12 @@ end
|
|||||||
|
|
||||||
M.get_marked_file_name = function(idx)
|
M.get_marked_file_name = function(idx)
|
||||||
local mark = harpoon.get_mark_config().marks[idx]
|
local mark = harpoon.get_mark_config().marks[idx]
|
||||||
|
log.debug("get_marked_file_name():", mark and mark.filename)
|
||||||
return mark and mark.filename
|
return mark and mark.filename
|
||||||
end
|
end
|
||||||
|
|
||||||
M.get_length = function()
|
M.get_length = function()
|
||||||
|
log.debug("get_length()")
|
||||||
return table.maxn(harpoon.get_mark_config().marks)
|
return table.maxn(harpoon.get_mark_config().marks)
|
||||||
end
|
end
|
||||||
|
|
||||||
@ -270,6 +290,9 @@ M.to_quickfix_list = function()
|
|||||||
end
|
end
|
||||||
|
|
||||||
M.set_mark_list = function(new_list)
|
M.set_mark_list = function(new_list)
|
||||||
|
log.debug("set_mark_list()")
|
||||||
|
log.trace("set_mark_list(): new_list", new_list)
|
||||||
|
|
||||||
local config = harpoon.get_mark_config()
|
local config = harpoon.get_mark_config()
|
||||||
|
|
||||||
for k, v in pairs(new_list) do
|
for k, v in pairs(new_list) do
|
||||||
@ -306,16 +329,19 @@ M.toggle_file = function(file_name_or_buf_id)
|
|||||||
end
|
end
|
||||||
|
|
||||||
M.get_current_index = function()
|
M.get_current_index = function()
|
||||||
|
log.debug("get_current_index()")
|
||||||
return M.get_index_of(vim.fn.bufname(vim.fn.bufnr()))
|
return M.get_index_of(vim.fn.bufname(vim.fn.bufnr()))
|
||||||
end
|
end
|
||||||
|
|
||||||
M.on = function(event, cb)
|
M.on = function(event, cb)
|
||||||
|
log.debug("on():", event)
|
||||||
if not callbacks[event] then
|
if not callbacks[event] then
|
||||||
log.debug("on(): no callbacks yet for", event)
|
log.debug("on(): no callbacks yet for", event)
|
||||||
callbacks[event] = {}
|
callbacks[event] = {}
|
||||||
end
|
end
|
||||||
|
|
||||||
table.insert(callbacks[event], cb)
|
table.insert(callbacks[event], cb)
|
||||||
|
log.trace(callbacks)
|
||||||
end
|
end
|
||||||
|
|
||||||
return M
|
return M
|
||||||
|
@ -9,15 +9,16 @@ Harpoon_win_id = nil
|
|||||||
Harpoon_bufh = nil
|
Harpoon_bufh = nil
|
||||||
|
|
||||||
local function create_window()
|
local function create_window()
|
||||||
|
log.debug("_create_window()")
|
||||||
local config = harpoon.get_menu_config()
|
local config = harpoon.get_menu_config()
|
||||||
local width = config.width or 60
|
local width = config.width or 60
|
||||||
local height = config.height or 10
|
local height = config.height or 10
|
||||||
local borderchars = config.borderchars or { '─', '│', '─', '│', '╭', '╮', '╯', '╰' }
|
local borderchars = config.borderchars or { "─", "│", "─", "│", "╭", "╮", "╯", "╰" }
|
||||||
local bufnr = vim.api.nvim_create_buf(false, false)
|
local bufnr = vim.api.nvim_create_buf(false, false)
|
||||||
|
|
||||||
local Harpoon_win_id, win = popup.create(bufnr, {
|
local Harpoon_win_id, win = popup.create(bufnr, {
|
||||||
title = 'Harpoon',
|
title = "Harpoon",
|
||||||
highlight = 'HarpoonWindow',
|
highlight = "HarpoonWindow",
|
||||||
line = math.floor(((vim.o.lines - height) / 2) - 1),
|
line = math.floor(((vim.o.lines - height) / 2) - 1),
|
||||||
col = math.floor((vim.o.columns - width) / 2),
|
col = math.floor((vim.o.columns - width) / 2),
|
||||||
minwidth = width,
|
minwidth = width,
|
||||||
@ -25,7 +26,7 @@ local function create_window()
|
|||||||
borderchars = borderchars,
|
borderchars = borderchars,
|
||||||
})
|
})
|
||||||
|
|
||||||
vim.api.nvim_win_set_option(win.border.win_id, 'winhl', 'Normal:HarpoonBorder')
|
vim.api.nvim_win_set_option(win.border.win_id, "winhl", "Normal:HarpoonBorder")
|
||||||
|
|
||||||
return {
|
return {
|
||||||
bufnr = bufnr,
|
bufnr = bufnr,
|
||||||
@ -38,7 +39,7 @@ local function get_menu_items()
|
|||||||
local indices = {}
|
local indices = {}
|
||||||
|
|
||||||
for idx = 1, #lines do
|
for idx = 1, #lines do
|
||||||
local space_location = string.find(lines[idx], ' ')
|
local space_location = string.find(lines[idx], " ")
|
||||||
|
|
||||||
if space_location ~= nil then
|
if space_location ~= nil then
|
||||||
table.insert(indices, string.sub(lines[idx], space_location + 1))
|
table.insert(indices, string.sub(lines[idx], space_location + 1))
|
||||||
@ -48,16 +49,13 @@ local function get_menu_items()
|
|||||||
return indices
|
return indices
|
||||||
end
|
end
|
||||||
|
|
||||||
local save_changes = function()
|
|
||||||
Marked.set_mark_list(get_menu_items())
|
|
||||||
end
|
|
||||||
|
|
||||||
M.toggle_quick_menu = function()
|
M.toggle_quick_menu = function()
|
||||||
|
log.debug("toggle_quick_menu()")
|
||||||
if Harpoon_win_id ~= nil and vim.api.nvim_win_is_valid(Harpoon_win_id) then
|
if Harpoon_win_id ~= nil and vim.api.nvim_win_is_valid(Harpoon_win_id) then
|
||||||
local global_config = harpoon.get_global_settings()
|
local global_config = harpoon.get_global_settings()
|
||||||
|
|
||||||
if global_config.save_on_toggle then
|
if global_config.save_on_toggle then
|
||||||
require('harpoon.ui').on_menu_save()
|
require("harpoon.ui").on_menu_save()
|
||||||
end
|
end
|
||||||
|
|
||||||
vim.api.nvim_win_close(Harpoon_win_id, true)
|
vim.api.nvim_win_close(Harpoon_win_id, true)
|
||||||
@ -92,10 +90,12 @@ M.toggle_quick_menu = function()
|
|||||||
end
|
end
|
||||||
|
|
||||||
M.on_menu_save = function()
|
M.on_menu_save = function()
|
||||||
save_changes()
|
log.debug("on_menu_save()")
|
||||||
|
Marked.set_mark_list(get_menu_items())
|
||||||
end
|
end
|
||||||
|
|
||||||
M.nav_file = function(id)
|
M.nav_file = function(id)
|
||||||
|
log.debug("nav_file(): Navigating to", id)
|
||||||
local idx = Marked.get_index_of(id)
|
local idx = Marked.get_index_of(id)
|
||||||
if not Marked.valid_index(idx) then
|
if not Marked.valid_index(idx) then
|
||||||
log.debug("nav_file(): No mark exists for id", id)
|
log.debug("nav_file(): No mark exists for id", id)
|
||||||
@ -108,20 +108,23 @@ M.nav_file = function(id)
|
|||||||
|
|
||||||
vim.api.nvim_set_current_buf(buf_id)
|
vim.api.nvim_set_current_buf(buf_id)
|
||||||
if set_row and mark.row then
|
if set_row and mark.row then
|
||||||
vim.cmd(string.format(":%d", mark.row))
|
local ok, err = pcall(vim.cmd, string.format(":%d", mark.row))
|
||||||
|
if not ok then
|
||||||
|
log.warn("nav_file(): Could not set row to", mark.row, err)
|
||||||
|
end
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
function M.location_window(options)
|
function M.location_window(options)
|
||||||
local default_options = {
|
local default_options = {
|
||||||
relative = 'editor',
|
relative = "editor",
|
||||||
style = 'minimal',
|
style = "minimal",
|
||||||
width = 30,
|
width = 30,
|
||||||
height = 15,
|
height = 15,
|
||||||
row = 2,
|
row = 2,
|
||||||
col = 2,
|
col = 2,
|
||||||
}
|
}
|
||||||
options = vim.tbl_extend('keep', options, default_options)
|
options = vim.tbl_extend("keep", options, default_options)
|
||||||
|
|
||||||
local bufnr = options.bufnr or vim.fn.nvim_create_buf(false, true)
|
local bufnr = options.bufnr or vim.fn.nvim_create_buf(false, true)
|
||||||
local win_id = vim.fn.nvim_open_win(bufnr, true, options)
|
local win_id = vim.fn.nvim_open_win(bufnr, true, options)
|
||||||
@ -142,15 +145,15 @@ function M.notification(text)
|
|||||||
width = 20,
|
width = 20,
|
||||||
height = 2,
|
height = 2,
|
||||||
row = 1,
|
row = 1,
|
||||||
col = win_width - 21
|
col = win_width - 21,
|
||||||
})
|
})
|
||||||
|
|
||||||
vim.api.nvim_buf_set_lines(info.bufnr, 0, 5, false, {"!!! Notification", text})
|
vim.api.nvim_buf_set_lines(info.bufnr, 0, 5, false, { "!!! Notification", text })
|
||||||
vim.api.nvim_set_current_win(prev_win)
|
vim.api.nvim_set_current_win(prev_win)
|
||||||
|
|
||||||
return {
|
return {
|
||||||
bufnr = info.bufnr,
|
bufnr = info.bufnr,
|
||||||
win_id = info.win_id
|
win_id = info.win_id,
|
||||||
}
|
}
|
||||||
end
|
end
|
||||||
|
|
||||||
@ -159,32 +162,34 @@ function M.close_notification(bufnr)
|
|||||||
end
|
end
|
||||||
|
|
||||||
M.nav_next = function()
|
M.nav_next = function()
|
||||||
|
log.debug("nav_next()")
|
||||||
local current_index = Marked.get_current_index()
|
local current_index = Marked.get_current_index()
|
||||||
local number_of_items = Marked.get_length()
|
local number_of_items = Marked.get_length()
|
||||||
|
|
||||||
if current_index == nil then
|
if current_index == nil then
|
||||||
current_index = 1
|
current_index = 1
|
||||||
else
|
else
|
||||||
current_index = current_index + 1
|
current_index = current_index + 1
|
||||||
end
|
end
|
||||||
|
|
||||||
if (current_index > number_of_items) then
|
if (current_index > number_of_items) then
|
||||||
current_index = 1
|
current_index = 1
|
||||||
end
|
end
|
||||||
M.nav_file(current_index)
|
M.nav_file(current_index)
|
||||||
end
|
end
|
||||||
|
|
||||||
M.nav_prev = function()
|
M.nav_prev = function()
|
||||||
|
log.debug("nav_prev()")
|
||||||
local current_index = Marked.get_current_index()
|
local current_index = Marked.get_current_index()
|
||||||
local number_of_items = Marked.get_length()
|
local number_of_items = Marked.get_length()
|
||||||
|
|
||||||
if current_index == nil then
|
if current_index == nil then
|
||||||
current_index = number_of_items
|
current_index = number_of_items
|
||||||
else
|
else
|
||||||
current_index = current_index - 1
|
current_index = current_index - 1
|
||||||
end
|
end
|
||||||
|
|
||||||
if (current_index < 1) then
|
if (current_index < 1) then
|
||||||
current_index = number_of_items
|
current_index = number_of_items
|
||||||
end
|
end
|
||||||
|
|
||||||
@ -192,4 +197,3 @@ M.nav_prev = function()
|
|||||||
end
|
end
|
||||||
|
|
||||||
return M
|
return M
|
||||||
|
|
||||||
|
Loading…
x
Reference in New Issue
Block a user