-
-
Notifications
You must be signed in to change notification settings - Fork 619
feat(#1851): persist bookmarks #3033
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: master
Are you sure you want to change the base?
Changes from 7 commits
51d262b
6ec9fc1
318f0ba
6a0b187
464930d
fbfa65b
69f5a63
7108587
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -208,8 +208,8 @@ function Filters:prepare(project) | |
|
||
local explorer = require("nvim-tree.core").get_explorer() | ||
if explorer then | ||
for _, node in pairs(explorer.marks:list()) do | ||
status.bookmarks[node.absolute_path] = node.type | ||
for key, node in pairs(explorer.marks.marks) do | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This is private and should not be accessed, see CI failure Please use I know this is not your code, but we can clean this up by using Edit: it appears that |
||
status.bookmarks[key] = node | ||
end | ||
end | ||
|
||
|
Original file line number | Diff line number | Diff line change | ||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|
|
@@ -11,6 +11,45 @@ local utils = require("nvim-tree.utils") | |||||||||||
local Class = require("nvim-tree.classic") | ||||||||||||
local DirectoryNode = require("nvim-tree.node.directory") | ||||||||||||
|
||||||||||||
local function get_save_path(opts) | ||||||||||||
return opts.marks.save_path or (vim.fn.stdpath("data") .. "/nvim-tree-bookmarks.json") | ||||||||||||
end | ||||||||||||
|
||||||||||||
local function save_bookmarks(marks, opts) | ||||||||||||
if not opts.marks.enable_persistence then | ||||||||||||
return | ||||||||||||
end | ||||||||||||
|
||||||||||||
local storepath = get_save_path(opts) | ||||||||||||
local file = io.open(storepath, "w") | ||||||||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||||||||
if file then | ||||||||||||
local data = {} | ||||||||||||
for path, _ in pairs(marks) do | ||||||||||||
table.insert(data, path) | ||||||||||||
end | ||||||||||||
file:write(vim.json.encode(data)) | ||||||||||||
file:close() | ||||||||||||
end | ||||||||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
Print
|
||||||||||||
end | ||||||||||||
|
||||||||||||
local function load_bookmarks(opts) | ||||||||||||
local storepath = get_save_path(opts) | ||||||||||||
local file = io.open(storepath, "r") | ||||||||||||
if file then | ||||||||||||
local content = file:read("*all") | ||||||||||||
file:close() | ||||||||||||
if content and content ~= "" then | ||||||||||||
local data = vim.json.decode(content) | ||||||||||||
local marks = {} | ||||||||||||
for _, path in ipairs(data) do | ||||||||||||
marks[path] = true -- or reconstruct node if needed | ||||||||||||
end | ||||||||||||
return marks | ||||||||||||
end | ||||||||||||
end | ||||||||||||
return {} | ||||||||||||
end | ||||||||||||
|
||||||||||||
---@class (exact) Marks: Class | ||||||||||||
---@field private explorer Explorer | ||||||||||||
---@field private marks table<string, Node> by absolute path | ||||||||||||
|
@@ -26,8 +65,15 @@ local Marks = Class:extend() | |||||||||||
---@param args MarksArgs | ||||||||||||
function Marks:new(args) | ||||||||||||
self.explorer = args.explorer | ||||||||||||
|
||||||||||||
self.marks = {} | ||||||||||||
if self.explorer.opts.marks.enable_persistence then | ||||||||||||
local ok, loaded_marks = pcall(load_bookmarks, self.explorer.opts) | ||||||||||||
if ok then | ||||||||||||
self.marks = loaded_marks | ||||||||||||
else | ||||||||||||
notify.warn("Failed to load bookmarks: " .. loaded_marks) | ||||||||||||
end | ||||||||||||
end | ||||||||||||
end | ||||||||||||
|
||||||||||||
---Clear all marks and reload if watchers disabled | ||||||||||||
|
@@ -59,6 +105,12 @@ function Marks:toggle(node) | |||||||||||
self.marks[node.absolute_path] = node | ||||||||||||
end | ||||||||||||
|
||||||||||||
if self.explorer.opts.marks.enable_persistence then | ||||||||||||
local ok, err = pcall(save_bookmarks, self.marks, self.explorer.opts) | ||||||||||||
if not ok then | ||||||||||||
notify.warn("Failed to save bookmarks: " .. err) | ||||||||||||
end | ||||||||||||
end | ||||||||||||
self.explorer.renderer:draw() | ||||||||||||
end | ||||||||||||
|
||||||||||||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Rather than a toggle and path, we can use just one option that is boolean or string. This is consistent with other options.
See proposed help
We then add the type explitictly so that it may be validated. Add to
ACCEPTED_TYPES
: