feat: config add now produces a correct HarpoonFileListItem

This commit is contained in:
mpaulson 2023-11-03 13:34:08 -06:00
parent 2aa20a84da
commit 9e60c39057
4 changed files with 91 additions and 25 deletions

View File

@ -37,7 +37,7 @@ harpoon.setup({
# question mark: what does it take to support custom things in here? # question mark: what does it take to support custom things in here?
# potentially subject to change # potentially subject to change
add = function() => void add = function() HarpoonListItem
} }
frecency = { frecency = {

View File

@ -1,6 +1,7 @@
local M = {} local M = {}
---@alias HarpoonListItem {value: any, context: any} ---@alias HarpoonListItem {value: any, context: any}
---@alias HarpoonListFileItem {value: string, context: {row: number, col: number}}
---@class HarpoonPartialConfigItem ---@class HarpoonPartialConfigItem
---@field encode? (fun(list_item: HarpoonListItem): string) ---@field encode? (fun(list_item: HarpoonListItem): string)
@ -20,7 +21,13 @@ local M = {}
---@field settings HarpoonSettings ---@field settings HarpoonSettings
---@field [string] HarpoonPartialConfigItem ---@field [string] HarpoonPartialConfigItem
---@return HarpoonConfig ---@class HarpoonPartialConfig
---@field default HarpoonPartialConfigItem?
---@field settings HarpoonSettings?
---@field [string] 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 {})
end end
@ -55,9 +62,26 @@ function M.get_default_config()
return list_item.value return list_item.value
end, end,
---@param list_item HarpoonListItem ---@param file_item HarpoonListFileItem
select = function(list_item) select = function(file_item)
error("please implement select") if file_item == nil then
return
end
local bufnr = vim.fn.bufnr(file_item.value)
local set_position = false
if bufnr == -1 then
set_position = true
bufnr = vim.fn.bufnr(file_item.value, true)
end
vim.api.nvim_set_current_buf(bufnr)
if set_position then
vim.api.nvim_win_set_cursor(0, {
file_item.context.row or 1,
file_item.context.col or 0
})
end
end, end,
---@param list_item_a HarpoonListItem ---@param list_item_a HarpoonListItem
@ -66,14 +90,23 @@ function M.get_default_config()
return list_item_a.value == list_item_b.value return list_item_a.value == list_item_b.value
end, end,
---@return HarpoonListItem
add = function() add = function()
error("please implement add") local name = vim.api.nvim_buf_get_name(vim.api.nvim_get_current_buf())
local pos = vim.api.nvim_win_get_cursor(0)
return {
value = name,
context = {
row = pos[1],
col = pos[2],
}
}
end, end,
} }
} }
end end
---@param partial_config HarpoonConfig ---@param partial_config HarpoonPartialConfig
---@param latest_config HarpoonConfig? ---@param latest_config HarpoonConfig?
---@return HarpoonConfig ---@return HarpoonConfig
function M.merge_config(partial_config, latest_config) function M.merge_config(partial_config, latest_config)

View File

@ -2,19 +2,19 @@ local get_config = require "harpoon.config".get_config
-- TODO: Define the config object -- TODO: Define the config object
--- @class Item --- @class HarpoonItem
--- @field value string --- @field value string
--- @field context any --- @field context any
--- create a table object to be new'd --- create a table object to be new'd
--- @class List --- @class HarpoonList
--- @field config any --- @field config any
--- @field name string --- @field name string
--- @field items Item[] --- @field items HarpoonItem[]
local List = {} local HarpoonList = {}
List.__index = List HarpoonList.__index = HarpoonList
function List:new(config, name, items) function HarpoonList:new(config, name, items)
return setmetatable({ return setmetatable({
items = items, items = items,
config = config, config = config,
@ -22,15 +22,15 @@ function List:new(config, name, items)
}, self) }, self)
end end
function List:push(item) function HarpoonList:push(item)
table.insert(self.items, item) table.insert(self.items, item)
end end
function List:addToFront(item) function HarpoonList:addToFront(item)
table.insert(self.items, 1, item) table.insert(self.items, 1, item)
end end
function List:remove(item) function HarpoonList:remove(item)
for i, v in ipairs(self.items) do for i, v in ipairs(self.items) do
if get_config(self.config, self.name)(v, item) then if get_config(self.config, self.name)(v, item) then
table.remove(self.items, i) table.remove(self.items, i)
@ -39,17 +39,17 @@ function List:remove(item)
end end
end end
function List:removeAt(index) function HarpoonList:removeAt(index)
table.remove(self.items, index) table.remove(self.items, index)
end end
function List:get(index) function HarpoonList:get(index)
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 List:resolve_displayed(displayed) function HarpoonList:resolve_displayed(displayed)
local not_found = {} local not_found = {}
local config = get_config(self.config, self.name) local config = get_config(self.config, self.name)
for _, v in ipairs(displayed) do for _, v in ipairs(displayed) do
@ -70,7 +70,7 @@ function List:resolve_displayed(displayed)
end end
--- @return string[] --- @return string[]
function List:display() function HarpoonList:display()
local out = {} local out = {}
local config = get_config(self.config, self.name) local config = get_config(self.config, self.name)
for _, v in ipairs(self.items) do for _, v in ipairs(self.items) do
@ -81,7 +81,7 @@ function List:display()
end end
--- @return string[] --- @return string[]
function List:encode() function HarpoonList:encode()
local out = {} local out = {}
local config = get_config(self.config, self.name) local config = get_config(self.config, self.name)
for _, v in ipairs(self.items) do for _, v in ipairs(self.items) do
@ -91,11 +91,11 @@ function List:encode()
return out return out
end end
--- @return List --- @return HarpoonList
--- @param config HarpoonConfig --- @param config HarpoonConfig
--- @param name string --- @param name string
--- @param items string[] --- @param items string[]
function List.decode(config, name, items) function HarpoonList.decode(config, name, items)
local list_items = {} local list_items = {}
local c = get_config(config, name) local c = get_config(config, name)
@ -103,9 +103,9 @@ function List.decode(config, name, items)
table.insert(list_items, c.decode(item)) table.insert(list_items, c.decode(item))
end end
return List:new(config, name, list_items) return HarpoonList:new(config, name, list_items)
end end
return List return HarpoonList

View File

@ -0,0 +1,33 @@
local List = require("harpoon.list")
local Config = require("harpoon.config")
local eq = assert.are.same
describe("config", function()
it("default.add", function()
local config = Config.get_default_config()
local config_item = Config.get_config(config, "foo")
local bufnr = vim.fn.bufnr("/tmp/harpoon-test", true)
vim.api.nvim_set_current_buf(bufnr)
vim.api.nvim_buf_set_text(0, 0, 0, 0, 0, {
"foo",
"bar",
"baz",
"qux"
})
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)