-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
NeoVim: Replace auto-sessions with custom
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
1 parent
16bf3a4
commit f31d34f
Showing
4 changed files
with
110 additions
and
13 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,5 @@ | ||
return { | ||
{ "nvim-lua/plenary.nvim" }, | ||
{ | ||
"nvim-telescope/telescope.nvim", | ||
version = "*", | ||
|