@@ -11,6 +11,49 @@ local utils = require("nvim-tree.utils")
11
11
local Class = require (" nvim-tree.classic" )
12
12
local DirectoryNode = require (" nvim-tree.node.directory" )
13
13
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
+
14
57
--- @class (exact ) Marks : Class
15
58
--- @field private explorer Explorer
16
59
--- @field private marks table<string , Node> by absolute path
@@ -22,6 +65,11 @@ local Marks = Class:extend()
22
65
--- @class (exact ) MarksArgs
23
66
--- @field explorer Explorer
24
67
68
+ function Marks :new (args )
69
+ self .explorer = args .explorer
70
+ self .marks = load_bookmarks (self .explorer .opts ) or {}
71
+ end
72
+
25
73
--- Clear all marks and reload if watchers disabled
26
74
--- @private
27
75
function Marks :clear_reload ()
@@ -38,6 +86,20 @@ function Marks:clear()
38
86
self .explorer .renderer :draw ()
39
87
end
40
88
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
+
41
103
--- Return node if marked
42
104
--- @public
43
105
--- @param node Node
@@ -219,57 +281,6 @@ function Marks:navigate_next()
219
281
self :navigate (false )
220
282
end
221
283
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
273
284
--- Prompts for selection of a marked node, sorted by absolute paths.
274
285
--- A folder will be focused, a file will be opened.
275
286
--- @public
0 commit comments