Restore explorer structure based on cwd with session #2696
Replies: 2 comments
---@module 'snacks'
-- Test keymap
vim.keymap.set("n", "<F1>", function()
Snacks.explorer()
end)
---@param picker snacks.Picker
---@param key string
local save_explorer_state = function(picker, key)
local dirs = {}
for _, item in ipairs(picker:items()) do
if item.dir then
table.insert(dirs, {
file = item.file,
open = item.open,
})
end
end
local state = {
cwd = picker:cwd(),
cursor = picker:current().idx,
dirs = dirs,
}
local all_states = vim.g.EXPLORER_STATES or {}
all_states[key] = state
vim.g.EXPLORER_STATES = all_states
vim.cmd.wshada()
vim.notify("Explorer state saved", vim.log.levels.DEBUG)
end
local Tree = require("snacks.explorer.tree")
---@param picker snacks.Picker
---@param key string
local load_explorer_state = function(picker, key)
vim.cmd.rshada()
local all_states = vim.g.EXPLORER_STATES or {}
local state = all_states[key]
if not state then
return
end
picker:set_cwd(state.cwd)
for _, item in ipairs(state.dirs) do
if item.open then
Tree:open(item.file)
else
Tree:close(item.file)
end
end
picker.list:set_target(state.cursor)
picker:action("explorer_update")
vim.notify("Explorer state loaded", vim.log.levels.DEBUG)
end
local root_key = function()
-- NOTE: Uses LazyVim utility
-- NOTE: State per root
return LazyVim.root() or "unknown"
end
return {
"folke/snacks.nvim",
---@type snacks.Config
opts = {
picker = {
sources = {
explorer = {
actions = {
save_state = function(picker)
save_explorer_state(picker, root_key())
end,
load_state = function(picker)
load_explorer_state(picker, root_key())
end,
},
win = {
list = {
keys = {
["<F2>"] = { "save_state", mode = { "i", "n" } },
["<F3>"] = { "load_state", mode = { "i", "n" } },
},
},
},
},
},
},
},
} |
0 replies
Answer selected by
pczern
|
Also created this solution, that generates files similar to session files for every return {
"folke/snacks.nvim",
opts = {
picker = {
sources = {
explorer = {
on_close = function(picker)
local Tree = require("snacks.explorer.tree")
local root = Tree:find(picker:cwd())
local function remove_parent(node)
if next(node.children) == nil then
return
end
local newNode =
{ children = node.children, expanded = node.expanded, path = node.path }
for index, child in pairs(node.children) do
newNode.children[index] = remove_parent(child)
end
return newNode
end
local json = vim.fn.json_encode(remove_parent(root))
local state_dir = vim.fn.stdpath("state") .. "/snacks_explorer/"
vim.fn.mkdir(state_dir, "p")
local file = state_dir
.. vim.fn.fnamemodify(picker:cwd(), ":p"):gsub("[/\\]", "%%")
.. ".json"
vim.fn.writefile({ json }, file, "s")
end,
on_show = function(picker)
local path = vim.fn.stdpath("state")
.. "/snacks_explorer/"
.. vim.fn.fnamemodify(picker:cwd(), ":p"):gsub("[/\\]", "%%")
.. ".json"
if vim.fn.filereadable(path) ~= 1 then
return nil
end
local file = io.open(path, "r")
local content = file:read("*a")
file:close()
local ok, state = pcall(vim.json.decode, content)
if not ok then
return nil
end
local Tree = require("snacks.explorer.tree")
local function open_node_recursively(node)
if node.expanded then
if vim.loop.fs_stat(node.path) ~= nil then
Tree:open(node.path)
end
end
if next(node.children) ~= nil then
for _, child in pairs(node.children) do
open_node_recursively(child)
end
end
end
open_node_recursively(state)
end,
},
},
},
},
} |
0 replies
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Uh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.
When reopening neovim for a working directory I'd like the folder structure of the explorer to restore as it was when I previously had neovim open.
I added https://github.com/folke/persistence.nvim to my config to store the session state of the editor.
I set
sessionoptionslike thissessionoptions=buffers,curdir,folds,help,tabpages,winsize,terminalremovingblank.Example:
I open some folders.
I reload the session that was saved by executing the lua command.
Only one folder (the one with the open file) from the session was reopened (I set `follow_file` in explorer settings). The other two folders `docs` and `extras` were not opened, although I opened them in the same saved session.
All reactions