avante.nvim/lua/avante/history.lua
yetone d5a4db8321
Revert tokenizers (#423)
* Revert "fix: add missing "with" in README"

This reverts commit bf1e7f1f17c8b82687b15c41e5a8104ea03f70cf.

* Revert "feat: tokenizers (#407)"

This reverts commit d2095ba267abbfe169582708a4449e609aa9709b.
2024-08-31 22:45:31 +08:00

50 lines
1.3 KiB
Lua

local fn, api = vim.fn, vim.api
local Path = require("plenary.path")
local Config = require("avante.config")
local M = {}
local H = {}
---@param bufnr integer
---@return string
H.filename = function(bufnr)
local code_buf_name = api.nvim_buf_get_name(bufnr)
-- Replace path separators with double underscores
local path_with_separators = fn.substitute(code_buf_name, "/", "__", "g")
-- Replace other non-alphanumeric characters with single underscores
return fn.substitute(path_with_separators, "[^A-Za-z0-9._]", "_", "g") .. ".json"
end
---@param bufnr integer
---@return Path
M.get = function(bufnr)
return Path:new(Config.history.storage_path):joinpath(H.filename(bufnr))
end
---@param bufnr integer
M.load = function(bufnr)
local history_file = M.get(bufnr)
if history_file:exists() then
local content = history_file:read()
return content ~= nil and vim.json.decode(content) or {}
end
return {}
end
---@param bufnr integer
---@param history table
M.save = function(bufnr, history)
local history_file = M.get(bufnr)
history_file:write(vim.json.encode(history), "w")
end
M.setup = function()
local history_dir = Path:new(Config.history.storage_path)
if not history_dir:exists() then
history_dir:mkdir({ parents = true })
end
end
return M