Skip to content

Commit

Permalink
NeoVim: Replace auto-sessions with custom
Browse files Browse the repository at this point in the history
I did not fully used the rmagatti/auto-session plugin. The only reason
why I had this plugin is for my custom worktree script to restore
the previous buffer when switching between worktrees. Although, this
plugin restored every single buffer that I previously had, which feels
unnecessary to me, because I only want to see the latest one where I
left off.

For this I've created a custom little script which does exactly this and
nothing more.
  • Loading branch information
ducktordanny committed Jul 27, 2024
1 parent 16bf3a4 commit f31d34f
Show file tree
Hide file tree
Showing 4 changed files with 110 additions and 13 deletions.
88 changes: 88 additions & 0 deletions config/nvim/lua/ducktordanny/custom/last_buffer.lua
Original file line number Diff line number Diff line change
@@ -0,0 +1,88 @@
local Path = require "plenary.path"

local M = {}

---@class CursorPosition
---@field row number
---@field column number

---@class WorktreeConfigItem
---@field file string
---@field cursor_position CursorPosition

---@alias WorktreeConfig table<string, WorktreeConfigItem>

M._config_file_path = vim.fn.expand "$HOME/.local/share/nvim/worktree.json"

---@return WorktreeConfig
M._get_config = function()
local path = Path:new(M._config_file_path)
local file_exists = path:exists()
if not file_exists then
return {}
end
local content = path:read()
if content == "" or not vim.startswith(content, "{") then
return {}
end
local config = vim.json.decode(content)
return config
end

---@param config WorktreeConfig
M._set_config = function(config)
local path = Path:new(M._config_file_path)
local content = vim.json.encode(config)
path:write(content, "w")
end

---@return WorktreeConfigItem | nil
M._get_current_buffer_details = function()
-- TODO: gsub could be a variable, but rn idc
local file = vim.api.nvim_buf_get_name(0):gsub("oil://", "")
if not file or file == "" then
return
end
local cursor_positions = vim.api.nvim_win_get_cursor(0)
if vim.startswith(file, "fugitive") then
local wins = vim.api.nvim_list_wins()
if #wins == 1 then
return
end
local current_win_id = vim.api.nvim_get_current_win()
vim.api.nvim_win_close(current_win_id, true)
return M._get_current_buffer_details()
end

return {
file = file,
cursor_position = {
row = cursor_positions[1],
column = cursor_positions[2],
},
}
end

M.save_for_current_cwd = function()
local current_buffer_details = M._get_current_buffer_details()
if not current_buffer_details then
return
end
local config = M._get_config()
local cwd = vim.fn.getcwd()
config[cwd] = current_buffer_details
M._set_config(config)
end

M.restore_by_cwd = function()
local config = M._get_config()
local cwd = vim.fn.getcwd()
local restore_data = config[cwd]
if not restore_data then
return
end
vim.cmd("edit " .. restore_data.file)
vim.api.nvim_win_set_cursor(0, { restore_data.cursor_position.row, restore_data.cursor_position.column })
end

return M
23 changes: 21 additions & 2 deletions config/nvim/lua/ducktordanny/custom/worktree.lua
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,21 @@ local actions = require "telescope.actions"
local action_state = require "telescope.actions.state"
local themes = require "telescope.themes"

local last_buffer = require "ducktordanny.custom.last_buffer"

local M = {}

---@class WorktreePaths
---@field tree_paths string[]
---@field bare_path string
---@field current_tree string
---@field branch_names string[]

---@type string | nil
M._current_worktree = nil

---@param no_current_tree_text string | nil The text for current tree to return if no worktree is selected.
---@return WorktreePaths
M._get_worktree_paths = function(no_current_tree_text)
no_current_tree_text = no_current_tree_text or ""
local project_path = vim.fn.getcwd()
Expand Down Expand Up @@ -44,6 +55,10 @@ M._get_worktree_paths = function(no_current_tree_text)
}
end

---@param tree_paths string[]
---@param bare_path string
---@param branch_names string[]
---@return string[]
M._get_formated_tree_list = function(tree_paths, bare_path, branch_names)
local tree_names = {}

Expand All @@ -55,17 +70,20 @@ M._get_formated_tree_list = function(tree_paths, bare_path, branch_names)
return tree_names
end

---@param tree_path string
M._handle_worktree_switch = function(tree_path)
vim.cmd ":wa"
vim.cmd ":LspStop"
vim.cmd ":SessionSave"
last_buffer.save_for_current_cwd()
vim.cmd ":%bd"
vim.cmd("cd" .. tree_path)
vim.cmd ":SessionRestore"
last_buffer.restore_by_cwd()
vim.cmd ":LspStart"
M._current_worktree = nil
end

---Returns the currently selected worktree (from cache).
---@return string
M.get_current_worktree = function()
if M._current_worktree == nil then
local worktree_details = M._get_worktree_paths()
Expand All @@ -74,6 +92,7 @@ M.get_current_worktree = function()
return M._current_worktree
end

---@param opts any Telescope opts
M.select_worktree = function(opts)
opts = opts or {}
local trees = M._get_worktree_paths "-"
Expand Down
11 changes: 0 additions & 11 deletions config/nvim/lua/ducktordanny/plugins/auto-sessions.lua

This file was deleted.

1 change: 1 addition & 0 deletions config/nvim/lua/ducktordanny/plugins/telescope.lua
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
return {
{ "nvim-lua/plenary.nvim" },
{
"nvim-telescope/telescope.nvim",
version = "*",
Expand Down

0 comments on commit f31d34f

Please sign in to comment.