mirror of
https://github.com/ThePrimeagen/harpoon.git
synced 2025-07-14 01:50:27 +00:00
Add logging
This commit is contained in:
parent
317e3a0e34
commit
8a4c443145
@ -6,7 +6,25 @@
|
|||||||
local M = {}
|
local M = {}
|
||||||
|
|
||||||
M.reload = function()
|
M.reload = function()
|
||||||
require("plenary.reload").reload_module("harpoon");
|
require("plenary.reload").reload_module("harpoon")
|
||||||
end
|
end
|
||||||
|
|
||||||
|
local function set_log_level()
|
||||||
|
local log_levels = { "trace", "debug", "info", "warning", "error", "fatal" }
|
||||||
|
local log_level = vim.g.harpoon_log_level
|
||||||
|
|
||||||
|
for _, level in pairs(log_levels) do
|
||||||
|
if level == log_level then
|
||||||
|
return log_level
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
return "warn" -- default, if user hasn't set
|
||||||
|
end
|
||||||
|
|
||||||
|
M.log = require("plenary.log").new({
|
||||||
|
plugin = "harpoon",
|
||||||
|
level = set_log_level(),
|
||||||
|
})
|
||||||
|
|
||||||
return M
|
return M
|
||||||
|
@ -1,7 +1,9 @@
|
|||||||
local Path = require("plenary.path")
|
local Path = require("plenary.path")
|
||||||
|
local utils = require("harpoon.utils")
|
||||||
|
local log = require("harpoon.dev").log
|
||||||
|
|
||||||
local config_path = vim.fn.stdpath("config")
|
local config_path = vim.fn.stdpath("config")
|
||||||
local data_path = vim.fn.stdpath("data")
|
local data_path = vim.fn.stdpath("data")
|
||||||
local utils = require("harpoon.utils")
|
|
||||||
local user_config = string.format("%s/harpoon.json", config_path)
|
local user_config = string.format("%s/harpoon.json", config_path)
|
||||||
local cache_config = string.format("%s/harpoon.json", data_path)
|
local cache_config = string.format("%s/harpoon.json", data_path)
|
||||||
|
|
||||||
@ -26,6 +28,7 @@ local M = {}
|
|||||||
... high level settings
|
... high level settings
|
||||||
}
|
}
|
||||||
--]]
|
--]]
|
||||||
|
|
||||||
HarpoonConfig = HarpoonConfig or {}
|
HarpoonConfig = HarpoonConfig or {}
|
||||||
|
|
||||||
-- tbl_deep_extend does not work the way you would think
|
-- tbl_deep_extend does not work the way you would think
|
||||||
@ -45,7 +48,7 @@ end
|
|||||||
|
|
||||||
local function merge_tables(...)
|
local function merge_tables(...)
|
||||||
local out = {}
|
local out = {}
|
||||||
for i = 1, select("#",...) do
|
for i = 1, select("#", ...) do
|
||||||
merge_table_impl(out, select(i, ...))
|
merge_table_impl(out, select(i, ...))
|
||||||
end
|
end
|
||||||
return out
|
return out
|
||||||
@ -54,23 +57,26 @@ end
|
|||||||
local function ensure_correct_config(config)
|
local function ensure_correct_config(config)
|
||||||
local projects = config.projects
|
local projects = config.projects
|
||||||
if projects[vim.loop.cwd()] == nil then
|
if projects[vim.loop.cwd()] == nil then
|
||||||
|
log.trace("ensure_correct_config(): No config found for:", vim.loop.cwd())
|
||||||
projects[vim.loop.cwd()] = {
|
projects[vim.loop.cwd()] = {
|
||||||
mark = {
|
mark = {
|
||||||
marks = {}
|
marks = {},
|
||||||
},
|
},
|
||||||
term = {
|
term = {
|
||||||
cmds = {}
|
cmds = {},
|
||||||
},
|
},
|
||||||
}
|
}
|
||||||
end
|
end
|
||||||
|
|
||||||
local proj = projects[vim.loop.cwd()]
|
local proj = projects[vim.loop.cwd()]
|
||||||
if proj.mark == nil then
|
if proj.mark == nil then
|
||||||
proj.mark = {marks = {}}
|
log.trace("ensure_correct_config(): No marks found for", vim.loop.cwd())
|
||||||
|
proj.mark = { marks = {} }
|
||||||
end
|
end
|
||||||
|
|
||||||
if proj.term == nil then
|
if proj.term == nil then
|
||||||
proj.term = {cmds = {}}
|
log.trace("ensure_correct_config(): No terminal commands found for", vim.loop.cwd())
|
||||||
|
proj.term = { cmds = {} }
|
||||||
end
|
end
|
||||||
|
|
||||||
local marks = proj.mark.marks
|
local marks = proj.mark.marks
|
||||||
@ -78,7 +84,7 @@ local function ensure_correct_config(config)
|
|||||||
local mark = marks[idx]
|
local mark = marks[idx]
|
||||||
if type(mark) == "string" then
|
if type(mark) == "string" then
|
||||||
mark = {
|
mark = {
|
||||||
filename = mark
|
filename = mark,
|
||||||
}
|
}
|
||||||
marks[idx] = mark
|
marks[idx] = mark
|
||||||
end
|
end
|
||||||
@ -103,15 +109,18 @@ local function expand_dir(config)
|
|||||||
end
|
end
|
||||||
|
|
||||||
M.save = function()
|
M.save = function()
|
||||||
Path:new(cache_config):write(vim.fn.json_encode(HarpoonConfig), 'w')
|
log.debug("save(): Saving cache config to", cache_config)
|
||||||
|
Path:new(cache_config):write(vim.fn.json_encode(HarpoonConfig), "w")
|
||||||
end
|
end
|
||||||
|
|
||||||
local function read_config(local_config)
|
local function read_config(local_config)
|
||||||
|
log.debug("_read_config():", local_config)
|
||||||
return vim.fn.json_decode(Path:new(local_config):read())
|
return vim.fn.json_decode(Path:new(local_config):read())
|
||||||
end
|
end
|
||||||
|
|
||||||
-- 1. saved. Where do we save?
|
-- 1. saved. Where do we save?
|
||||||
M.setup = function(config)
|
M.setup = function(config)
|
||||||
|
log.debug("setup(): Setting up...")
|
||||||
|
|
||||||
if not config then
|
if not config then
|
||||||
config = {}
|
config = {}
|
||||||
@ -121,43 +130,48 @@ M.setup = function(config)
|
|||||||
local ok2, c_config = pcall(read_config, cache_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)
|
||||||
u_config = {}
|
u_config = {}
|
||||||
end
|
end
|
||||||
|
|
||||||
if not ok2 then
|
if not ok2 then
|
||||||
|
log.debug("setup(): No cache config present at", cache_config)
|
||||||
c_config = {}
|
c_config = {}
|
||||||
end
|
end
|
||||||
|
|
||||||
local complete_config =
|
local complete_config = merge_tables({
|
||||||
merge_tables(
|
projects = {},
|
||||||
{projects = {} , global_settings = {
|
global_settings = {
|
||||||
["save_on_toggle"] = false,
|
["save_on_toggle"] = false,
|
||||||
["save_on_change"] = true,
|
["save_on_change"] = true,
|
||||||
}},
|
},
|
||||||
expand_dir(c_config),
|
}, expand_dir(c_config), expand_dir(u_config), expand_dir(config))
|
||||||
expand_dir(u_config),
|
|
||||||
expand_dir(config))
|
|
||||||
|
|
||||||
-- There was this issue where the vim.loop.cwd() didn't have marks or term, but had
|
-- There was this issue where the vim.loop.cwd() didn't have marks or term, but had
|
||||||
-- an object for vim.loop.cwd()
|
-- an object for vim.loop.cwd()
|
||||||
ensure_correct_config(complete_config)
|
ensure_correct_config(complete_config)
|
||||||
|
|
||||||
HarpoonConfig = complete_config
|
HarpoonConfig = complete_config
|
||||||
|
log.trace("setup(): Complete config", HarpoonConfig)
|
||||||
end
|
end
|
||||||
|
|
||||||
M.get_global_settings = function()
|
M.get_global_settings = function()
|
||||||
|
log.trace("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()")
|
||||||
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()")
|
||||||
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()")
|
||||||
return HarpoonConfig.menu or {}
|
return HarpoonConfig.menu or {}
|
||||||
end
|
end
|
||||||
|
|
||||||
@ -170,4 +184,3 @@ end
|
|||||||
M.setup()
|
M.setup()
|
||||||
|
|
||||||
return M
|
return M
|
||||||
|
|
||||||
|
@ -1,5 +1,6 @@
|
|||||||
local harpoon = require('harpoon')
|
local harpoon = require("harpoon")
|
||||||
local utils = require('harpoon.utils')
|
local utils = require("harpoon.utils")
|
||||||
|
local log = require("harpoon.dev").log
|
||||||
|
|
||||||
-- I think that I may have to organize this better. I am not the biggest fan
|
-- I think that I may have to organize this better. I am not the biggest fan
|
||||||
-- of procedural all the things
|
-- of procedural all the things
|
||||||
@ -14,6 +15,7 @@ local function emit_changed()
|
|||||||
end
|
end
|
||||||
|
|
||||||
if not callbacks["changed"] then
|
if not callbacks["changed"] then
|
||||||
|
log.debug("emit_changed(): no callbacks for 'changed', returning")
|
||||||
return
|
return
|
||||||
end
|
end
|
||||||
|
|
||||||
@ -81,21 +83,21 @@ 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
|
||||||
error("Couldn't find a valid file name to mark, sorry.")
|
log.error("Couldn't find a valid file name to mark, sorry.")
|
||||||
return
|
return
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
M.get_index_of = function(item)
|
M.get_index_of = function(item)
|
||||||
if item == nil then
|
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.")
|
log.error("You have provided a nil value to Harpoon, please provide a string rep of the file or the file idx.")
|
||||||
return
|
return
|
||||||
end
|
end
|
||||||
|
|
||||||
if type(item) == 'string' then
|
if type(item) == "string" then
|
||||||
local relative_item = utils.normalize_path(item)
|
local relative_item = utils.normalize_path(item)
|
||||||
for idx = 1, M.get_length() do
|
for idx = 1, M.get_length() do
|
||||||
if M.get_marked_file_name(idx) == relative_item then
|
if M.get_marked_file_name(idx) == relative_item then
|
||||||
return idx
|
return idx
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
@ -145,7 +147,7 @@ M.add_file = function(file_name_or_buf_id)
|
|||||||
local found_idx = get_first_empty_slot()
|
local found_idx = get_first_empty_slot()
|
||||||
harpoon.get_mark_config().marks[found_idx] = create_mark(buf_name)
|
harpoon.get_mark_config().marks[found_idx] = create_mark(buf_name)
|
||||||
M.remove_empty_tail(false)
|
M.remove_empty_tail(false)
|
||||||
emit_changed();
|
emit_changed()
|
||||||
end
|
end
|
||||||
|
|
||||||
-- dont_emit_on_changed should only be used internally
|
-- dont_emit_on_changed should only be used internally
|
||||||
@ -179,13 +181,11 @@ M.store_offset = function()
|
|||||||
return
|
return
|
||||||
end
|
end
|
||||||
|
|
||||||
harpoon.get_mark_config().marks[idx].row =
|
harpoon.get_mark_config().marks[idx].row = vim.fn.line(".")
|
||||||
vim.fn.line(".");
|
|
||||||
end)
|
end)
|
||||||
|
|
||||||
if not ok then
|
if not ok then
|
||||||
-- TODO: Developer logs?
|
log.warn("store_offset(): pcall failed:", res)
|
||||||
print("M.store_offset#pcall failed:", res)
|
|
||||||
end
|
end
|
||||||
|
|
||||||
emit_changed()
|
emit_changed()
|
||||||
@ -196,16 +196,19 @@ M.rm_file = function(file_name_or_buf_id)
|
|||||||
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
|
||||||
|
log.debug("rm_file(): No mark exists for id", file_name_or_buf_id)
|
||||||
return
|
return
|
||||||
end
|
end
|
||||||
|
|
||||||
harpoon.get_mark_config().marks[idx] = create_mark("")
|
harpoon.get_mark_config().marks[idx] = create_mark("")
|
||||||
M.remove_empty_tail(false)
|
M.remove_empty_tail(false)
|
||||||
emit_changed()
|
emit_changed()
|
||||||
|
log.debug("rm_file(): Removed mark at id", idx)
|
||||||
end
|
end
|
||||||
|
|
||||||
M.clear_all = function()
|
M.clear_all = function()
|
||||||
harpoon.get_mark_config().marks = {}
|
harpoon.get_mark_config().marks = {}
|
||||||
|
log.debug("clear_all(): Clearing all marks.")
|
||||||
emit_changed()
|
emit_changed()
|
||||||
end
|
end
|
||||||
|
|
||||||
@ -231,6 +234,8 @@ M.set_current_at = function(idx)
|
|||||||
local buf_name = get_buf_name()
|
local buf_name = get_buf_name()
|
||||||
local current_idx = M.get_index_of(buf_name)
|
local current_idx = M.get_index_of(buf_name)
|
||||||
|
|
||||||
|
log.debug("set_current_at(): Setting id", idx, buf_name)
|
||||||
|
|
||||||
-- Remove it if it already exists
|
-- Remove it if it already exists
|
||||||
if M.valid_index(current_idx) then
|
if M.valid_index(current_idx) then
|
||||||
config.marks[current_idx] = create_mark("")
|
config.marks[current_idx] = create_mark("")
|
||||||
@ -260,11 +265,11 @@ M.to_quickfix_list = function()
|
|||||||
col = mark.col,
|
col = mark.col,
|
||||||
}
|
}
|
||||||
end
|
end
|
||||||
|
log.debug("to_quickfix_list(): Sending marks to quickfix list.")
|
||||||
vim.fn.setqflist(qf_list)
|
vim.fn.setqflist(qf_list)
|
||||||
end
|
end
|
||||||
|
|
||||||
M.set_mark_list = function(new_list)
|
M.set_mark_list = function(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
|
||||||
@ -285,14 +290,18 @@ end
|
|||||||
M.toggle_file = function(file_name_or_buf_id)
|
M.toggle_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("toggle_file():", buf_name)
|
||||||
|
|
||||||
validate_buf_name(buf_name)
|
validate_buf_name(buf_name)
|
||||||
|
|
||||||
if (mark_exists(buf_name)) then
|
if (mark_exists(buf_name)) then
|
||||||
M.rm_file(buf_name)
|
M.rm_file(buf_name)
|
||||||
print("Mark removed")
|
print("Mark removed")
|
||||||
|
log.trace("toggle_file(): Mark removed")
|
||||||
else
|
else
|
||||||
M.add_file(buf_name)
|
M.add_file(buf_name)
|
||||||
print("Mark Added")
|
print("Mark added")
|
||||||
|
log.trace("toggle_file(): Mark added")
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
@ -302,6 +311,7 @@ end
|
|||||||
|
|
||||||
M.on = function(event, cb)
|
M.on = function(event, cb)
|
||||||
if not callbacks[event] then
|
if not callbacks[event] then
|
||||||
|
log.debug("on(): no callbacks yet for", event)
|
||||||
callbacks[event] = {}
|
callbacks[event] = {}
|
||||||
end
|
end
|
||||||
|
|
||||||
@ -309,5 +319,3 @@ M.on = function(event, cb)
|
|||||||
end
|
end
|
||||||
|
|
||||||
return M
|
return M
|
||||||
|
|
||||||
|
|
||||||
|
@ -1,6 +1,7 @@
|
|||||||
local harpoon = require('harpoon')
|
local harpoon = require("harpoon")
|
||||||
local popup = require('popup')
|
local popup = require("popup")
|
||||||
local Marked = require('harpoon.mark')
|
local Marked = require("harpoon.mark")
|
||||||
|
local log = require("harpoon.dev").log
|
||||||
|
|
||||||
local M = {}
|
local M = {}
|
||||||
|
|
||||||
@ -97,6 +98,7 @@ end
|
|||||||
M.nav_file = function(id)
|
M.nav_file = function(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)
|
||||||
return
|
return
|
||||||
end
|
end
|
||||||
|
|
||||||
|
Loading…
x
Reference in New Issue
Block a user