From db0b8a4cd1a057d37c6da953cebee9c93c317325 Mon Sep 17 00:00:00 2001 From: Andrew Date: Thu, 7 Mar 2024 11:35:34 -0500 Subject: [PATCH] very rough wip of saving between sessions --- lua/before.lua | 113 +++++++++++++++++++++++++++++++++++++++++++++---- 1 file changed, 105 insertions(+), 8 deletions(-) diff --git a/lua/before.lua b/lua/before.lua index a4c1266..ee3dcc9 100644 --- a/lua/before.lua +++ b/lua/before.lua @@ -23,14 +23,22 @@ local function is_regular_buffer(bufnr) end local function should_remove(location) - return not bufvalid(location.bufnr) or not within_bounds(location.bufnr, location.line) or + local invalid_bufnr = not bufvalid(location.bufnr) or not within_bounds(location.bufnr, location.line) or not is_regular_buffer(location.bufnr) + + -- then try to look up filename + local invalid_file = not vim.fn.filereadable(location.file) + + -- return invalid_bufnr or invalid_file? + -- TODO: don't really know what to do here + return false end function M.track_edit() local bufnr = vim.api.nvim_get_current_buf() + local file = vim.api.nvim_buf_get_name(bufnr) local pos = vim.api.nvim_win_get_cursor(0) - local location = { bufnr = bufnr, line = pos[1], col = pos[2] } + local location = { file = file, bufnr = bufnr, line = pos[1], col = pos[2] } local prev_location = M.edit_locations[#M.edit_locations] if is_regular_buffer(bufnr) then @@ -49,6 +57,8 @@ function M.track_edit() table.remove(M.edit_locations, 1) M.cursor = M.max_entries end + + M.save_cache() end local function find_backwards_jump(currentLocation) @@ -106,14 +116,22 @@ end function M.jump_to_last_edit() if #M.edit_locations > 0 then local bufnr = vim.api.nvim_get_current_buf() + local file = vim.api.nvim_buf_get_name(bufnr) local pos = vim.api.nvim_win_get_cursor(0) - local current = { bufnr = bufnr, line = pos[1], col = pos[2] } + local current = { file = file, bufnr = bufnr, line = pos[1], col = pos[2] } local new_location = find_backwards_jump(current) - if new_location then - vim.api.nvim_win_set_buf(0, new_location.bufnr) + vim.print(vim.inspect(new_location)) + + if new_location and new_location.file ~= file then + vim.api.nvim_command("e " .. new_location.file) vim.api.nvim_win_set_cursor(0, { new_location.line, new_location.col }) + else + if new_location then + vim.api.nvim_win_set_buf(0, new_location.bufnr) + vim.api.nvim_win_set_cursor(0, { new_location.line, new_location.col }) + end end else print("No edit locations stored.") @@ -123,20 +141,92 @@ end function M.jump_to_next_edit() if #M.edit_locations > 0 then local bufnr = vim.api.nvim_get_current_buf() + local file = vim.api.nvim_buf_get_name(bufnr) local pos = vim.api.nvim_win_get_cursor(0) - local current = { bufnr = bufnr, line = pos[1], col = pos[2] } + local current = { file = file, bufnr = bufnr, line = pos[1], col = pos[2] } local new_location = find_forward_jump(current) - if new_location then - vim.api.nvim_win_set_buf(0, new_location.bufnr) + if new_location and new_location.file ~= file then + vim.api.nvim_command("e " .. new_location.file) vim.api.nvim_win_set_cursor(0, { new_location.line, new_location.col }) + else + if new_location then + vim.api.nvim_win_set_buf(0, new_location.bufnr) + vim.api.nvim_win_set_cursor(0, { new_location.line, new_location.col }) + end end else print("No edit locations stored.") end end +function M.cache_path() + return vim.fn.stdpath("cache") .. "/before" +end + +function M.save_cache() + local path = M.cache_path() + + if vim.fn.isdirectory(path) == 0 then + vim.fn.mkdir(path, "p") + end + + local cache = M.serialize_cache(M.edit_locations) + + vim.fn.writefile(cache, path .. "/cache") +end + +function M.load_cache() + local path = M.cache_path() + + local success, data = pcall(vim.fn.readfile, path .. "/cache") + -- print("success", success) + if success then + M.edit_locations = M.deserialize_cache(data) + M.cursor = #M.edit_locations + else + -- do nothing? + -- M.edit_locations = {} + end + + print("loaded cache", vim.inspect(M.edit_locations)) +end + +function M.serialize_cache(content) + local lines = {} + for _, location in ipairs(content) do + table.insert(lines, string.format("%s:%s:%s:%s", location.file, location.bufnr, location.line, location.col)) + end + + return lines +end + +-- Loop through it and serialize it to a string: +function M.deserialize_cache(lines) + local content = {} + for _, line in ipairs(lines) do + local parts = vim.fn.split(line, ":") + + -- ignore repeated entries + local tableContains = function(tab, val) + for index, value in ipairs(tab) do + if value.file == val.file and value.bufnr == val.bufnr and value.line == val.line and value.col == val.col then + return true + end + end + return false + end + + local entry = { file = parts[1], bufnr = tonumber(parts[2]), line = tonumber(parts[3]), col = tonumber(parts[4]) } + if not tableContains(content, entry) then + table.insert(content, entry) + end + end + + return content +end + M.defaults = { history_size = 10 } @@ -152,6 +242,13 @@ function M.setup(opts) require('before').track_edit() end, }) + + vim.api.nvim_create_autocmd({ "VimEnter" }, { + pattern = '*', + callback = function() + M.load_cache() + end, + }) end return M