Skip to content

Commit 464930d

Browse files
committed
Made saving/loading conditional on enable_persistence,
1 parent 6a0b187 commit 464930d

File tree

2 files changed

+67
-52
lines changed

2 files changed

+67
-52
lines changed

lua/nvim-tree.lua

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -511,7 +511,11 @@ local DEFAULT_OPTS = { -- BEGIN_DEFAULT_OPTS
511511
watcher = false,
512512
},
513513
},
514-
} -- END_DEFAULT_OPTS
514+
marks = {
515+
enable_persistence = false,
516+
save_path = nil, -- nil will default to stdpath("data") .. "/nvim-tree-bookmarks.json"
517+
},
518+
}-- END_DEFAULT_OPTS
515519

516520
local function merge_options(conf)
517521
return vim.tbl_deep_extend("force", DEFAULT_OPTS, conf or {})

lua/nvim-tree/marks/init.lua

Lines changed: 62 additions & 51 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,49 @@ local utils = require("nvim-tree.utils")
1111
local Class = require("nvim-tree.classic")
1212
local DirectoryNode = require("nvim-tree.node.directory")
1313

14+
local function get_save_path(opts)
15+
return opts.marks.save_path or (vim.fn.stdpath("data") .. "/nvim-tree-bookmarks.json")
16+
end
17+
18+
local function save_bookmarks(marks, opts)
19+
if not opts.marks.enable_persistence then
20+
return
21+
end
22+
23+
local storepath = get_save_path(opts)
24+
local file = io.open(storepath, "w")
25+
if file then
26+
local data = {}
27+
for path, _ in pairs(marks) do
28+
table.insert(data, path)
29+
end
30+
print(storepath)
31+
file:write(vim.json.encode(data))
32+
file:close()
33+
end
34+
end
35+
36+
local function load_bookmarks(opts)
37+
if not opts.marks.enable_persistence then
38+
return {}
39+
end
40+
local storepath = get_save_path(opts)
41+
local file = io.open(storepath, "r")
42+
if file then
43+
local content = file:read("*all")
44+
file:close()
45+
if content and content ~= "" then
46+
local data = vim.json.decode(content)
47+
local marks = {}
48+
for _, path in ipairs(data) do
49+
marks[path] = true -- or reconstruct node if needed
50+
end
51+
return marks
52+
end
53+
end
54+
return {}
55+
end
56+
1457
---@class (exact) Marks: Class
1558
---@field private explorer Explorer
1659
---@field private marks table<string, Node> by absolute path
@@ -22,6 +65,11 @@ local Marks = Class:extend()
2265
---@class (exact) MarksArgs
2366
---@field explorer Explorer
2467

68+
function Marks:new(args)
69+
self.explorer = args.explorer
70+
self.marks = load_bookmarks(self.explorer.opts) or {}
71+
end
72+
2573
---Clear all marks and reload if watchers disabled
2674
---@private
2775
function Marks:clear_reload()
@@ -38,6 +86,20 @@ function Marks:clear()
3886
self.explorer.renderer:draw()
3987
end
4088

89+
function Marks:toggle(node)
90+
if node.absolute_path == nil then
91+
return
92+
end
93+
94+
if self:get(node) then
95+
self.marks[node.absolute_path] = nil
96+
else
97+
self.marks[node.absolute_path] = node
98+
end
99+
save_bookmarks(self.marks, self.explorer.opts)
100+
self.explorer.renderer:draw()
101+
end
102+
41103
---Return node if marked
42104
---@public
43105
---@param node Node
@@ -219,57 +281,6 @@ function Marks:navigate_next()
219281
self:navigate(false)
220282
end
221283

222-
223-
local function save_bookmarks(marks)
224-
local storepath = vim.fn.stdpath("data") .. "/nvim-tree-bookmarks.json"
225-
local file = io.open(storepath, "w")
226-
if file then
227-
local data = {}
228-
for path, _ in pairs(marks) do
229-
table.insert(data, path)
230-
end
231-
file:write(vim.fn.json_encode(data))
232-
file:close()
233-
end
234-
end
235-
236-
local function load_bookmarks()
237-
local storepath = vim.fn.stdpath("data") .. "/nvim-tree-bookmarks.json"
238-
local file = io.open(storepath, "r")
239-
if file then
240-
local content = file:read("*all")
241-
file:close()
242-
if content and content ~= "" then
243-
local data = vim.fn.json_decode(content)
244-
local marks = {}
245-
for _, path in ipairs(data) do
246-
marks[path] = true -- or reconstruct node if needed
247-
end
248-
return marks
249-
end
250-
end
251-
return {}
252-
end
253-
254-
function Marks:new(args)
255-
self.explorer = args.explorer
256-
self.marks = load_bookmarks() or {}
257-
end
258-
259-
function Marks:toggle(node)
260-
if node.absolute_path == nil then
261-
return
262-
end
263-
264-
if self:get(node) then
265-
self.marks[node.absolute_path] = nil
266-
else
267-
self.marks[node.absolute_path] = node
268-
end
269-
270-
save_bookmarks(self.marks)
271-
self.explorer.renderer:draw()
272-
end
273284
---Prompts for selection of a marked node, sorted by absolute paths.
274285
---A folder will be focused, a file will be opened.
275286
---@public

0 commit comments

Comments
 (0)