Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 8 additions & 0 deletions doc/nvim-tree-lua.txt
Original file line number Diff line number Diff line change
Expand Up @@ -1911,6 +1911,13 @@ Config: experimental *nvim-tree-config-experimental*
In the event of a problem please disable the experiment and raise an
issue.

Fields: ~
• {session_restore_nvim}? (`boolean`, default: `false`) Restore
nvim-tree buffers when restoring vim sessions
(requires 0.13+). To restore the working
directory, ensure that |'sessionoptions'|
includes "globals"

==============================================================================
Config: log *nvim-tree-config-log*

Expand Down Expand Up @@ -2216,6 +2223,7 @@ Following is the default configuration, see |nvim_tree.config| for details. >lua
persist = false,
},
experimental = {
session_restore_nvim = false
},
log = {
enable = false,
Expand Down
4 changes: 4 additions & 0 deletions lua/nvim-tree/_meta/config/experimental.lua
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,10 @@ error("Cannot require a meta file")
---
---@class nvim_tree.config.experimental
---
---Restore nvim-tree buffers when restoring vim sessions (requires 0.13+).
---To restore the working directory, ensure that |'sessionoptions'| includes "globals"
---(default: `false`)
---@field session_restore_nvim? boolean
--Example below for future reference:
--
--Buffers opened by nvim-tree will use with relative paths instead of absolute.
Expand Down
52 changes: 52 additions & 0 deletions lua/nvim-tree/autocmd.lua
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,58 @@ function M.global()
end,
})

if vim.fn.has("nvim-0.13") == 1 and config.g.experimental.session_restore_nvim then
vim.api.nvim_create_autocmd("SessionWritePre", { ---@diagnostic disable-line: param-type-mismatch
group = augroup_id,
callback = function()
local cwd = require("nvim-tree.core").get_cwd()
vim.g.NvimTreeState = vim.json.encode({ cwd = cwd })
end,
})

vim.api.nvim_create_autocmd("SessionLoadPost", {
group = augroup_id,
callback = function()
local api = require("nvim-tree.api").tree

---@type table<integer,boolean>
local tabs = {}

local session_state = vim.json.decode(vim.g.NvimTreeState or "{}") or {}
local path = session_state and session_state.cwd or nil

-- Save tabs with leftover nvim-tree buffers
for _, tab in ipairs(vim.api.nvim_list_tabpages()) do
for _, win in ipairs(vim.api.nvim_tabpage_list_wins(tab)) do
local buf = vim.api.nvim_win_get_buf(win)
if api.is_tree_buf(buf) then
tabs[tab] = true
end
end
end

-- The new window may inherit options from the current one if we don't schedule
vim.schedule(function()
api.close_in_all_tabs()

for tab, _ in pairs(tabs) do
if vim.api.nvim_tabpage_is_valid(tab) then
local win = vim.api.nvim_tabpage_get_win(tab)

-- Invoke `open` from each tab's current window to prevent having to switch tabs
if vim.api.nvim_win_is_valid(win) then
vim.api.nvim_win_call(win, function()
api.open({ path = path })
end)
end
end
end
end)
end,
})
end


if config.g.tab.sync.open then
vim.api.nvim_create_autocmd("TabEnter", {
group = augroup_id,
Expand Down
1 change: 1 addition & 0 deletions lua/nvim-tree/config.lua
Original file line number Diff line number Diff line change
Expand Up @@ -297,6 +297,7 @@ M.d = { -- config-default-start
persist = false,
},
experimental = {
session_restore_nvim = false
},
log = {
enable = false,
Expand Down