From 68b322326837f371ace9f6f2c57a0abda23696e4 Mon Sep 17 00:00:00 2001 From: mpaulson Date: Mon, 4 Dec 2023 11:55:53 -0700 Subject: [PATCH] feat: logger --- lua/harpoon/config.lua | 8 ++++++++ lua/harpoon/init.lua | 12 +++--------- lua/harpoon/list.lua | 1 + lua/harpoon/logger.lua | 39 +++++++++++++++++++++++++++++++++++++++ lua/harpoon/ui.lua | 11 ++++++++--- 5 files changed, 59 insertions(+), 12 deletions(-) create mode 100644 lua/harpoon/logger.lua diff --git a/lua/harpoon/config.lua b/lua/harpoon/config.lua index abd7998..d38cf89 100644 --- a/lua/harpoon/config.lua +++ b/lua/harpoon/config.lua @@ -1,3 +1,4 @@ +local Logger = require("harpoon.logger") local Path = require("plenary.path") local function normalize_path(buf_name, root) return Path:new(buf_name):make_relative(root) @@ -85,6 +86,7 @@ function M.get_default_config() ---@param list HarpoonList ---@param options HarpoonListFileOptions select = function(list_item, list, options) + Logger:log("config_default#select", list_item, list.name, options) options = options or {} if list_item == nil then return @@ -142,6 +144,8 @@ function M.get_default_config() config.get_root_dir() ) + Logger:log("config_default#add", name) + local bufnr = vim.fn.bufnr(name, false) local pos = { 1, 0 } @@ -165,8 +169,12 @@ function M.get_default_config() if item then local pos = vim.api.nvim_win_get_cursor(0) + + Logger:log("config_default#BufLeave updating position", bufnr, bufname, item, "to position", pos) + item.context.row = pos[1] item.context.col = pos[2] + end end, diff --git a/lua/harpoon/init.lua b/lua/harpoon/init.lua index 58b2e68..fd5677f 100644 --- a/lua/harpoon/init.lua +++ b/lua/harpoon/init.lua @@ -1,3 +1,4 @@ +local Log = require("harpoon.logger") local Ui = require("harpoon.ui") local Data = require("harpoon.data") local Config = require("harpoon.config") @@ -10,6 +11,7 @@ local HarpoonGroup = require("harpoon.autocmd") ---@field ui HarpoonUI ---@field listeners HarpoonListeners ---@field data HarpoonData +---@field logger HarpoonLog ---@field lists {[string]: {[string]: HarpoonList}} ---@field hooks_setup boolean local Harpoon = {} @@ -23,6 +25,7 @@ function Harpoon:new() local harpoon = setmetatable({ config = config, data = Data.Data:new(), + logger = Log, ui = Ui:new(config.settings), listeners = Listeners.listeners, lists = {}, @@ -38,15 +41,6 @@ function Harpoon:setup(partial_config) self.config = Config.merge_config(partial_config, self.config) self.ui:configure(self.config.settings) - local highlights = { - HarpoonWindow = { default = true, link = "NormalFloat" }, - HarpoonBorder = { default = true, link = "FloatBorder" }, - HarpoonTitle = { default = true, link = "FloatTitle" }, - } - for k, v in pairs(highlights) do - vim.api.nvim_set_hl(0, k, v) - end - ---TODO: should we go through every seen list and update its config? if self.hooks_setup == false then diff --git a/lua/harpoon/list.lua b/lua/harpoon/list.lua index 52588d9..7f88949 100644 --- a/lua/harpoon/list.lua +++ b/lua/harpoon/list.lua @@ -1,3 +1,4 @@ +local Log = require("harpoon.logger") local Listeners = require("harpoon.listeners") local function index_of(items, element, config) diff --git a/lua/harpoon/logger.lua b/lua/harpoon/logger.lua new file mode 100644 index 0000000..27ed51e --- /dev/null +++ b/lua/harpoon/logger.lua @@ -0,0 +1,39 @@ + +---@class HarpoonLog +---@field lines string[] +local HarpoonLog = {} + +HarpoonLog.__index = HarpoonLog + +---@return HarpoonLog +function HarpoonLog:new() + local logger = setmetatable({ + lines = {}, + }, self) + + return logger +end + +---@vararg any +function HarpoonLog:log(...) + + local msg = {} + for i = 1, select("#", ...) do + table.insert(msg, vim.inspect(select(i, ...))) + end + + table.insert(self.lines, table.concat(msg, " ")) +end + +function HarpoonLog:clear() + self.lines = {} +end + +function HarpoonLog:show() + local bufnr = vim.api.nvim_create_buf(false, true) + print(vim.inspect(self.lines)) + vim.api.nvim_buf_set_lines(bufnr, 0, -1, false, self.lines) + vim.api.nvim_win_set_buf(0, bufnr) +end + +return HarpoonLog:new() diff --git a/lua/harpoon/ui.lua b/lua/harpoon/ui.lua index 1e9e9e7..7c4548f 100644 --- a/lua/harpoon/ui.lua +++ b/lua/harpoon/ui.lua @@ -1,5 +1,6 @@ local popup = require("plenary").popup local Buffer = require("harpoon.buffer") +local Logger = require("harpoon.logger") local DEFAULT_WINDOW_WIDTH = 69 -- nice ---@class HarpoonUI @@ -30,6 +31,10 @@ function HarpoonUI:close_menu() end self.closing = true + Logger:log("ui#close_menu name: ", self.active_list.name, "win and bufnr", { + win = self.win_id, + bufnr = self.bufnr + }) if self.bufnr ~= nil and vim.api.nvim_buf_is_valid(self.bufnr) then vim.api.nvim_buf_delete(self.bufnr, { force = true }) @@ -90,11 +95,10 @@ function HarpoonUI:_create_window() return win_id, bufnr end -local count = 0 - ---@param list? HarpoonList function HarpoonUI:toggle_quick_menu(list) - count = count + 1 + + Logger:log("ui#toggle_quick_menu", list and list.name) if list == nil or self.win_id ~= nil then if self.settings.save_on_toggle then @@ -129,6 +133,7 @@ end function HarpoonUI:save() local list = Buffer.get_contents(self.bufnr) + Logger:log("ui#save", list) self.active_list:resolve_displayed(list) end