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(
"autocmd BufLeave <buffer> ++nested ++once silent lua require('harpoon2').ui:toggle_quick_menu()"
)
end
---@param bufnr number

View File

@ -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 {})
@ -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,
@ -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
@ -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
@ -158,8 +157,8 @@ function M.get_default_config()
end
end,
autocmds = {"BufLeave"},
}
autocmds = { "BufLeave" },
},
}
end

View File

@ -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
@ -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
@ -68,9 +67,8 @@ function Data:new()
return setmetatable({
_data = data,
has_error = not ok,
seen = {}
seen = {},
}, self)
end
---@param key string
@ -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
@ -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
@ -136,7 +138,6 @@ function Data:sync()
end
end
M.Data = Data
return M

View File

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

View File

@ -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
@ -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
@ -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
@ -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
@ -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
@ -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)
@ -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
@ -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
@ -187,6 +211,4 @@ function HarpoonList.decode(list_config, name, items)
return HarpoonList:new(list_config, name, list_items)
end
return HarpoonList

View File

@ -1,4 +1,3 @@
---@alias HarpoonListener fun(type: string, args: any[] | any | nil): nil
---@class HarpoonListeners
@ -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

View File

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

View File

@ -14,9 +14,9 @@ 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, {
@ -24,9 +24,7 @@ describe("config", function()
context = {
row = 3,
col = 1,
}
},
})
end)
end)

View File

@ -6,7 +6,6 @@ local eq = assert.are.same
local be = utils.before_each(os.tmpname())
describe("harpoon", function()
before_each(function()
be()
harpoon = require("harpoon2")
@ -20,7 +19,7 @@ describe("harpoon", function()
"foo",
"bar",
"baz",
"qux"
"qux",
}, row, col)
local list = harpoon:list():append()
@ -28,19 +27,18 @@ describe("harpoon", function()
"foo",
"bar",
"baz",
"qux"
"qux",
}, row, col)
vim.api.nvim_set_current_buf(target_buf)
vim.api.nvim_win_set_cursor(0, {row + 1, col})
vim.api.nvim_win_set_cursor(0, { row + 1, col })
vim.api.nvim_set_current_buf(other_buf)
local expected = {
{value = file_name, context = {row = row + 1, col = col}},
{ value = file_name, context = { row = row + 1, col = col } },
}
eq(expected, list.items)
end)
it("full harpoon add sync cycle", function()
@ -52,7 +50,7 @@ describe("harpoon", function()
"foo",
"bar",
"baz",
"qux"
"qux",
}, row, col)
local list = harpoon:list()
@ -61,8 +59,8 @@ describe("harpoon", function()
eq(harpoon:dump(), {
testies = {
[default_list_name] = list:encode()
}
[default_list_name] = list:encode(),
},
})
end)
@ -88,13 +86,13 @@ describe("harpoon", function()
eq(harpoon:dump(), {
testies = {
[default_list_name] = list:encode()
}
[default_list_name] = list:encode(),
},
})
eq(list.items, {
{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_2, context = { row = row_2, col = col_2 } },
{ value = file_name_1, context = { row = row_1, col = col_1 } },
})
harpoon:list():append()
@ -102,10 +100,9 @@ describe("harpoon", function()
harpoon:list():prepend()
eq(list.items, {
{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_2, context = { row = row_2, col = col_2 } },
{ value = file_name_1, context = { row = row_1, col = col_1 } },
})
end)
it("ui - display resolve", function()
@ -115,8 +112,8 @@ describe("harpoon", function()
-- split string on /
local parts = vim.split(item.value, "/")
return parts[#parts]
end
}
end,
},
})
local file_names = {
@ -149,8 +146,8 @@ describe("harpoon", function()
list:resolve_displayed(displayed)
eq(list.items, {
{value = file_names[1], context = {row = 4, col = 2}},
{value = file_names[4], context = {row = 4, col = 2}},
{ value = file_names[1], context = { row = 4, col = 2 } },
{ value = file_names[4], context = { row = 4, col = 2 } },
})
end)
@ -188,10 +185,16 @@ describe("harpoon", function()
list:resolve_displayed(displayed)
eq({
{value = file_names[1], 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 = file_names[1], 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 },
},
}, list.items)
table.remove(displayed, 3)
@ -199,10 +202,12 @@ describe("harpoon", function()
list:resolve_displayed(displayed)
eq({
{value = file_names[1], 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 = file_names[1], 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 },
},
}, list.items)
end)
end)

View File

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

View File

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

View File

@ -21,15 +21,15 @@ function M.before_each(name)
settings = {
key = function()
return "testies"
end
}
end,
},
})
end
end
function M.clean_files()
for _, bufnr in ipairs(M.created_files) do
vim.api.nvim_buf_delete(bufnr, {force = true})
vim.api.nvim_buf_delete(bufnr, { force = true })
end
M.created_files = {}
@ -42,7 +42,7 @@ function M.create_file(name, contents, row, col)
vim.api.nvim_set_current_buf(bufnr)
vim.api.nvim_buf_set_text(0, 0, 0, 0, 0, contents)
if row then
vim.api.nvim_win_set_cursor(0, {row, col})
vim.api.nvim_win_set_cursor(0, { row, col })
end
table.insert(M.created_files, bufnr)

View File

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