chore: format

This commit is contained in:
mpaulson 2023-11-28 20:36:20 -07:00
parent 6fdff8bc41
commit b58e3db559
13 changed files with 102 additions and 92 deletions

View File

@ -91,7 +91,6 @@ function M.setup_autocmds_and_keymaps(bufnr)
vim.cmd( vim.cmd(
"autocmd BufLeave <buffer> ++nested ++once silent lua require('harpoon2').ui:toggle_quick_menu()" "autocmd BufLeave <buffer> ++nested ++once silent lua require('harpoon2').ui:toggle_quick_menu()"
) )
end end
---@param bufnr number ---@param bufnr number

View File

@ -42,7 +42,6 @@ local M = {}
---@field settings? HarpoonPartialSettings ---@field settings? HarpoonPartialSettings
---@field [string] HarpoonPartialConfigItem ---@field [string] HarpoonPartialConfigItem
---@return HarpoonPartialConfigItem ---@return HarpoonPartialConfigItem
function M.get_config(config, name) function M.get_config(config, name)
return vim.tbl_extend("force", {}, config.default, config[name] or {}) return vim.tbl_extend("force", {}, config.default, config[name] or {})
@ -104,7 +103,7 @@ function M.get_default_config()
if set_position then if set_position then
vim.api.nvim_win_set_cursor(0, { vim.api.nvim_win_set_cursor(0, {
file_item.context.row or 1, file_item.context.row or 1,
file_item.context.col or 0 file_item.context.col or 0,
}) })
end end
end, end,
@ -122,13 +121,13 @@ function M.get_default_config()
---@param name any ---@param name any
---@return HarpoonListItem ---@return HarpoonListItem
add = function(name) add = function(name)
name = name or name = name
-- TODO: should we do path normalization??? -- TODO: should we do path normalization???
-- i know i have seen sometimes it becoming an absolute -- i know i have seen sometimes it becoming an absolute
-- path, if that is the case we can use the context to -- path, if that is the case we can use the context to
-- store the bufname and then have value be the normalized -- store the bufname and then have value be the normalized
-- value -- 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 bufnr = vim.fn.bufnr(name, false)
@ -142,13 +141,13 @@ function M.get_default_config()
context = { context = {
row = pos[1], row = pos[1],
col = pos[2], col = pos[2],
} },
} }
end, end,
BufLeave = function(arg, list) BufLeave = function(arg, list)
local bufnr = arg.buf; local bufnr = arg.buf
local bufname = vim.api.nvim_buf_get_name(bufnr); local bufname = vim.api.nvim_buf_get_name(bufnr)
local item = list:get_by_display(bufname) local item = list:get_by_display(bufname)
if item then if item then
@ -159,7 +158,7 @@ function M.get_default_config()
end, end,
autocmds = { "BufLeave" }, autocmds = { "BufLeave" },
} },
} }
end end

View File

@ -40,7 +40,6 @@ end
--- @field has_error boolean --- @field has_error boolean
local Data = {} local Data = {}
-- 1. load the data -- 1. load the data
-- 2. keep track of the lists requested -- 2. keep track of the lists requested
-- 3. sync save -- 3. sync save
@ -68,9 +67,8 @@ function Data:new()
return setmetatable({ return setmetatable({
_data = data, _data = data,
has_error = not ok, has_error = not ok,
seen = {} seen = {},
}, self) }, self)
end end
---@param key string ---@param key string
@ -89,7 +87,9 @@ end
---@return string[] ---@return string[]
function Data:data(key, name) function Data:data(key, name)
if self.has_error then 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 end
if not self.seen[key] then if not self.seen[key] then
@ -105,7 +105,9 @@ end
---@param values string[] ---@param values string[]
function Data:update(key, name, values) function Data:update(key, name, values)
if self.has_error then 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 end
self:_get_data(key, name) self:_get_data(key, name)
self._data[key][name] = values self._data[key][name] = values
@ -136,7 +138,6 @@ function Data:sync()
end end
end end
M.Data = Data M.Data = Data
return M return M

View File

@ -49,14 +49,13 @@ function Harpoon:setup(partial_config)
if self.hooks_setup == false then if self.hooks_setup == false then
local augroup = vim.api.nvim_create_augroup 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, group = HarpoonGroup,
pattern = '*', pattern = "*",
callback = function(ev) callback = function(ev)
self:_for_each_list(function(list, config) self:_for_each_list(function(list, config)
local fn = config[ev.event] local fn = config[ev.event]
if fn ~= nil then if fn ~= nil then
fn(ev, list) fn(ev, list)

View File

@ -1,7 +1,10 @@
local Listeners = require("harpoon2.listeners") local Listeners = require("harpoon2.listeners")
local function index_of(items, element, config) 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 local index = -1
for i, item in ipairs(items) do for i, item in ipairs(items) do
if equals(element, item) then if equals(element, item) then
@ -45,7 +48,10 @@ function HarpoonList:append(item)
local index = index_of(self.items, item, self.config) local index = index_of(self.items, item, self.config)
if index == -1 then 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) table.insert(self.items, item)
end end
@ -57,7 +63,10 @@ function HarpoonList:prepend(item)
item = item or self.config.add() item = item or self.config.add()
local index = index_of(self.items, item, self.config) local index = index_of(self.items, item, self.config)
if index == -1 then 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) table.insert(self.items, 1, item)
end end
@ -68,7 +77,10 @@ end
function HarpoonList:remove(item) function HarpoonList:remove(item)
for i, v in ipairs(self.items) do for i, v in ipairs(self.items) do
if self.config.equals(v, item) then 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) table.remove(self.items, i)
break break
end end
@ -78,7 +90,10 @@ end
---@return HarpoonList ---@return HarpoonList
function HarpoonList:removeAt(index) 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) table.remove(self.items, index)
return self return self
end end
@ -96,7 +111,6 @@ function HarpoonList:get_by_display(name)
return self.items[index] return self.items[index]
end end
--- much inefficiencies. dun care --- much inefficiencies. dun care
---@param displayed string[] ---@param displayed string[]
function HarpoonList:resolve_displayed(displayed) function HarpoonList:resolve_displayed(displayed)
@ -107,17 +121,24 @@ function HarpoonList:resolve_displayed(displayed)
for i, v in ipairs(list_displayed) do for i, v in ipairs(list_displayed) do
local index = index_of(list_displayed, v) local index = index_of(list_displayed, v)
if index == -1 then 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
end end
for i, v in ipairs(displayed) do for i, v in ipairs(displayed) do
local index = index_of(list_displayed, v) local index = index_of(list_displayed, v)
if index == -1 then 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) new_list[i] = self.config.add(v)
else 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 if index_in_new_list == -1 then
new_list[i] = self.items[index] new_list[i] = self.items[index]
end end
@ -130,7 +151,10 @@ end
function HarpoonList:select(index, options) function HarpoonList:select(index, options)
local item = self.items[index] local item = self.items[index]
if item then 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) self.config.select(item, options)
end end
end end
@ -187,6 +211,4 @@ function HarpoonList.decode(list_config, name, items)
return HarpoonList:new(list_config, name, list_items) return HarpoonList:new(list_config, name, list_items)
end end
return HarpoonList return HarpoonList

View File

@ -1,4 +1,3 @@
---@alias HarpoonListener fun(type: string, args: any[] | any | nil): nil ---@alias HarpoonListener fun(type: string, args: any[] | any | nil): nil
---@class HarpoonListeners ---@class HarpoonListeners
@ -11,14 +10,14 @@ HarpoonListeners.__index = HarpoonListeners
function HarpoonListeners:new() function HarpoonListeners:new()
return setmetatable({ return setmetatable({
listeners = {}, listeners = {},
listenersByType = {} listenersByType = {},
}, self) }, self)
end end
---@param cbOrType HarpoonListener | string ---@param cbOrType HarpoonListener | string
---@param cbOrNil HarpoonListener | string ---@param cbOrNil HarpoonListener | string
function HarpoonListeners:add_listener(cbOrType, cbOrNil) function HarpoonListeners:add_listener(cbOrType, cbOrNil)
if (type(cbOrType) == "string") then if type(cbOrType) == "string" then
if not self.listenersByType[cbOrType] then if not self.listenersByType[cbOrType] then
self.listenersByType[cbOrType] = {} self.listenersByType[cbOrType] = {}
end end

