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
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
8 changed files with 109 additions and 9 deletions

View File

@ -14,6 +14,7 @@ CONTENTS *nvim-tree*
6. Highlight Groups |nvim-tree-highlight| 6. Highlight Groups |nvim-tree-highlight|
7. Events |nvim-tree-events| 7. Events |nvim-tree-events|
7.1 Available Events |nvim-tree.events| 7.1 Available Events |nvim-tree.events|
8. Bookmarks |nvim-tree-bookmarks|
============================================================================== ==============================================================================
1. INTRODUCTION *nvim-tree-introduction* 1. INTRODUCTION *nvim-tree-introduction*
@ -220,6 +221,7 @@ Subsequent calls to setup will replace the previous configuration.
glyphs = { glyphs = {
default = "", default = "",
symlink = "", symlink = "",
bookmark = "",
folder = { folder = {
arrow_closed = "", arrow_closed = "",
arrow_open = "", arrow_open = "",
@ -1012,6 +1014,7 @@ DEFAULT MAPPINGS *nvim-tree-default-mappings
`.` run_file_command enter vim command mode with the file the cursor is on `.` run_file_command enter vim command mode with the file the cursor is on
`<C-k>` toggle_file_info toggle a popup with file infos about the file under the cursor `<C-k>` toggle_file_info toggle a popup with file infos about the file under the cursor
`g?` toggle_help toggle help `g?` toggle_help toggle help
`m` toggle_mark Toggle node in bookmarks
> >
view.mappings.list = { -- BEGIN_DEFAULT_MAPPINGS view.mappings.list = { -- BEGIN_DEFAULT_MAPPINGS
@ -1059,6 +1062,7 @@ DEFAULT MAPPINGS *nvim-tree-default-mappings
{ key = ".", action = "run_file_command" } { key = ".", action = "run_file_command" }
{ key = "<C-k>", action = "toggle_file_info" } { key = "<C-k>", action = "toggle_file_info" }
{ key = "g?", action = "toggle_help" } { key = "g?", action = "toggle_help" }
{ key = "m", action = "toggle_mark" }
} -- END_DEFAULT_MAPPINGS } -- END_DEFAULT_MAPPINGS
< <
============================================================================== ==============================================================================
@ -1130,6 +1134,11 @@ There are 2 highlight groups for the live filter feature
NvimTreeLiveFilterPrefix NvimTreeLiveFilterPrefix
NvimTreeLiveFilterValue NvimTreeLiveFilterValue
Color of the bookmark icon
NvimTreeBookmark
============================================================================== ==============================================================================
7. EVENTS *nvim-tree-events* 7. EVENTS *nvim-tree-events*
@ -1245,4 +1254,14 @@ on_tree_resize({handler})
{handler} `{function}` Handler function, with the {handler} `{function}` Handler function, with the
signature `function(size)`. signature `function(size)`.
==============================================================================
8. BOOKMARKS *nvim-tree-bookmarks*
You can toggle marks on files/folders with
`require("nvim-tree.marks").toggle_mark(node)` which is bound to `m` by
default.
To get the list of marked paths, you can call
`require("nvim-tree.marks").get_marks()`. This will return `{string}`.
vim:tw=78:ts=4:sw=4:et:ft=help:norl: vim:tw=78:ts=4:sw=4:et:ft=help:norl:

View File

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

View File

@ -45,6 +45,7 @@ local Actions = {
run_file_command = require("nvim-tree.actions.node.run-command").run_file_command, 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, toggle_file_info = require("nvim-tree.actions.node.file-popup").toggle_file_info,
system_open = require("nvim-tree.actions.node.system-open").fn, 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) local function handle_action_on_help_ui(action)

View File

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

View File

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

View File

@ -38,27 +38,25 @@ function NodeIterator:recursor(f)
end end
function NodeIterator:iterate() function NodeIterator:iterate()
local iteration_count = 0
local function iter(nodes) local function iter(nodes)
local i = 1
for _, node in ipairs(nodes) do for _, node in ipairs(nodes) do
if self._filter_hidden(node) then if self._filter_hidden(node) then
iteration_count = iteration_count + 1
if self._match(node) then if self._match(node) then
return node, i return node, iteration_count
end end
self._apply_fn_on_node(node) self._apply_fn_on_node(node, iteration_count)
local children = self._recurse_with(node) local children = self._recurse_with(node)
if children then if children then
local n, idx = iter(children) local n = iter(children)
i = i + idx
if n then if n then
return n, i return n, iteration_count
end end
else
i = i + 1
end end
end end
end end
return nil, i return nil, 0
end end
return iter(self.nodes) 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 git = require "nvim-tree.renderer.components.git"
local Builder = require "nvim-tree.renderer.builder" local Builder = require "nvim-tree.renderer.builder"
local live_filter = require "nvim-tree.live-filter" local live_filter = require "nvim-tree.live-filter"
local marks = require "nvim-tree.marks"
local api = vim.api local api = vim.api
@ -88,8 +89,10 @@ function M.draw()
if view.is_help_ui() then if view.is_help_ui() then
diagnostics.clear() diagnostics.clear()
marks.clear()
else else
diagnostics.update() diagnostics.update()
marks.draw()
end end
view.grow_from_content() view.grow_from_content()