|
| 1 | +local logger = require("neogit.logger") |
| 2 | +local config = require("neogit.config") |
| 3 | +local Path = require("plenary.path") |
| 4 | + |
| 5 | +local M = {} |
| 6 | + |
| 7 | +M.loaded = false |
| 8 | + |
| 9 | +local function log(message) |
| 10 | + logger.debug("State: " .. message .. ": '" .. M.path:absolute() .. "'") |
| 11 | +end |
| 12 | + |
| 13 | +---@return Path |
| 14 | +function M.filepath() |
| 15 | + local base_path = vim.fn.stdpath("state") .. "/neogit/" |
| 16 | + local filename = "state" |
| 17 | + |
| 18 | + if config.values.use_per_project_settings then |
| 19 | + filename = vim.loop.cwd():gsub("/", "%%") |
| 20 | + end |
| 21 | + |
| 22 | + return Path:new(base_path .. filename) |
| 23 | +end |
| 24 | + |
| 25 | +---Initializes state |
| 26 | +function M.setup() |
| 27 | + if M.loaded then |
| 28 | + return |
| 29 | + end |
| 30 | + |
| 31 | + M.path = M.filepath() |
| 32 | + M.loaded = true |
| 33 | + M.state = M.read() |
| 34 | + log("Loaded") |
| 35 | +end |
| 36 | + |
| 37 | +---@return boolean |
| 38 | +function M.enabled() |
| 39 | + return M.loaded and config.values.remember_settings |
| 40 | +end |
| 41 | + |
| 42 | +---Reads state from disk |
| 43 | +---@return table |
| 44 | +function M.read() |
| 45 | + if not M.enabled() then |
| 46 | + return {} |
| 47 | + end |
| 48 | + |
| 49 | + if not M.path:exists() then |
| 50 | + log("Creating file") |
| 51 | + M.path:touch { parents = true } |
| 52 | + M.path:write(vim.mpack.encode {}, "w") |
| 53 | + end |
| 54 | + |
| 55 | + log("Reading file") |
| 56 | + return vim.mpack.decode(M.path:read()) |
| 57 | +end |
| 58 | + |
| 59 | +---Writes state to disk |
| 60 | +function M.write() |
| 61 | + if not M.enabled() then |
| 62 | + return |
| 63 | + end |
| 64 | + |
| 65 | + log("Writing file") |
| 66 | + M.path:write(vim.mpack.encode(M.state), "w") |
| 67 | +end |
| 68 | + |
| 69 | +---Construct a cache-key from a table |
| 70 | +---@param key_table table |
| 71 | +---@return string |
| 72 | +local function gen_key(key_table) |
| 73 | + return table.concat(key_table, "--") |
| 74 | +end |
| 75 | + |
| 76 | +---Set option and write to disk |
| 77 | +---@param key table |
| 78 | +---@param value any |
| 79 | +function M.set(key, value) |
| 80 | + if not M.enabled() then |
| 81 | + return |
| 82 | + end |
| 83 | + |
| 84 | + if not vim.tbl_contains(config.values.ignored_settings, gen_key(key)) then |
| 85 | + M.state[gen_key(key)] = value |
| 86 | + M.write() |
| 87 | + end |
| 88 | +end |
| 89 | + |
| 90 | +---Get option. If value isn't set, return provided default. |
| 91 | +---@param key table |
| 92 | +---@param default any |
| 93 | +---@return any |
| 94 | +function M.get(key, default) |
| 95 | + if not M.enabled() then |
| 96 | + return default |
| 97 | + end |
| 98 | + |
| 99 | + return M.state[gen_key(key)] or default |
| 100 | +end |
| 101 | + |
| 102 | +---Reset current state, removing whats written to disk |
| 103 | +function M._reset() |
| 104 | + log("Reset file") |
| 105 | + M.path:write(vim.mpack.encode {}, "w") |
| 106 | + M.state = {} |
| 107 | +end |
| 108 | + |
| 109 | +return M |
0 commit comments