diff options
| author | Thomas Vanbesien <tvanbesi@proton.me> | 2026-06-08 17:06:55 +0200 |
|---|---|---|
| committer | Thomas Vanbesien <tvanbesi@proton.me> | 2026-06-08 17:06:55 +0200 |
| commit | 8b4aa7c441fe614fe45afc4fde263c8274c5da0a (patch) | |
| tree | 3f5f7e518c729fc44fcaf1ccef152f1e9ba533b3 /plugin | |
| parent | 62df2097896f3bb87dfdf186ee2c0a14a43a36d5 (diff) | |
| download | nvim-config-8b4aa7c441fe614fe45afc4fde263c8274c5da0a.tar.gz nvim-config-8b4aa7c441fe614fe45afc4fde263c8274c5da0a.zip | |
refactor(nvim): make session sidecar generic (not only for tabpages)
Diffstat (limited to 'plugin')
| -rw-r--r-- | plugin/50-session.lua | 44 |
1 files changed, 29 insertions, 15 deletions
diff --git a/plugin/50-session.lua b/plugin/50-session.lua index 5dc033f..9d1a8bf 100644 --- a/plugin/50-session.lua +++ b/plugin/50-session.lua @@ -9,11 +9,31 @@ if not vim.uv.fs_stat(session_dir) then vim.notify("Sessions save directory created at " .. session_dir) end --- :mksession doesn't persist tab-local vars (t:tabname), so we write tab names to a sidecar --- JSON file alongside the .vim session. Names are stored positionally because :mksession --- recreates tabs in their original order, making the index stable across reloads. -local function tabnames_path(path) - return (path:gsub("%.vim$", "")) .. ".tabnames.json" +-- :mksession doesn't persist everything we want (e.g. tab-local t:tabname), so we keep a +-- sidecar JSON object alongside the .vim session for extra data. Tab names live under the +-- `tabnames` key, stored positionally because :mksession recreates tabs in their original +-- order, making the index stable across reloads. +local function sidecar_path(path) + return (path:gsub("%.vim$", "")) .. ".json" +end + +local function read_sidecar(path) + local f = io.open(sidecar_path(path), "r") + if not f then + return {} + end + local content = f:read("*a") + f:close() + local ok, data = pcall(vim.json.decode, content) + return (ok and type(data) == "table") and data or {} +end + +local function write_sidecar(path, data) + local f = io.open(sidecar_path(path), "w") + if f then + f:write(vim.json.encode(data)) + f:close() + end end local function save_session(path) @@ -24,21 +44,15 @@ local function save_session(path) local ok, name = pcall(vim.api.nvim_tabpage_get_var, tp, "tabname") names[i] = (ok and type(name) == "string") and name or "" end - local f = io.open(tabnames_path(path), "w") - if f then - f:write(vim.json.encode(names)) - f:close() - end + write_sidecar(path, { tabnames = names }) end local function load_session(path) vim.cmd.source(path) - local f = io.open(tabnames_path(path), "r") - if not f then + local names = read_sidecar(path).tabnames + if not names then return end - local names = vim.json.decode(f:read("*a")) - f:close() for i, tp in ipairs(vim.api.nvim_list_tabpages()) do if names[i] and names[i] ~= "" then vim.api.nvim_tabpage_set_var(tp, "tabname", names[i]) @@ -54,7 +68,7 @@ end local function delete_session(path) vim.fs.rm(path) - local sidecar = tabnames_path(path) + local sidecar = sidecar_path(path) if vim.uv.fs_stat(sidecar) then vim.fs.rm(sidecar) end |
