From 51d262b488a91c9016aaefc7fd563cd945005b41 Mon Sep 17 00:00:00 2001 From: xiantang Date: Sun, 22 Dec 2024 14:23:47 +0800 Subject: [PATCH 1/3] Support Persist Bookmarks --- lua/nvim-tree/marks/init.lua | 77 +++++++++++++++++++++++++----------- 1 file changed, 53 insertions(+), 24 deletions(-) diff --git a/lua/nvim-tree/marks/init.lua b/lua/nvim-tree/marks/init.lua index c940f999983..0c00bcd3e35 100644 --- a/lua/nvim-tree/marks/init.lua +++ b/lua/nvim-tree/marks/init.lua @@ -10,6 +10,7 @@ local utils = require("nvim-tree.utils") local Class = require("nvim-tree.classic") local DirectoryNode = require("nvim-tree.node.directory") +local json = vim.json ---@class (exact) Marks: Class ---@field private explorer Explorer @@ -22,14 +23,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() @@ -46,22 +39,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 @@ -243,6 +220,58 @@ function Marks:navigate_next() self:navigate(false) end +-- local json = vim.json + +local function save_bookmarks(marks) + local path = vim.fn.stdpath("data") .. "/nvim-tree-bookmarks.json" + local file = io.open(path, "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 path = vim.fn.stdpath("data") .. "/nvim-tree-bookmarks.json" + local file = io.open(path, "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 From 6ec9fc13ffcd22a4d456929b4b14c0fb4954e0f9 Mon Sep 17 00:00:00 2001 From: xiantang Date: Sun, 22 Dec 2024 14:44:44 +0800 Subject: [PATCH 2/3] Fix node new open error --- lua/nvim-tree/explorer/filters.lua | 4 +++- lua/nvim-tree/marks/init.lua | 8 ++++---- 2 files changed, 7 insertions(+), 5 deletions(-) diff --git a/lua/nvim-tree/explorer/filters.lua b/lua/nvim-tree/explorer/filters.lua index 62230687e40..e9b3f2c7d0e 100644 --- a/lua/nvim-tree/explorer/filters.lua +++ b/lua/nvim-tree/explorer/filters.lua @@ -209,7 +209,9 @@ 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 + if type(node) == "table" then + status.bookmarks[node.absolute_path] = node.type + end end end diff --git a/lua/nvim-tree/marks/init.lua b/lua/nvim-tree/marks/init.lua index 0c00bcd3e35..5f05d272fdc 100644 --- a/lua/nvim-tree/marks/init.lua +++ b/lua/nvim-tree/marks/init.lua @@ -223,8 +223,8 @@ end -- local json = vim.json local function save_bookmarks(marks) - local path = vim.fn.stdpath("data") .. "/nvim-tree-bookmarks.json" - local file = io.open(path, "w") + 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 @@ -236,8 +236,8 @@ local function save_bookmarks(marks) end local function load_bookmarks() - local path = vim.fn.stdpath("data") .. "/nvim-tree-bookmarks.json" - local file = io.open(path, "r") + 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() From 318f0ba1cf84933037a5c87b3dd61aaf27c73d8a Mon Sep 17 00:00:00 2001 From: xiantang Date: Sun, 22 Dec 2024 15:12:54 +0800 Subject: [PATCH 3/3] Fix filter marks index issue --- lua/nvim-tree/explorer/filters.lua | 6 ++---- lua/nvim-tree/marks/init.lua | 2 -- 2 files changed, 2 insertions(+), 6 deletions(-) diff --git a/lua/nvim-tree/explorer/filters.lua b/lua/nvim-tree/explorer/filters.lua index e9b3f2c7d0e..5518f31b43e 100644 --- a/lua/nvim-tree/explorer/filters.lua +++ b/lua/nvim-tree/explorer/filters.lua @@ -208,10 +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 - if type(node) == "table" then - status.bookmarks[node.absolute_path] = node.type - end + for key, node in pairs(explorer.marks.marks) do + status.bookmarks[key] = node end end diff --git a/lua/nvim-tree/marks/init.lua b/lua/nvim-tree/marks/init.lua index 5f05d272fdc..930b4c85797 100644 --- a/lua/nvim-tree/marks/init.lua +++ b/lua/nvim-tree/marks/init.lua @@ -10,7 +10,6 @@ local utils = require("nvim-tree.utils") local Class = require("nvim-tree.classic") local DirectoryNode = require("nvim-tree.node.directory") -local json = vim.json ---@class (exact) Marks: Class ---@field private explorer Explorer @@ -220,7 +219,6 @@ function Marks:navigate_next() self:navigate(false) end --- local json = vim.json local function save_bookmarks(marks) local storepath = vim.fn.stdpath("data") .. "/nvim-tree-bookmarks.json"