Skip to content

Commit

Permalink
chore: format
Browse files Browse the repository at this point in the history
  • Loading branch information
ThePrimeagen committed Nov 29, 2023
1 parent 6fdff8b commit b58e3db
Show file tree
Hide file tree
Showing 13 changed files with 102 additions and 92 deletions.
1 change: 0 additions & 1 deletion lua/harpoon2/buffer.lua
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,6 @@ function M.setup_autocmds_and_keymaps(bufnr)
vim.cmd(
"autocmd BufLeave <buffer> ++nested ++once silent lua require('harpoon2').ui:toggle_quick_menu()"
)

end

---@param bufnr number
Expand Down
19 changes: 9 additions & 10 deletions lua/harpoon2/config.lua
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,6 @@ local M = {}
---@field settings? HarpoonPartialSettings
---@field [string] HarpoonPartialConfigItem


---@return HarpoonPartialConfigItem
function M.get_config(config, name)
return vim.tbl_extend("force", {}, config.default, config[name] or {})
Expand Down Expand Up @@ -104,7 +103,7 @@ function M.get_default_config()
if set_position then
vim.api.nvim_win_set_cursor(0, {
file_item.context.row or 1,
file_item.context.col or 0
file_item.context.col or 0,
})
end
end,
Expand All @@ -122,17 +121,17 @@ function M.get_default_config()
---@param name any
---@return HarpoonListItem
add = function(name)
name = name or
name = name
-- TODO: should we do path normalization???
-- i know i have seen sometimes it becoming an absolute
-- path, if that is the case we can use the context to
-- store the bufname and then have value be the normalized
-- value
vim.api.nvim_buf_get_name(vim.api.nvim_get_current_buf())
or vim.api.nvim_buf_get_name(vim.api.nvim_get_current_buf())

local bufnr = vim.fn.bufnr(name, false)

local pos = {1, 0}
local pos = { 1, 0 }
if bufnr ~= -1 then
pos = vim.api.nvim_win_get_cursor(0)
end
Expand All @@ -142,13 +141,13 @@ function M.get_default_config()
context = {
row = pos[1],
col = pos[2],
}
},
}
end,

BufLeave = function(arg, list)
local bufnr = arg.buf;
local bufname = vim.api.nvim_buf_get_name(bufnr);
local bufnr = arg.buf
local bufname = vim.api.nvim_buf_get_name(bufnr)
local item = list:get_by_display(bufname)

if item then
Expand All @@ -158,8 +157,8 @@ function M.get_default_config()
end
end,

autocmds = {"BufLeave"},
}
autocmds = { "BufLeave" },
},
}
end

Expand Down
15 changes: 8 additions & 7 deletions lua/harpoon2/data.lua
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,6 @@ end
--- @field has_error boolean
local Data = {}


-- 1. load the data
-- 2. keep track of the lists requested
-- 3. sync save
Expand All @@ -49,7 +48,7 @@ Data.__index = Data

---@return HarpoonRawData
local function read_data()
local path = Path:new(full_data_path)
local path = Path:new(full_data_path)
local exists = path:exists()

if not exists then
Expand All @@ -68,9 +67,8 @@ function Data:new()
return setmetatable({
_data = data,
has_error = not ok,
seen = {}
seen = {},
}, self)

end

---@param key string
Expand All @@ -89,7 +87,9 @@ end
---@return string[]
function Data:data(key, name)
if self.has_error then
error("Harpoon: there was an error reading the data file, cannot read data")
error(
"Harpoon: there was an error reading the data file, cannot read data"
)
end

if not self.seen[key] then
Expand All @@ -105,7 +105,9 @@ end
---@param values string[]
function Data:update(key, name, values)
if self.has_error then
error("Harpoon: there was an error reading the data file, cannot update")
error(
"Harpoon: there was an error reading the data file, cannot update"
)
end
self:_get_data(key, name)
self._data[key][name] = values
Expand Down Expand Up @@ -136,7 +138,6 @@ function Data:sync()
end
end


M.Data = Data

return M
7 changes: 3 additions & 4 deletions lua/harpoon2/init.lua
Original file line number Diff line number Diff line change
Expand Up @@ -49,14 +49,13 @@ function Harpoon:setup(partial_config)

if self.hooks_setup == false then
local augroup = vim.api.nvim_create_augroup
local HarpoonGroup = augroup('Harpoon', {})
local HarpoonGroup = augroup("Harpoon", {})

