Skip to content

Commit

Permalink
feat: logger
Browse files Browse the repository at this point in the history
  • Loading branch information
ThePrimeagen committed Dec 4, 2023
1 parent 80a4288 commit 68b3223
Show file tree
Hide file tree
Showing 5 changed files with 59 additions and 12 deletions.
8 changes: 8 additions & 0 deletions lua/harpoon/config.lua
Original file line number Diff line number Diff line change
@@ -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)
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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 }
Expand All @@ -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,

Expand Down
12 changes: 3 additions & 9 deletions lua/harpoon/init.lua
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
local Log = require("harpoon.logger")
local Ui = require("harpoon.ui")
local Data = require("harpoon.data")
local Config = require("harpoon.config")
Expand All @@ -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 = {}
Expand All @@ -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 = {},
Expand All @@ -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
Expand Down
1 change: 1 addition & 0 deletions lua/harpoon/list.lua
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
local Log = require("harpoon.logger")
local Listeners = require("harpoon.listeners")

local function index_of(items, element, config)
Expand Down
39 changes: 39 additions & 0 deletions lua/harpoon/logger.lua
Original file line number Diff line number Diff line change
@@ -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()
11 changes: 8 additions & 3 deletions lua/harpoon/ui.lua
Original file line number Diff line number Diff line change
@@ -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
Expand Down Expand Up @@ -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 })
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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

Expand Down

0 comments on commit 68b3223

Please sign in to comment.