View File

@ -1,6 +1,3 @@
local harpoon = require("harpoon2") local harpoon = require("harpoon2")
harpoon.ui:toggle_quick_menu(harpoon:list()) harpoon.ui:toggle_quick_menu(harpoon:list())

View File

@ -14,7 +14,7 @@ describe("config", function()
"foo", "foo",
"bar", "bar",
"baz", "baz",
"qux" "qux",
}) })
vim.api.nvim_win_set_cursor(0, { 3, 1 }) vim.api.nvim_win_set_cursor(0, { 3, 1 })
@ -24,9 +24,7 @@ describe("config", function()
context = { context = {
row = 3, row = 3,
col = 1, col = 1,
} },
}) })
end) end)
end) end)

View File

@ -6,7 +6,6 @@ local eq = assert.are.same
local be = utils.before_each(os.tmpname()) local be = utils.before_each(os.tmpname())
describe("harpoon", function() describe("harpoon", function()
before_each(function() before_each(function()
be() be()
harpoon = require("harpoon2") harpoon = require("harpoon2")
@ -20,7 +19,7 @@ describe("harpoon", function()
"foo", "foo",
"bar", "bar",
"baz", "baz",
"qux" "qux",
}, row, col) }, row, col)
local list = harpoon:list():append() local list = harpoon:list():append()
@ -28,7 +27,7 @@ describe("harpoon", function()
"foo", "foo",
"bar", "bar",
"baz", "baz",
"qux" "qux",
}, row, col) }, row, col)
vim.api.nvim_set_current_buf(target_buf) vim.api.nvim_set_current_buf(target_buf)
@ -40,7 +39,6 @@ describe("harpoon", function()
} }
eq(expected, list.items) eq(expected, list.items)
end) end)
it("full harpoon add sync cycle", function() it("full harpoon add sync cycle", function()
@ -52,7 +50,7 @@ describe("harpoon", function()
"foo", "foo",
"bar", "bar",
"baz", "baz",
"qux" "qux",
}, row, col) }, row, col)
local list = harpoon:list() local list = harpoon:list()
@ -61,8 +59,8 @@ describe("harpoon", function()
eq(harpoon:dump(), { eq(harpoon:dump(), {
testies = { testies = {
[default_list_name] = list:encode() [default_list_name] = list:encode(),
} },
}) })
end) end)
@ -88,8 +86,8 @@ describe("harpoon", function()
eq(harpoon:dump(), { eq(harpoon:dump(), {
testies = { testies = {
[default_list_name] = list:encode() [default_list_name] = list:encode(),
} },
}) })
eq(list.items, { eq(list.items, {
@ -105,7 +103,6 @@ describe("harpoon", function()
{ value = file_name_2, context = { row = row_2, col = col_2 } }, { value = file_name_2, context = { row = row_2, col = col_2 } },
{ value = file_name_1, context = { row = row_1, col = col_1 } }, { value = file_name_1, context = { row = row_1, col = col_1 } },
}) })
end) end)
it("ui - display resolve", function() it("ui - display resolve", function()
@ -115,8 +112,8 @@ describe("harpoon", function()
-- split string on / -- split string on /
local parts = vim.split(item.value, "/") local parts = vim.split(item.value, "/")
return parts[#parts] return parts[#parts]
end end,
} },
}) })
local file_names = { local file_names = {
@ -190,8 +187,14 @@ describe("harpoon", function()
eq({ eq({
{ value = file_names[1], context = { row = 4, col = 2 } }, { value = file_names[1], context = { row = 4, col = 2 } },
{ value = file_names[4], context = { row = 4, col = 2 } }, { value = file_names[4], context = { row = 4, col = 2 } },
{value = "/tmp/harpoon-test-other-file-1", context = {row = 1, col = 0}}, {
{value = "/tmp/harpoon-test-other-file-2", context = {row = 1, col = 0}}, value = "/tmp/harpoon-test-other-file-1",
context = { row = 1, col = 0 },
},
{
value = "/tmp/harpoon-test-other-file-2",
context = { row = 1, col = 0 },
},
}, list.items) }, list.items)
table.remove(displayed, 3) table.remove(displayed, 3)
@ -201,8 +204,10 @@ describe("harpoon", function()
eq({ eq({
{ value = file_names[1], context = { row = 4, col = 2 } }, { value = file_names[1], context = { row = 4, col = 2 } },
{ value = file_names[4], context = { row = 4, col = 2 } }, { value = file_names[4], context = { row = 4, col = 2 } },
{value = "/tmp/harpoon-test-other-file-2", context = {row = 1, col = 0}}, {
value = "/tmp/harpoon-test-other-file-2",
context = { row = 1, col = 0 },
},
}, list.items) }, list.items)
end) end)
end) end)

