-
-
Notifications
You must be signed in to change notification settings - Fork 179
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
193786e
commit b876553
Showing
2 changed files
with
121 additions
and
0 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
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,50 @@ | ||
---Handle Vim session-saving logic when buffers contain a toggleterm terminal. | ||
|
||
---Write `lines` of text to-disk at `path`. | ||
--- | ||
---@param lines string[] The text to write. | ||
---@param path string An absolute path on-disk to append `lines` onto. | ||
local function _append_lines(commands, path) | ||
local handler = io.open(path, "a") | ||
|
||
if not handler then | ||
vim.api.nvim_err_writeln('Unable to write to "%s" Sessionx.vim.', path) | ||
|
||
return | ||
end | ||
|
||
for _, line in ipairs(commands) do | ||
handler:write(line .. "\n") | ||
end | ||
|
||
handler:close() | ||
end | ||
|
||
---@return string # Get the full path where a Sessionx.vim file should be written to-disk. | ||
local function _get_sessionx_path() | ||
local path = vim.v.this_session | ||
|
||
if not path then | ||
return nil | ||
end | ||
|
||
return vim.fn.fnamemodify(path, ":h") .. "/Sessionx.vim" | ||
end | ||
|
||
---Serialize all toggleterm terminals to a Sessionx.vim file so they can be restored later. | ||
local function _save_terminals() | ||
local commands = require("toggleterm.terminal").get_all_terminal_commands() | ||
local path = _get_sessionx_path() | ||
|
||
if not commands then | ||
-- No new terminals needed to be saved | ||
-- TODO: It's unclear if this should be removed or somehow cleared | ||
vim.fn.delete(path) | ||
|
||
return | ||
end | ||
|
||
_append_lines(commands, path) | ||
end | ||
|
||
vim.api.nvim_create_autocmd("SessionWritePost", {callback=_save_terminals}) |