feat: logger

This commit is contained in:
mpaulson 2023-12-04 11:55:53 -07:00
parent 80a428855f
commit 68b3223268
5 changed files with 59 additions and 12 deletions

View File

@ -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,

View File

@ -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

View File

@ -1,3 +1,4 @@
local Log = require("harpoon.logger")
local Listeners = require("harpoon.listeners")
local function index_of(items, element, config)

39
lua/harpoon/logger.lua Normal file
View File

@ -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()

View File

@ -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