View File

@ -4,7 +4,6 @@ local eq = assert.are.same
describe("list", function() describe("list", function()
it("decode", function() it("decode", function()
local config = Config.merge_config({ local config = Config.merge_config({
foo = { foo = {
decode = function(item) decode = function(item)
@ -18,8 +17,8 @@ describe("list", function()
display = function(item) display = function(item)
return table.concat(item.value, "---") return table.concat(item.value, "---")
end end,
} },
}) })
local list_config = Config.get_config(config, "foo") local list_config = Config.get_config(config, "foo")
@ -32,4 +31,3 @@ describe("list", function()
}) })
end) end)
end) end)

View File

@ -3,7 +3,6 @@ local utils = require("harpoon2.test.utils")
local eq = assert.are.same local eq = assert.are.same
describe("harpoon", function() describe("harpoon", function()
before_each(utils.before_each(os.tmpname())) before_each(utils.before_each(os.tmpname()))
it("open the ui without any items in the list", function() it("open the ui without any items in the list", function()
@ -24,5 +23,3 @@ describe("harpoon", function()
eq(harpoon.ui.win_id, nil) eq(harpoon.ui.win_id, nil)
end) end)
end) end)

View File

@ -21,8 +21,8 @@ function M.before_each(name)
settings = { settings = {
key = function() key = function()
return "testies" return "testies"
end end,
} },
}) })
end end
end end

View File

@ -56,7 +56,8 @@ function HarpoonUI:_create_window()
end end
local height = 8 local height = 8
local borderchars = { "", "", "", "", "", "", "", "" } local borderchars =
{ "", "", "", "", "", "", "", "" }
local bufnr = vim.api.nvim_create_buf(false, false) local bufnr = vim.api.nvim_create_buf(false, false)
local _, popup_info = popup.create(bufnr, { local _, popup_info = popup.create(bufnr, {
title = "Harpoon", title = "Harpoon",
@ -73,11 +74,7 @@ function HarpoonUI:_create_window()
self.win_id = win_id self.win_id = win_id
vim.api.nvim_win_set_option(self.win_id, "number", true) vim.api.nvim_win_set_option(self.win_id, "number", true)
vim.api.nvim_win_set_option( vim.api.nvim_win_set_option(win_id, "winhl", "Normal:HarpoonBorder")
win_id,
"winhl",
"Normal:HarpoonBorder"
)
return win_id, bufnr return win_id, bufnr
end end
@ -86,7 +83,6 @@ local count = 0
---@param list? HarpoonList ---@param list? HarpoonList
function HarpoonUI:toggle_quick_menu(list) function HarpoonUI:toggle_quick_menu(list)
count = count + 1 count = count + 1
if list == nil or self.win_id ~= nil then if list == nil or self.win_id ~= nil then