Skip to content

Commit

Permalink
feat: add resession extension for AstroCore
Browse files Browse the repository at this point in the history
  • Loading branch information
mehalter committed Jul 21, 2023
1 parent 18e9189 commit 5217973
Showing 1 changed file with 44 additions and 0 deletions.
44 changes: 44 additions & 0 deletions lua/resession/extensions/astronvim.lua
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
local M = {}

M.on_save = function()
-- initiate astronvim data
local data = { bufnrs = {}, tabs = {} }

local buf_utils = require "astrocore.buffer"

data.current_buf = buf_utils.current_buf
data.last_buf = buf_utils.last_buf

-- save tab scoped buffers and buffer numbers from buffer name
for new_tabpage, tabpage in ipairs(vim.api.nvim_list_tabpages()) do
data.tabs[new_tabpage] = vim.t[tabpage].bufs
for _, bufnr in ipairs(data.tabs[new_tabpage]) do
data.bufnrs[vim.api.nvim_buf_get_name(bufnr)] = bufnr
end
end

return data
end

M.on_load = function(data)
-- create map from old buffer numbers to new buffer numbers
local new_bufnrs = {}
for _, bufnr in ipairs(vim.api.nvim_list_bufs()) do
local bufname = vim.api.nvim_buf_get_name(bufnr)
if bufname and data.bufnrs[bufname] then new_bufnrs[data.bufnrs[bufname]] = bufnr end
end
-- build new tab scoped buffer lists
for tabpage, tabs in pairs(data.tabs) do
vim.t[tabpage].bufs = vim.tbl_map(function(bufnr) return new_bufnrs[bufnr] end, tabs)
end

local buf_utils = require "astrocore.buffer"
buf_utils.current_buf = new_bufnrs[data.current_buf]
buf_utils.last_buf = new_bufnrs[data.last_buf]

require("astrocore.utils").event "BufsUpdated"

if vim.fn.bufnr() ~= buf_utils.current_buf then vim.cmd.b(buf_utils.current_buf) end
end

return M

0 comments on commit 5217973

Please sign in to comment.