feat(bookmarks): add bookmark feature (#1412)

This commit is contained in:
Kiyan
2022-07-11 10:00:12 +02:00
committed by GitHub
parent 0fa2ec1950
commit df92f1527f
8 changed files with 109 additions and 9 deletions

View File

@@ -469,6 +469,7 @@ local DEFAULT_OPTS = { -- BEGIN_DEFAULT_OPTS
glyphs = {
default = "",
symlink = "",
bookmark = "",
folder = {
arrow_closed = "",
arrow_open = "",
@@ -674,6 +675,7 @@ function M.setup(conf)
require("nvim-tree.lib").setup(opts)
require("nvim-tree.renderer").setup(opts)
require("nvim-tree.live-filter").setup(opts)
require("nvim-tree.marks").setup(opts)
if M.config.renderer.icons.show.file and pcall(require, "nvim-web-devicons") then
require("nvim-web-devicons").setup()
end

View File

@@ -45,6 +45,7 @@ local Actions = {
run_file_command = require("nvim-tree.actions.node.run-command").run_file_command,
toggle_file_info = require("nvim-tree.actions.node.file-popup").toggle_file_info,
system_open = require("nvim-tree.actions.node.system-open").fn,
toggle_mark = require("nvim-tree.marks").toggle_mark,
}
local function handle_action_on_help_ui(action)

View File

@@ -227,6 +227,11 @@ local DEFAULT_MAPPINGS = {
action = "toggle_help",
desc = "toggle help",
},
{
key = "m",
action = "toggle_mark",
desc = "Toggle node in bookmarks",
},
}
-- END_DEFAULT_MAPPINGS

View File

@@ -53,6 +53,8 @@ local function get_hl_groups()
WindowPicker = { gui = "bold", fg = "#ededed", bg = "#4493c8" },
LiveFilterPrefix = { gui = "bold", fg = colors.purple },
LiveFilterValue = { gui = "bold", fg = "#fff" },
Bookmark = { fg = colors.green },
}
end

View File

@@ -38,27 +38,25 @@ function NodeIterator:recursor(f)
end
function NodeIterator:iterate()
local iteration_count = 0
local function iter(nodes)
local i = 1
for _, node in ipairs(nodes) do
if self._filter_hidden(node) then
iteration_count = iteration_count + 1
if self._match(node) then
return node, i
return node, iteration_count
end
self._apply_fn_on_node(node)
self._apply_fn_on_node(node, iteration_count)
local children = self._recurse_with(node)
if children then
local n, idx = iter(children)
i = i + idx
local n = iter(children)
if n then
return n, i
return n, iteration_count
end
else
i = i + 1
end
end
end
return nil, i
return nil, 0
end
return iter(self.nodes)

70
lua/nvim-tree/marks.lua Normal file
View File

@@ -0,0 +1,70 @@
local view = require "nvim-tree.view"
local Iterator = require "nvim-tree.iterators.node-iterator"
local core = require "nvim-tree.core"
local NvimTreeMarks = {}
local M = {}
local function add_mark(node)
NvimTreeMarks[node.absolute_path] = true
M.draw()
end
local function remove_mark(node)
NvimTreeMarks[node.absolute_path] = nil
M.draw()
end
function M.toggle_mark(node)
if M.get_mark(node) then
remove_mark(node)
else
add_mark(node)
end
end
function M.get_mark(node)
return NvimTreeMarks[node.absolute_path]
end
function M.get_marks()
local list = {}
for k in pairs(NvimTreeMarks) do
table.insert(list, k)
end
return list
end
local GROUP = "NvimTreeMarkSigns"
local SIGN_NAME = "NvimTreeMark"
function M.clear()
vim.fn.sign_unplace(GROUP)
end
function M.draw()
if not view.is_visible() then
return
end
M.clear()
local buf = view.get_bufnr()
Iterator.builder(core.get_explorer().nodes)
:recursor(function(node)
return node.open and node.nodes
end)
:applier(function(node, idx)
if M.get_mark(node) then
vim.fn.sign_place(0, GROUP, SIGN_NAME, buf, { lnum = idx + 1, priority = 3 })
end
end)
:iterate()
end
function M.setup(opts)
vim.fn.sign_define(SIGN_NAME, { text = opts.renderer.icons.glyphs.bookmark, texthl = "NvimTreeBookmark" })
end
return M

View File

@@ -10,6 +10,7 @@ local help = require "nvim-tree.renderer.help"
local git = require "nvim-tree.renderer.components.git"
local Builder = require "nvim-tree.renderer.builder"
local live_filter = require "nvim-tree.live-filter"
local marks = require "nvim-tree.marks"
local api = vim.api
@@ -88,8 +89,10 @@ function M.draw()
if view.is_help_ui() then
diagnostics.clear()
marks.clear()
else
diagnostics.update()
marks.draw()
end
view.grow_from_content()