mirror of
https://github.com/ThePrimeagen/harpoon.git
synced 2025-07-14 01:50:27 +00:00
feat: config add now produces a correct HarpoonFileListItem
This commit is contained in:
parent
2aa20a84da
commit
9e60c39057
@ -37,7 +37,7 @@ harpoon.setup({
|
||||
|
||||
# question mark: what does it take to support custom things in here?
|
||||
# potentially subject to change
|
||||
add = function() => void
|
||||
add = function() HarpoonListItem
|
||||
}
|
||||
|
||||
frecency = {
|
||||
|
@ -1,6 +1,7 @@
|
||||
local M = {}
|
||||
|
||||
---@alias HarpoonListItem {value: any, context: any}
|
||||
---@alias HarpoonListFileItem {value: string, context: {row: number, col: number}}
|
||||
|
||||
---@class HarpoonPartialConfigItem
|
||||
---@field encode? (fun(list_item: HarpoonListItem): string)
|
||||
@ -20,7 +21,13 @@ local M = {}
|
||||
---@field settings HarpoonSettings
|
||||
---@field [string] HarpoonPartialConfigItem
|
||||
|
||||
---@return HarpoonConfig
|
||||
---@class HarpoonPartialConfig
|
||||
---@field default HarpoonPartialConfigItem?
|
||||
---@field settings HarpoonSettings?
|
||||
---@field [string] HarpoonPartialConfigItem
|
||||
|
||||
|
||||
---@return HarpoonPartialConfigItem
|
||||
function M.get_config(config, name)
|
||||
return vim.tbl_extend("force", {}, config.default, config[name] or {})
|
||||
end
|
||||
@ -55,9 +62,26 @@ function M.get_default_config()
|
||||
return list_item.value
|
||||
end,
|
||||
|
||||
---@param list_item HarpoonListItem
|
||||
select = function(list_item)
|
||||
error("please implement select")
|
||||
---@param file_item HarpoonListFileItem
|
||||
select = function(file_item)
|
||||
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,
|
||||
|
||||
---@param list_item_a HarpoonListItem
|
||||
@ -66,14 +90,23 @@ function M.get_default_config()
|
||||
return list_item_a.value == list_item_b.value
|
||||
end,
|
||||
|
||||
---@return HarpoonListItem
|
||||
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
|
||||
|
||||
---@param partial_config HarpoonConfig
|
||||
---@param partial_config HarpoonPartialConfig
|
||||
---@param latest_config HarpoonConfig?
|
||||
---@return HarpoonConfig
|
||||
function M.merge_config(partial_config, latest_config)
|
||||
|
@ -2,19 +2,19 @@ local get_config = require "harpoon.config".get_config
|
||||
|
||||
-- TODO: Define the config object
|
||||
|
||||
--- @class Item
|
||||
--- @class HarpoonItem
|
||||
--- @field value string
|
||||
--- @field context any
|
||||
|
||||
--- create a table object to be new'd
|
||||
--- @class List
|
||||
--- @class HarpoonList
|
||||
--- @field config any
|
||||
--- @field name string
|
||||
--- @field items Item[]
|
||||
local List = {}
|
||||
--- @field items HarpoonItem[]
|
||||
local HarpoonList = {}
|
||||
|
||||
List.__index = List
|
||||
function List:new(config, name, items)
|
||||
HarpoonList.__index = HarpoonList
|
||||
function HarpoonList:new(config, name, items)
|
||||
return setmetatable({
|
||||
items = items,
|
||||
config = config,
|
||||
@ -22,15 +22,15 @@ function List:new(config, name, items)
|
||||
}, self)
|
||||
end
|
||||
|
||||
function List:push(item)
|
||||
function HarpoonList:push(item)
|
||||
table.insert(self.items, item)
|
||||
end
|
||||
|
||||
function List:addToFront(item)
|
||||
function HarpoonList:addToFront(item)
|
||||
table.insert(self.items, 1, item)
|
||||
end
|
||||
|
||||
function List:remove(item)
|
||||
function HarpoonList:remove(item)
|
||||
for i, v in ipairs(self.items) do
|
||||
if get_config(self.config, self.name)(v, item) then
|
||||
table.remove(self.items, i)
|
||||
@ -39,17 +39,17 @@ function List:remove(item)
|
||||
end
|
||||
end
|
||||
|
||||
function List:removeAt(index)
|
||||
function HarpoonList:removeAt(index)
|
||||
table.remove(self.items, index)
|
||||
end
|
||||
|
||||
function List:get(index)
|
||||
function HarpoonList:get(index)
|
||||
return self.items[index]
|
||||
end
|
||||
|
||||
--- much inefficiencies. dun care
|
||||
---@param displayed string[]
|
||||
function List:resolve_displayed(displayed)
|
||||
function HarpoonList:resolve_displayed(displayed)
|
||||
local not_found = {}
|
||||
local config = get_config(self.config, self.name)
|
||||
for _, v in ipairs(displayed) do
|
||||
@ -70,7 +70,7 @@ function List:resolve_displayed(displayed)
|
||||
end
|
||||
|
||||
--- @return string[]
|
||||
function List:display()
|
||||
function HarpoonList:display()
|
||||
local out = {}
|
||||
local config = get_config(self.config, self.name)
|
||||
for _, v in ipairs(self.items) do
|
||||
@ -81,7 +81,7 @@ function List:display()
|
||||
end
|
||||
|
||||
--- @return string[]
|
||||
function List:encode()
|
||||
function HarpoonList:encode()
|
||||
local out = {}
|
||||
local config = get_config(self.config, self.name)
|
||||
for _, v in ipairs(self.items) do
|
||||
@ -91,11 +91,11 @@ function List:encode()
|
||||
return out
|
||||
end
|
||||
|
||||
--- @return List
|
||||
--- @return HarpoonList
|
||||
--- @param config HarpoonConfig
|
||||
--- @param name string
|
||||
--- @param items string[]
|
||||
function List.decode(config, name, items)
|
||||
function HarpoonList.decode(config, name, items)
|
||||
local list_items = {}
|
||||
local c = get_config(config, name)
|
||||
|
||||
@ -103,9 +103,9 @@ function List.decode(config, name, items)
|
||||
table.insert(list_items, c.decode(item))
|
||||
end
|
||||
|
||||
return List:new(config, name, list_items)
|
||||
return HarpoonList:new(config, name, list_items)
|
||||
end
|
||||
|
||||
|
||||
return List
|
||||
return HarpoonList
|
||||
|
||||
|
33
lua/harpoon/test/config_spec.lua
Normal file
33
lua/harpoon/test/config_spec.lua
Normal 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)
|
||||
|
||||
|
Loading…
x
Reference in New Issue
Block a user