vim.api.nvim_create_autocmd({"BufLeave", "VimLeavePre"}, {
vim.api.nvim_create_autocmd({ "BufLeave", "VimLeavePre" }, {
group = HarpoonGroup,
pattern = '*',
pattern = "*",
callback = function(ev)
self:_for_each_list(function(list, config)

local fn = config[ev.event]
if fn ~= nil then
fn(ev, list)
Expand Down
46 changes: 34 additions & 12 deletions lua/harpoon2/list.lua
Original file line number Diff line number Diff line change
@@ -1,7 +1,10 @@
local Listeners = require("harpoon2.listeners")

local function index_of(items, element, config)
local equals = config and config.equals or function(a, b) return a == b end
local equals = config and config.equals
or function(a, b)
return a == b
end
local index = -1
for i, item in ipairs(items) do
if equals(element, item) then
Expand Down Expand Up @@ -45,7 +48,10 @@ function HarpoonList:append(item)

local index = index_of(self.items, item, self.config)
if index == -1 then
Listeners.listeners:emit(Listeners.event_names.ADD, {list = self, item = item, idx = #self.items + 1})
Listeners.listeners:emit(
Listeners.event_names.ADD,
{ list = self, item = item, idx = #self.items + 1 }
)
table.insert(self.items, item)
end

Expand All @@ -57,7 +63,10 @@ function HarpoonList:prepend(item)
item = item or self.config.add()
local index = index_of(self.items, item, self.config)
if index == -1 then
Listeners.listeners:emit(Listeners.event_names.ADD, {list = self, item = item, idx = 1})
Listeners.listeners:emit(
Listeners.event_names.ADD,
{ list = self, item = item, idx = 1 }
)
table.insert(self.items, 1, item)
end

Expand All @@ -68,7 +77,10 @@ end
function HarpoonList:remove(item)
for i, v in ipairs(self.items) do
if self.config.equals(v, item) then
Listeners.listeners:emit(Listeners.event_names.REMOVE, {list = self, item = item, idx = i})
Listeners.listeners:emit(
Listeners.event_names.REMOVE,
{ list = self, item = item, idx = i }
)
table.remove(self.items, i)
break
end
Expand All @@ -78,7 +90,10 @@ end

---@return HarpoonList
function HarpoonList:removeAt(index)
Listeners.listeners:emit(Listeners.event_names.REMOVE, {list = self, item = self.items[index], idx = index})
Listeners.listeners:emit(
Listeners.event_names.REMOVE,
{ list = self, item = self.items[index], idx = index }
)
table.remove(self.items, index)
return self
end
Expand All @@ -96,7 +111,6 @@ function HarpoonList:get_by_display(name)
return self.items[index]
end


--- much inefficiencies. dun care
---@param displayed string[]
function HarpoonList:resolve_displayed(displayed)
Expand All @@ -107,17 +121,24 @@ function HarpoonList:resolve_displayed(displayed)
for i, v in ipairs(list_displayed) do
local index = index_of(list_displayed, v)
if index == -1 then
Listeners.listeners:emit(Listeners.event_names.REMOVE, {list = self, item = v, idx = i})
Listeners.listeners:emit(
Listeners.event_names.REMOVE,
{ list = self, item = v, idx = i }
)
end
end

for i, v in ipairs(displayed) do
local index = index_of(list_displayed, v)
if index == -1 then
Listeners.listeners:emit(Listeners.event_names.ADD, {list = self, item = v, idx = i})
Listeners.listeners:emit(
Listeners.event_names.ADD,
{ list = self, item = v, idx = i }
)
new_list[i] = self.config.add(v)
else
local index_in_new_list = index_of(new_list, self.items[index], self.config)
local index_in_new_list =
index_of(new_list, self.items[index], self.config)
if index_in_new_list == -1 then
new_list[i] = self.items[index]
end
Expand All @@ -130,7 +151,10 @@ end
function HarpoonList:select(index, options)
local item = self.items[index]
if item then
Listeners.listeners:emit(Listeners.event_names.SELECT, {list = self, item = item, idx = index})
Listeners.listeners:emit(
Listeners.event_names.SELECT,
{ list = self, item = item, idx = index }
)
self.config.select(item, options)
end
end
Expand Down Expand Up @@ -187,6 +211,4 @@ function HarpoonList.decode(list_config, name, items)
return HarpoonList:new(list_config, name, list_items)
end


return HarpoonList

5 changes: 2 additions & 3 deletions lua/harpoon2/listeners.lua
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@

---@alias HarpoonListener fun(type: string, args: any[] | any | nil): nil

---@class HarpoonListeners
Expand All @@ -11,14 +10,14 @@ HarpoonListeners.__index = HarpoonListeners
function HarpoonListeners:new()
return setmetatable({
listeners = {},
listenersByType = {}
listenersByType = {},
}, self)
end

---@param cbOrType HarpoonListener | string
---@param cbOrNil HarpoonListener | string
function HarpoonListeners:add_listener(cbOrType, cbOrNil)
if (type(cbOrType) == "string") then
if type(cbOrType) == "string" then
if not self.listenersByType[cbOrType] then
self.listenersByType[cbOrType] = {}
end
Expand Down
3 changes: 0 additions & 3 deletions lua/harpoon2/scratch/toggle.lua
Original file line number Diff line number Diff line change
@@ -1,6 +1,3 @@

local harpoon = require("harpoon2")

harpoon.ui:toggle_quick_menu(harpoon:list())


8 changes: 3 additions & 5 deletions lua/harpoon2/test/config_spec.lua
Original file line number Diff line number Diff line change
Expand Up @@ -14,19 +14,17 @@ describe("config", function()
"foo",
"bar",
"baz",
"qux"
"qux",
})
vim.api.nvim_win_set_cursor(0, {3, 1})
vim.api.nvim_win_set_cursor(0, { 3, 1 })

local item = config_item.add()
eq(item, {
value = "/tmp/harpoon-test",
context = {
row = 3,
col = 1,
}
},
})
end)
end)


Loading

0 comments on commit b58e3db

Please sign in to comment.