From 543f3a16e143db22204934a415e242770da815e3 Mon Sep 17 00:00:00 2001 From: mpaulson Date: Fri, 3 Nov 2023 15:18:57 -0600 Subject: [PATCH] feat: I am going to marry that data and that list --- HARPOON2.md | 4 + lua/harpoon/data.lua | 100 ++++++++++++++++++ lua/harpoon/init.lua | 36 ++++++- lua/harpoon/list.lua | 7 +- lua/harpoon/some.json | 228 ++++++++++++++++++++++++++++++++++++++++++ 5 files changed, 369 insertions(+), 6 deletions(-) create mode 100644 lua/harpoon/data.lua create mode 100644 lua/harpoon/some.json diff --git a/HARPOON2.md b/HARPOON2.md index e9df042..d282069 100644 --- a/HARPOON2.md +++ b/HARPOON2.md @@ -88,3 +88,7 @@ frecency = later feature likely, but great idea // i don't understand this one harpoon -> qfix : qfix -> harpoon + +harpoon.list("notehu") -> HarpoonList +harpoon.list().add() + diff --git a/lua/harpoon/data.lua b/lua/harpoon/data.lua new file mode 100644 index 0000000..2d07d1e --- /dev/null +++ b/lua/harpoon/data.lua @@ -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 + diff --git a/lua/harpoon/init.lua b/lua/harpoon/init.lua index d7de3c8..dc6e7b7 100644 --- a/lua/harpoon/init.lua +++ b/lua/harpoon/init.lua @@ -1,13 +1,41 @@ +local Data = require("harpoon.data") +local Config = require("harpoon.config") -- setup -- read from a config file -- -local M = {} +local DEFAULT_LIST = "__harpoon_files" ----@param c HarpoonConfig -function config(c) +---@class Harpoon +---@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 -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() diff --git a/lua/harpoon/list.lua b/lua/harpoon/list.lua index 6ba1700..7874dd7 100644 --- a/lua/harpoon/list.lua +++ b/lua/harpoon/list.lua @@ -52,11 +52,14 @@ end function HarpoonList:resolve_displayed(displayed) local not_found = {} local config = get_config(self.config, self.name) + for _, v in ipairs(displayed) do local found = false for _, in_table in ipairs(self.items) do - found = config.display(in_table, v) - break + if config.display(in_table) == v then + found = true + break + end end if not found then diff --git a/lua/harpoon/some.json b/lua/harpoon/some.json new file mode 100644 index 0000000..e9fa520 --- /dev/null +++ b/lua/harpoon/some.json @@ -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, + + ] +} +