-- What the person chose in this editor, remembered across restarts: at two levels, for everywhere -- and for a working directory, the directory's own winning. One JSON file in Neovim's state -- directory. Only the editor's own things are here (the mode, the effort, suggestions and AI on or -- off, the bars and their widths): the models are remembered by the LLeMbas CLI and the account. local M = {} --- The keys that are kept, and the kind of value each takes. M.KEYS = { mode = "string", effort = "string", autocomplete = "boolean", ai = "boolean", top = "boolean", bottom = "boolean", tree = "boolean", chat = "boolean", tree_width = "number", chat_width = "number", hidden_files = "boolean", } M.MAX_DIRECTORIES = 500 M.enabled = true function M.path() return vim.fn.stdpath("state") .. "/llembas/settings.json" end local function empty() return { version = 1, global = {}, directories = {}, terminals = {}, seq = 0 } end local function read() local f = io.open(M.path(), "r") if not f then return empty() end local text = f:read("*a") f:close() local ok, data = pcall(vim.json.decode, text, { luanil = { object = true, array = true } }) if not ok or type(data) ~= "table" then return empty() end for _, k in ipairs({ "global", "directories", "terminals" }) do if type(data[k]) ~= "table" or vim.islist(data[k]) and next(data[k]) ~= nil then data[k] = {} end end data.seq = tonumber(data.seq) or 0 return data end local function write(data) local path = M.path() vim.fn.mkdir(vim.fs.dirname(path), "p", "0700") -- An empty table must be written as an object, or the next read finds a list. for _, k in ipairs({ "global", "directories", "terminals" }) do if next(data[k]) == nil then data[k] = vim.empty_dict() end end local tmp = ("%s.%d.tmp"):format(path, vim.uv.os_getpid()) local f = io.open(tmp, "w") if not f then return false end f:write(vim.json.encode(data)) f:close() pcall(vim.uv.fs_chmod, tmp, 384) -- 0600 local ok = vim.uv.fs_rename(tmp, path) if not ok then pcall(os.remove, tmp) end return ok and true or false end --- The directory's name as the store keys it: absolute, links resolved where it exists. function M.dir(cwd) cwd = cwd or vim.fn.getcwd() return vim.uv.fs_realpath(cwd) or vim.fs.normalize(cwd) end local function known(level) local out = {} for k, v in pairs(type(level) == "table" and level or {}) do if k ~= "seq" and M.KEYS[k] and type(v) == M.KEYS[k] then out[k] = v end end return out end --- What is remembered for `cwd`: the machine-wide values, with the directory's own over them. --- Second value: the directory's own entry alone. function M.get(cwd) if not M.enabled then return {}, {} end local data = read() local own = known(data.directories[M.dir(cwd)]) return vim.tbl_extend("force", known(data.global), own), own end --- Remember `key = value` for the directory (`value` nil forgets it there). function M.set(key, value, cwd) if not M.enabled or not M.KEYS[key] then return false end if value ~= nil and type(value) ~= M.KEYS[key] then return false end local data = read() local dir = M.dir(cwd) local entry = type(data.directories[dir]) == "table" and data.directories[dir] or {} if entry[key] == value then return true end entry[key] = value data.seq = data.seq + 1 entry.seq = data.seq data.directories[dir] = entry -- The directories written longest ago go first. local names = vim.tbl_keys(data.directories) if #names > M.MAX_DIRECTORIES then table.sort(names, function(a, b) return (tonumber(data.directories[a].seq) or 0) < (tonumber(data.directories[b].seq) or 0) end) for i = 1, #names - M.MAX_DIRECTORIES do data.directories[names[i]] = nil end end return write(data) end --- Make what holds in `cwd` now the machine-wide default: what a directory without a choice of --- its own starts with. function M.make_default(cwd) if not M.enabled then return false end local data = read() local now = vim.tbl_extend("force", known(data.global), known(data.directories[M.dir(cwd)])) data.global = now return write(data) end --- Forget what the directory chose for itself: it takes the machine-wide values again. function M.forget(cwd) if not M.enabled then return false end local data = read() if data.directories[M.dir(cwd)] == nil then return true end data.directories[M.dir(cwd)] = nil return write(data) end --- Whether the key chords were seen to arrive from the terminal named `id` (nil: never seen). function M.terminal(id) if not M.enabled or not id or id == "" then return nil end return read().terminals[id] == true or nil end function M.saw_terminal(id) if not M.enabled or not id or id == "" then return end local data = read() if data.terminals[id] == true then return end data.terminals[id] = true write(data) end return M