Skip to content

Commit 01db61e

Browse files
committed
feat: add mark capabilites
1 parent e842f08 commit 01db61e

File tree

6 files changed

+107
-5
lines changed

6 files changed

+107
-5
lines changed

README.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -263,6 +263,8 @@ local list = {
263263
{ key = "s", cb = tree_cb("system_open") },
264264
{ key = "q", cb = tree_cb("close") },
265265
{ key = "g?", cb = tree_cb("toggle_help") },
266+
{ key = 'm', cb = ":lua require'nvim-tree.marks'.toggle_mark()<cr>" },
267+
{ key = 'M', cb = ":lua require'nvim-tree.marks'.disable_all()<cr>" },
266268
}
267269
```
268270

@@ -282,6 +284,7 @@ You can toggle the help UI by pressing `g?`.
282284
- Git integration (icons and file highlight)
283285
- Lsp diagnostics integration (signs)
284286
- Indent markers
287+
- Marks (for bulk actions, see `:help nvim-tree.marks`)
285288
- Mouse support
286289
- It's fast
287290

doc/nvim-tree-lua.txt

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -591,6 +591,8 @@ Defaults to:
591591
{ key = "s", cb = tree_cb("system_open") },
592592
{ key = "q", cb = tree_cb("close") },
593593
{ key = "g?", cb = tree_cb("toggle_help") },
594+
{ key = 'm', cb = ":lua require'nvim-tree.marks'.toggle_mark()<cr>" },
595+
{ key = 'M', cb = ":lua require'nvim-tree.marks'.disable_all()<cr>" },
594596
}
595597
<
596598

@@ -774,4 +776,35 @@ on_folder_removed({handler})
774776
{handler} (function) Handler function, with the
775777
signature `function(payload)`.
776778

779+
==============================================================================
780+
MARKS *nvim-tree-marks*
781+
782+
|nvim-tree.marks| *nvim-tree.marks*
783+
784+
You can toggle marks on file/folders to batch a function on multiple
785+
nodes with the `marks` module.
786+
787+
>
788+
local nvim_tree_marks = require "nvim-tree.marks"
789+
local marks = nvim_tree_marks.get_marks()
790+
<
791+
792+
toggle_mark() *nvim-tree.marks.toggle_mark()*
793+
794+
Toggle a mark on a file or folder under the cursor.
795+
Mapped to `m` by default.
796+
797+
get_marks() *nvim-tree.marks.get_marks()*
798+
799+
Get the list of current marked files/folders.
800+
Returns a list of nodes.
801+
802+
disable_all() *nvim-tree.marks.disable_all()*
803+
804+
Remove all marks currently enabled.
805+
Mapped to `M` by default.
806+
807+
You can highlight |NvimTreeMarkedFile| to override the default highlight
808+
for files and folders with marks.
809+
777810
vim:tw=78:ts=8:noet:ft=help:norl:

lua/nvim-tree/colors.lua

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@ local function get_hl_groups()
4747
GitRenamed = { fg = colors.purple },
4848
GitNew = { fg = colors.yellow },
4949

50-
WindowPicker = { gui = "bold", fg = "#ededed", bg = "#4493c8" }
50+
WindowPicker = { gui = "bold", fg = "#ededed", bg = "#4493c8" },
5151
}
5252
end
5353

@@ -73,6 +73,7 @@ local function get_links()
7373
StatusLine = "StatusLine",
7474
StatusLineNC = "StatusLineNC",
7575
SignColumn = 'NvimTreeNormal',
76+
MarkedFile = "Visual"
7677
}
7778
end
7879

lua/nvim-tree/marks.lua

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
local lib = require'nvim-tree.lib'
2+
local view = require'nvim-tree.view'
3+
local renderer = require'nvim-tree.renderer'
4+
5+
local M = {}
6+
7+
local function iterate_get_marks(node)
8+
local marks = {}
9+
for _, n in pairs(node.entries) do
10+
if n.marked then
11+
table.insert(marks, n)
12+
end
13+
if n.entries then
14+
vim.fn.extend(marks, iterate_get_marks(n))
15+
end
16+
end
17+
return marks
18+
end
19+
20+
function M.get_marks()
21+
return iterate_get_marks(lib.Tree)
22+
end
23+
24+
function M.toggle_mark()
25+
local node = lib.get_node_at_cursor()
26+
if not node then
27+
return
28+
end
29+
30+
node.marked = not node.marked
31+
if view.win_open() then
32+
renderer.draw(lib.Tree, true)
33+
end
34+
end
35+
36+
local function iterate_toggle_off(node)
37+
for _, n in pairs(node.entries) do
38+
n.marked = false
39+
if n.entries then
40+
iterate_toggle_off(n)
41+
end
42+
end
43+
end
44+
45+
function M.disable_all()
46+
iterate_toggle_off(lib.Tree)
47+
if view.win_open() then
48+
renderer.draw(lib.Tree, true)
49+
end
50+
end
51+
52+
return M

lua/nvim-tree/renderer/init.lua

Lines changed: 14 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -292,6 +292,7 @@ local function update_draw_data(tree, depth, markers)
292292
end
293293

294294
local git_hl = get_git_hl(node)
295+
local filename_start_at
295296

296297
if node.entries then
297298
local has_children = #node.entries ~= 0 or node.has_children
@@ -310,10 +311,15 @@ local function update_draw_data(tree, depth, markers)
310311
if special[node.absolute_path] then
311312
folder_hl = "NvimTreeSpecialFolderName"
312313
end
314+
filename_start_at = offset+#git_icon+#icon
313315
set_folder_hl(index, offset, #icon, #name+#git_icon, folder_hl)
314316
if git_hl then
315317
set_folder_hl(index, offset, #icon, #name+#git_icon, git_hl)
316318
end
319+
if node.marked then
320+
table.insert(hl, { 'NvimTreeMarkedFile', index, filename_start_at, -1 })
321+
end
322+
317323
index = index + 1
318324
if node.open then
319325
table.insert(lines, padding..icon..git_icon..name..(vim.g.nvim_tree_add_trailing == 1 and '/' or ''))
@@ -325,7 +331,8 @@ local function update_draw_data(tree, depth, markers)
325331
local icon = get_symlink_icon()
326332
local link_hl = git_hl or 'NvimTreeSymlink'
327333
local arrow = vim.g.nvim_tree_symlink_arrow or ''
328-
table.insert(hl, { link_hl, index, offset, -1 })
334+
filename_start_at = offset
335+
table.insert(hl, { link_hl, index, filename_start_at, -1 })
329336
table.insert(lines, padding..icon..node.name..arrow..node.link_to)
330337
index = index + 1
331338

@@ -342,10 +349,11 @@ local function update_draw_data(tree, depth, markers)
342349
end
343350
table.insert(lines, padding..icon..git_icons..node.name)
344351

352+
filename_start_at = offset+#git_icons+#icon
345353
if node.executable then
346-
table.insert(hl, {'NvimTreeExecFile', index, offset+#icon+#git_icons, -1 })
354+
table.insert(hl, {'NvimTreeExecFile', index, filename_start_at, -1 })
347355
elseif picture[node.extension] then
348-
table.insert(hl, {'NvimTreeImageFile', index, offset+#icon+#git_icons, -1 })
356+
table.insert(hl, {'NvimTreeImageFile', index, filename_start_at, -1 })
349357
end
350358

351359
if should_hl_opened_files then
@@ -365,6 +373,9 @@ local function update_draw_data(tree, depth, markers)
365373
end
366374
index = index + 1
367375
end
376+
if not node.entries and node.marked then
377+
table.insert(hl, { 'NvimTreeMarkedFile', index-1, filename_start_at, -1 })
378+
end
368379
end
369380
end
370381

lua/nvim-tree/view.lua

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -75,7 +75,9 @@ M.View = {
7575
{ key = "-", cb = M.nvim_tree_callback("dir_up") },
7676
{ key = "s", cb = M.nvim_tree_callback("system_open") },
7777
{ key = "q", cb = M.nvim_tree_callback("close") },
78-
{ key = "g?", cb = M.nvim_tree_callback("toggle_help") }
78+
{ key = "g?", cb = M.nvim_tree_callback("toggle_help") },
79+
{ key = 'm', cb = ":lua require'nvim-tree.marks'.toggle_mark()<cr>" },
80+
{ key = 'M', cb = ":lua require'nvim-tree.marks'.disable_all()<cr>" },
7981
}
8082
}
8183

0 commit comments

Comments
 (0)