feat: I am going to marry that data and that list

This commit is contained in:
mpaulson 2023-11-03 15:18:57 -06:00
parent 9e60c39057
commit 543f3a16e1
5 changed files with 369 additions and 6 deletions

View File

@ -88,3 +88,7 @@ frecency = later feature likely, but great idea
// i don't understand this one // i don't understand this one
harpoon -> qfix : qfix -> harpoon harpoon -> qfix : qfix -> harpoon
harpoon.list("notehu") -> HarpoonList
harpoon.list().add()

100
lua/harpoon/data.lua Normal file
View File

@ -0,0 +1,100 @@
local Path = require("plenary.path")
local data_path = vim.fn.stdpath("data")
local full_data_path = string.format("%s/harpoon2.json", data_path)
local M = {}
function M.set_data_path(path)
full_data_path = path
end
local function has_keys(t)
for _ in pairs(t) do
return true
end
return false
end
--- @alias RawData {[string]: string[]}
--- @class Data
--- @field seen {[string]: boolean}
--- @field _data RawData
--- @field has_error boolean
-- 1. load the data
-- 2. keep track of the lists requested
-- 3. sync save
local Data = {}
Data.__index = Data
---@param data any
local function write_data(data)
Path:new(full_data_path):write_data(vim.json.encode(data))
end
---@return RawData
local function read_data()
return vim.json.decode(Path:new(full_data_path):read())
end
---@return Harpoon
function Data:new()
local ok, data = pcall(read_data)
return setmetatable({
_data = data,
has_error = not ok,
seen = {}
}, self)
end
---@param name string
---@return string[]
function Data:data(name)
if self.has_error then
error("Harpoon: there was an error reading the data file, cannot read data")
end
return self._data[name] or {}
end
---@param name string
---@param values string[]
function Data:update(name, values)
if self.has_error then
error("Harpoon: there was an error reading the data file, cannot update")
end
self.seen[name] = true
self._data[name] = values
end
function Data:sync()
if self.has_error then
return
end
if not has_keys(self.seen) then
return
end
local ok, data = pcall(read_data)
if not ok then
error("Harpoon: unable to sync data, error reading data file")
end
for k, v in pairs(self._data) do
data[k] = v
end
pcall(write_data, data)
end
M.Data = Data
return M

View File

@ -1,13 +1,41 @@
local Data = require("harpoon.data")
local Config = require("harpoon.config")
-- setup -- setup
-- read from a config file -- read from a config file
-- --
local M = {} local DEFAULT_LIST = "__harpoon_files"
---@param c HarpoonConfig ---@class Harpoon
function config(c) ---@field config HarpoonConfig
---@field data Data
local Harpoon = {}
Harpoon.__index = Harpoon
---@return Harpoon
function Harpoon:new()
local config = Config.get_default_config()
return setmetatable({
config = config,
data = Data:new()
}, self)
end end
return M ---@param partial_config HarpoonPartialConfig
---@return Harpoon
function Harpoon:setup(partial_config)
self.config = Config.merge_config(partial_config, self.config)
return self
end
---@param list string?
---@return HarpoonList
function Harpoon:list(name)
name = name or DEFAULT_LIST
end
return Harpoon:new()

View File

@ -52,11 +52,14 @@ end
function HarpoonList: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
local found = false local found = false
for _, in_table in ipairs(self.items) do for _, in_table in ipairs(self.items) do
found = config.display(in_table, v) if config.display(in_table) == v then
break found = true
break
end
end end
if not found then if not found then

228
lua/harpoon/some.json Normal file
View File

@ -0,0 +1,228 @@
{
"key": [
nothuntoehuntoehuntoehuntoehuntoehun
oentuhnoteuhntoehuouenhtuoenhtuonhtuoenhtuoenht 0,nthouenhtouenthoue
1,
2,
3,
4,
5,
6,
7,
8,
9,
10,
11,
1,
3,
5,
7,
9,
11,
13,
15,
17,
19,
21,
23,
2,
5,
8,
11,
14,
17,
20,
23,
26,
29,
32,
35,
3,
7,
11,
15,
19,
23,
27,
31,
35,
39,
43,
47,
4,
9,
14,
19,
24,
29,
34,
39,
44,
49,
54,
59,
5,
11,
17,
23,
29,
35,
41,
47,
53,
59,
65,
71,
6,
13,
20,
27,
34,
41,
48,
55,
62,
69,
76,
83,
7,
15,
23,
31,
39,
47,
55,
63,
71,
79,
87,
95,
8,
17,
26,
35,
44,
53,
62,
71,
80,
89,
98,
107,
9,
19,
29,
39,
49,
59,
69,
79,
89,
99,
109,
119,
10,
21,
32,
43,
54,
65,
76,
87,
98,
109,
120,
131,
11,
23,
35,
47,
59,
71,
83,
95,
107,
119,
131,
143,
12,
25,
38,
51,
64,
77,
90,
103,
116,
129,
142,
155,
13,
27,
41,
55,
69,
83,
97,
111,
125,
139,
153,
167,
14,
29,
44,
59,
74,
89,
104,
119,
134,
149,
164,
179,
15,
31,
47,
63,
79,
95,
111,
127,
143,
159,
175,
191,
16,
33,
50,
67,
84,
101,
118,
135,
152,
169,
186,
203,
]
}