forked from yetone/avante.nvim
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
refactor(history): use XDG_DATA_STATE as source dir (yetone#209)
Signed-off-by: Aaron Pham <contact@aarnphm.xyz>
- Loading branch information
Showing
4 changed files
with
58 additions
and
54 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters