From 8a4c4431452009fb8e9b7e346d42a4f0a617452e Mon Sep 17 00:00:00 2001 From: Raigo Jerva Date: Tue, 27 Apr 2021 20:40:47 +0300 Subject: [PATCH] Add logging --- lua/harpoon/dev.lua | 20 +++++++++++++++++- lua/harpoon/init.lua | 49 ++++++++++++++++++++++++++++---------------- lua/harpoon/mark.lua | 38 ++++++++++++++++++++-------------- lua/harpoon/ui.lua | 8 +++++--- 4 files changed, 78 insertions(+), 37 deletions(-) diff --git a/lua/harpoon/dev.lua b/lua/harpoon/dev.lua index 192cef2..e428250 100644 --- a/lua/harpoon/dev.lua +++ b/lua/harpoon/dev.lua @@ -6,7 +6,25 @@ local M = {} M.reload = function() - require("plenary.reload").reload_module("harpoon"); + require("plenary.reload").reload_module("harpoon") 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 diff --git a/lua/harpoon/init.lua b/lua/harpoon/init.lua index 8c3d92b..cef3d04 100644 --- a/lua/harpoon/init.lua +++ b/lua/harpoon/init.lua @@ -1,7 +1,9 @@ local Path = require("plenary.path") +local utils = require("harpoon.utils") +local log = require("harpoon.dev").log + local config_path = vim.fn.stdpath("config") local data_path = vim.fn.stdpath("data") -local utils = require("harpoon.utils") local user_config = string.format("%s/harpoon.json", config_path) local cache_config = string.format("%s/harpoon.json", data_path) @@ -26,6 +28,7 @@ local M = {} ... high level settings } --]] + HarpoonConfig = HarpoonConfig or {} -- tbl_deep_extend does not work the way you would think @@ -45,7 +48,7 @@ end local function merge_tables(...) local out = {} - for i = 1, select("#",...) do + for i = 1, select("#", ...) do merge_table_impl(out, select(i, ...)) end return out @@ -54,23 +57,26 @@ end local function ensure_correct_config(config) local projects = config.projects if projects[vim.loop.cwd()] == nil then + log.trace("ensure_correct_config(): No config found for:", vim.loop.cwd()) projects[vim.loop.cwd()] = { mark = { - marks = {} + marks = {}, }, term = { - cmds = {} + cmds = {}, }, } end local proj = projects[vim.loop.cwd()] if proj.mark == nil then - proj.mark = {marks = {}} + log.trace("ensure_correct_config(): No marks found for", vim.loop.cwd()) + proj.mark = { marks = {} } end 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 local marks = proj.mark.marks @@ -78,7 +84,7 @@ local function ensure_correct_config(config) local mark = marks[idx] if type(mark) == "string" then mark = { - filename = mark + filename = mark, } marks[idx] = mark end @@ -103,15 +109,18 @@ local function expand_dir(config) end 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 local function read_config(local_config) + log.debug("_read_config():", local_config) return vim.fn.json_decode(Path:new(local_config):read()) end -- 1. saved. Where do we save? M.setup = function(config) + log.debug("setup(): Setting up...") if not config then config = {} @@ -121,43 +130,48 @@ M.setup = function(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 if not ok2 then + log.debug("setup(): No cache config present at", cache_config) c_config = {} end - local complete_config = - merge_tables( - {projects = {} , global_settings = { - ["save_on_toggle"] = false, - ["save_on_change"] = true, - }}, - expand_dir(c_config), - expand_dir(u_config), - expand_dir(config)) + local complete_config = merge_tables({ + projects = {}, + global_settings = { + ["save_on_toggle"] = false, + ["save_on_change"] = true, + }, + }, expand_dir(c_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 -- an object for vim.loop.cwd() ensure_correct_config(complete_config) HarpoonConfig = complete_config + log.trace("setup(): Complete config", HarpoonConfig) end M.get_global_settings = function() + log.trace("get_global_settings()") return HarpoonConfig.global_settings end M.get_term_config = function() + log.trace("get_term_config()") return ensure_correct_config(HarpoonConfig).projects[vim.loop.cwd()].term end M.get_mark_config = function() + -- log.trace("get_mark_config()") return ensure_correct_config(HarpoonConfig).projects[vim.loop.cwd()].mark end M.get_menu_config = function() + log.trace("get_menu_config()") return HarpoonConfig.menu or {} end @@ -170,4 +184,3 @@ end M.setup() return M - diff --git a/lua/harpoon/mark.lua b/lua/harpoon/mark.lua index a5097e6..225911f 100644 --- a/lua/harpoon/mark.lua +++ b/lua/harpoon/mark.lua @@ -1,5 +1,6 @@ -local harpoon = require('harpoon') -local utils = require('harpoon.utils') +local harpoon = require("harpoon") +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 -- of procedural all the things @@ -14,6 +15,7 @@ local function emit_changed() end if not callbacks["changed"] then + log.debug("emit_changed(): no callbacks for 'changed', returning") return end @@ -81,21 +83,21 @@ end local function validate_buf_name(buf_name) 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 end end 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.") + log.error("You have provided a nil value to Harpoon, please provide a string rep of the file or the file idx.") return end - if type(item) == 'string' then + if type(item) == "string" then local relative_item = utils.normalize_path(item) 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 end end @@ -145,7 +147,7 @@ M.add_file = function(file_name_or_buf_id) local found_idx = get_first_empty_slot() harpoon.get_mark_config().marks[found_idx] = create_mark(buf_name) M.remove_empty_tail(false) - emit_changed(); + emit_changed() end -- dont_emit_on_changed should only be used internally @@ -179,13 +181,11 @@ M.store_offset = function() return end - harpoon.get_mark_config().marks[idx].row = - vim.fn.line("."); + harpoon.get_mark_config().marks[idx].row = vim.fn.line(".") end) if not ok then - -- TODO: Developer logs? - print("M.store_offset#pcall failed:", res) + log.warn("store_offset(): pcall failed:", res) end emit_changed() @@ -196,16 +196,19 @@ M.rm_file = function(file_name_or_buf_id) local idx = M.get_index_of(buf_name) if not M.valid_index(idx) then + log.debug("rm_file(): No mark exists for id", file_name_or_buf_id) return end harpoon.get_mark_config().marks[idx] = create_mark("") M.remove_empty_tail(false) emit_changed() + log.debug("rm_file(): Removed mark at id", idx) end M.clear_all = function() harpoon.get_mark_config().marks = {} + log.debug("clear_all(): Clearing all marks.") emit_changed() end @@ -231,6 +234,8 @@ M.set_current_at = function(idx) local buf_name = get_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 if M.valid_index(current_idx) then config.marks[current_idx] = create_mark("") @@ -260,11 +265,11 @@ M.to_quickfix_list = function() col = mark.col, } end + log.debug("to_quickfix_list(): Sending marks to quickfix list.") vim.fn.setqflist(qf_list) end M.set_mark_list = function(new_list) - local config = harpoon.get_mark_config() for k, v in pairs(new_list) do @@ -285,14 +290,18 @@ end M.toggle_file = function(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) if (mark_exists(buf_name)) then M.rm_file(buf_name) print("Mark removed") + log.trace("toggle_file(): Mark removed") else M.add_file(buf_name) - print("Mark Added") + print("Mark added") + log.trace("toggle_file(): Mark added") end end @@ -302,6 +311,7 @@ end M.on = function(event, cb) if not callbacks[event] then + log.debug("on(): no callbacks yet for", event) callbacks[event] = {} end @@ -309,5 +319,3 @@ M.on = function(event, cb) end return M - - diff --git a/lua/harpoon/ui.lua b/lua/harpoon/ui.lua index c87e163..a94d739 100644 --- a/lua/harpoon/ui.lua +++ b/lua/harpoon/ui.lua @@ -1,6 +1,7 @@ -local harpoon = require('harpoon') -local popup = require('popup') -local Marked = require('harpoon.mark') +local harpoon = require("harpoon") +local popup = require("popup") +local Marked = require("harpoon.mark") +local log = require("harpoon.dev").log local M = {} @@ -97,6 +98,7 @@ end M.nav_file = function(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) return end