mirror of
https://github.com/ThePrimeagen/harpoon.git
synced 2025-07-13 17:40:25 +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
|
||||
|
||||
return "warn" -- default, if user hasn't set
|
||||
return "warn" -- default, if user hasn't set to one from log_levels
|
||||
end
|
||||
|
||||
M.log = require("plenary.log").new({
|
||||
|
@ -127,13 +127,14 @@ M.setup = function(config)
|
||||
end
|
||||
|
||||
local ok, u_config = pcall(read_config, user_config)
|
||||
local ok2, c_config = pcall(read_config, cache_config)
|
||||
|
||||
if not ok then
|
||||
log.debug("setup(): No user config present at", user_config)
|
||||
u_config = {}
|
||||
end
|
||||
|
||||
local ok2, c_config = pcall(read_config, cache_config)
|
||||
|
||||
if not ok2 then
|
||||
log.debug("setup(): No cache config present at", cache_config)
|
||||
c_config = {}
|
||||
@ -156,22 +157,22 @@ M.setup = function(config)
|
||||
end
|
||||
|
||||
M.get_global_settings = function()
|
||||
log.trace("get_global_settings()")
|
||||
log.debug("get_global_settings()")
|
||||
return HarpoonConfig.global_settings
|
||||
end
|
||||
|
||||
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
|
||||
end
|
||||
|
||||
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
|
||||
end
|
||||
|
||||
M.get_menu_config = function()
|
||||
log.trace("get_menu_config()")
|
||||
log.debug("get_menu_config()")
|
||||
return HarpoonConfig.menu or {}
|
||||
end
|
||||
|
||||
|
@ -19,7 +19,8 @@ local function emit_changed()
|
||||
return
|
||||
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()
|
||||
end
|
||||
end
|
||||
@ -36,6 +37,7 @@ local function filter_empty_string(list)
|
||||
end
|
||||
|
||||
local function get_first_empty_slot()
|
||||
log.debug("_get_first_empty_slot()")
|
||||
for idx = 1, M.get_length() do
|
||||
local filename = M.get_marked_file_name(idx)
|
||||
if filename == "" then
|
||||
@ -47,6 +49,7 @@ local function get_first_empty_slot()
|
||||
end
|
||||
|
||||
local function get_buf_name(id)
|
||||
log.debug("get_buf_name():", id)
|
||||
if id == nil then
|
||||
return utils.normalize_path(vim.fn.bufname(vim.fn.bufnr()))
|
||||
elseif type(id) == "string" then
|
||||
@ -72,25 +75,29 @@ local function create_mark(filename)
|
||||
end
|
||||
|
||||
local function mark_exists(buf_name)
|
||||
log.debug("_mark_exists()")
|
||||
for idx = 1, M.get_length() do
|
||||
if M.get_marked_file_name(idx) == buf_name then
|
||||
log.trace("_mark_exists(): Mark exists", buf_name)
|
||||
return true
|
||||
end
|
||||
end
|
||||
|
||||
log.trace("_mark_exists(): Mark doesn't exist", buf_name)
|
||||
return false
|
||||
end
|
||||
|
||||
local function validate_buf_name(buf_name)
|
||||
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
|
||||
end
|
||||
end
|
||||
|
||||
M.get_index_of = function(item)
|
||||
log.debug("get_index_of():", item)
|
||||
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
|
||||
end
|
||||
|
||||
@ -105,6 +112,7 @@ M.get_index_of = function(item)
|
||||
return nil
|
||||
end
|
||||
|
||||
-- TODO move this to a "harpoon_" prefix?
|
||||
if vim.g.manage_a_mark_zero_index then
|
||||
item = item + 1
|
||||
end
|
||||
@ -113,10 +121,12 @@ M.get_index_of = function(item)
|
||||
return item
|
||||
end
|
||||
|
||||
log.debug("get_index_of(): No item found,", item)
|
||||
return nil
|
||||
end
|
||||
|
||||
M.status = function()
|
||||
log.debug("status()")
|
||||
local idx = M.get_index_of(get_buf_name())
|
||||
|
||||
if M.valid_index(idx) then
|
||||
@ -126,6 +136,7 @@ M.status = function()
|
||||
end
|
||||
|
||||
M.valid_index = function(idx)
|
||||
log.debug("valid_index():", idx)
|
||||
if idx == nil then
|
||||
return false
|
||||
end
|
||||
@ -136,6 +147,7 @@ end
|
||||
|
||||
M.add_file = function(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
|
||||
-- we don't alter file layout.
|
||||
@ -150,8 +162,9 @@ M.add_file = function(file_name_or_buf_id)
|
||||
emit_changed()
|
||||
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)
|
||||
log.debug("remove_empty_tail()")
|
||||
_emit_on_changed = _emit_on_changed == nil or _emit_on_changed
|
||||
local config = harpoon.get_mark_config()
|
||||
local found = false
|
||||
@ -174,18 +187,22 @@ M.remove_empty_tail = function(_emit_on_changed)
|
||||
end
|
||||
|
||||
M.store_offset = function()
|
||||
log.debug("store_offset()")
|
||||
local ok, res = pcall(function()
|
||||
local buf_name = get_buf_name()
|
||||
local idx = M.get_index_of(buf_name)
|
||||
|
||||
if not M.valid_index(idx) then
|
||||
return
|
||||
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)
|
||||
|
||||
if not ok then
|
||||
log.warn("store_offset(): pcall failed:", res)
|
||||
log.warn("store_offset(): Could not store offset:", res)
|
||||
end
|
||||
|
||||
emit_changed()
|
||||
@ -214,6 +231,7 @@ end
|
||||
|
||||
--- ENTERPRISE PROGRAMMING
|
||||
M.get_marked_file = function(idxOrName)
|
||||
log.debug("get_marked_file():", idxOrName)
|
||||
if type(idxOrName) == "string" then
|
||||
idxOrName = M.get_index_of(idxOrName)
|
||||
end
|
||||
@ -222,10 +240,12 @@ end
|
||||
|
||||
M.get_marked_file_name = function(idx)
|
||||
local mark = harpoon.get_mark_config().marks[idx]
|
||||
log.debug("get_marked_file_name():", mark and mark.filename)
|
||||
return mark and mark.filename
|
||||
end
|
||||
|
||||
M.get_length = function()
|
||||
log.debug("get_length()")
|
||||
return table.maxn(harpoon.get_mark_config().marks)
|
||||
end
|
||||
|
||||
@ -270,6 +290,9 @@ M.to_quickfix_list = function()
|
||||
end
|
||||
|
||||
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()
|
||||
|
||||
for k, v in pairs(new_list) do
|
||||
@ -306,16 +329,19 @@ M.toggle_file = function(file_name_or_buf_id)
|
||||
end
|
||||
|
||||
M.get_current_index = function()
|
||||
log.debug("get_current_index()")
|
||||
return M.get_index_of(vim.fn.bufname(vim.fn.bufnr()))
|
||||
end
|
||||
|
||||
M.on = function(event, cb)
|
||||
log.debug("on():", event)
|
||||
if not callbacks[event] then
|
||||
log.debug("on(): no callbacks yet for", event)
|
||||
callbacks[event] = {}
|
||||
end
|
||||
|
||||
table.insert(callbacks[event], cb)
|
||||
log.trace(callbacks)
|
||||
end
|
||||
|
||||
return M
|
||||
|
@ -9,15 +9,16 @@ Harpoon_win_id = nil
|
||||
Harpoon_bufh = nil
|
||||
|
||||
local function create_window()
|
||||
log.debug("_create_window()")
|
||||
local config = harpoon.get_menu_config()
|
||||
local width = config.width or 60
|
||||
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 Harpoon_win_id, win = popup.create(bufnr, {
|
||||
title = 'Harpoon',
|
||||
highlight = 'HarpoonWindow',
|
||||
title = "Harpoon",
|
||||
highlight = "HarpoonWindow",
|
||||
line = math.floor(((vim.o.lines - height) / 2) - 1),
|
||||
col = math.floor((vim.o.columns - width) / 2),
|
||||
minwidth = width,
|
||||
@ -25,7 +26,7 @@ local function create_window()
|
||||
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 {
|
||||
bufnr = bufnr,
|
||||
@ -38,7 +39,7 @@ local function get_menu_items()
|
||||
local indices = {}
|
||||
|
||||
for idx = 1, #lines do
|
||||
local space_location = string.find(lines[idx], ' ')
|
||||
local space_location = string.find(lines[idx], " ")
|
||||
|
||||
if space_location ~= nil then
|
||||
table.insert(indices, string.sub(lines[idx], space_location + 1))
|
||||
@ -48,16 +49,13 @@ local function get_menu_items()
|
||||
return indices
|
||||
end
|
||||
|
||||
local save_changes = function()
|
||||
Marked.set_mark_list(get_menu_items())
|
||||
end
|
||||
|
||||
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
|
||||
local global_config = harpoon.get_global_settings()
|
||||
|
||||
if global_config.save_on_toggle then
|
||||
require('harpoon.ui').on_menu_save()
|
||||
require("harpoon.ui").on_menu_save()
|
||||
end
|
||||
|
||||
vim.api.nvim_win_close(Harpoon_win_id, true)
|
||||
@ -92,10 +90,12 @@ M.toggle_quick_menu = function()
|
||||
end
|
||||
|
||||
M.on_menu_save = function()
|
||||
save_changes()
|
||||
log.debug("on_menu_save()")
|
||||
Marked.set_mark_list(get_menu_items())
|
||||
end
|
||||
|
||||
M.nav_file = function(id)
|
||||
log.debug("nav_file(): Navigating to", id)
|
||||
local idx = Marked.get_index_of(id)
|
||||
if not Marked.valid_index(idx) then
|
||||
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)
|
||||
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
|
||||
|
||||
function M.location_window(options)
|
||||
local default_options = {
|
||||
relative = 'editor',
|
||||
style = 'minimal',
|
||||
relative = "editor",
|
||||
style = "minimal",
|
||||
width = 30,
|
||||
height = 15,
|
||||
row = 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 win_id = vim.fn.nvim_open_win(bufnr, true, options)
|
||||
@ -142,15 +145,15 @@ function M.notification(text)
|
||||
width = 20,
|
||||
height = 2,
|
||||
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)
|
||||
|
||||
return {
|
||||
bufnr = info.bufnr,
|
||||
win_id = info.win_id
|
||||
win_id = info.win_id,
|
||||
}
|
||||
end
|
||||
|
||||
@ -159,32 +162,34 @@ function M.close_notification(bufnr)
|
||||
end
|
||||
|
||||
M.nav_next = function()
|
||||
log.debug("nav_next()")
|
||||
local current_index = Marked.get_current_index()
|
||||
local number_of_items = Marked.get_length()
|
||||
|
||||
if current_index == nil then
|
||||
if current_index == nil then
|
||||
current_index = 1
|
||||
else
|
||||
current_index = current_index + 1
|
||||
end
|
||||
|
||||
if (current_index > number_of_items) then
|
||||
if (current_index > number_of_items) then
|
||||
current_index = 1
|
||||
end
|
||||
M.nav_file(current_index)
|
||||
end
|
||||
|
||||
M.nav_prev = function()
|
||||
log.debug("nav_prev()")
|
||||
local current_index = Marked.get_current_index()
|
||||
local number_of_items = Marked.get_length()
|
||||
|
||||
if current_index == nil then
|
||||
if current_index == nil then
|
||||
current_index = number_of_items
|
||||
else
|
||||
current_index = current_index - 1
|
||||
end
|
||||
|
||||
if (current_index < 1) then
|
||||
if (current_index < 1) then
|
||||
current_index = number_of_items
|
||||
end
|
||||
|
||||
@ -192,4 +197,3 @@ M.nav_prev = function()
|
||||
end
|
||||
|
||||
return M
|
||||
|
||||
|
Loading…
x
Reference in New Issue
Block a user