Skip to content
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

WIP: Support Persist Bookmarks #3033

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
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
4 changes: 2 additions & 2 deletions lua/nvim-tree/explorer/filters.lua
Original file line number Diff line number Diff line change
Expand Up @@ -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
status.bookmarks[key] = node
end
end

Expand Down
75 changes: 51 additions & 24 deletions lua/nvim-tree/marks/init.lua
Original file line number Diff line number Diff line change
Expand Up @@ -22,14 +22,6 @@ local Marks = Class:extend()
---@class (exact) MarksArgs
---@field explorer Explorer

---@protected
---@param args MarksArgs
function Marks:new(args)
self.explorer = args.explorer

self.marks = {}
end

---Clear all marks and reload if watchers disabled
---@private
function Marks:clear_reload()
Expand All @@ -46,22 +38,6 @@ function Marks:clear()
self.explorer.renderer:draw()
end

---@public
---@param node Node
function Marks:toggle(node)
if node.absolute_path == nil then
return
end

if self:get(node) then
self.marks[node.absolute_path] = nil
else
self.marks[node.absolute_path] = node
end

self.explorer.renderer:draw()
end

---Return node if marked
---@public
---@param node Node
Expand Down Expand Up @@ -243,6 +219,57 @@ function Marks:navigate_next()
self:navigate(false)
end


local function save_bookmarks(marks)
local storepath = vim.fn.stdpath("data") .. "/nvim-tree-bookmarks.json"
local file = io.open(storepath, "w")
if file then
local data = {}
for path, _ in pairs(marks) do
table.insert(data, path)
end
file:write(vim.fn.json_encode(data))
file:close()
end
end

local function load_bookmarks()
local storepath = vim.fn.stdpath("data") .. "/nvim-tree-bookmarks.json"
local file = io.open(storepath, "r")
if file then
local content = file:read("*all")
file:close()
if content and content ~= "" then
local data = vim.fn.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

function Marks:new(args)
self.explorer = args.explorer
self.marks = load_bookmarks() or {}
end

function Marks:toggle(node)
if node.absolute_path == nil then
return
end

if self:get(node) then
self.marks[node.absolute_path] = nil
else
self.marks[node.absolute_path] = node
end

save_bookmarks(self.marks)
self.explorer.renderer:draw()
end
---Prompts for selection of a marked node, sorted by absolute paths.
---A folder will be focused, a file will be opened.
---@public
Expand Down
Loading