Skip to content

Commit e2892ca

Browse files
committed
feat: add mark capabilites
1 parent 71122d7 commit e2892ca

File tree

5 files changed

+72
-5
lines changed

5 files changed

+72
-5
lines changed

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -253,6 +253,7 @@ This plugin is very fast because it uses the `libuv` `scandir` and `scandir_next
253253
- Git integration (icons and file highlight)
254254
- Lsp diagnostics integration (signs)
255255
- Indent markers
256+
- Marks (for bulk actions)
256257
- Mouse support
257258
- It's fast
258259

lua/nvim-tree/colors.lua

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,8 @@ 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" },
51+
MarkedFile = { gui = "bold", fg = "#6be37a" },
5152
}
5253
end
5354

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.toggle_all_off()
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.lua

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

310310
local git_hl = get_git_hl(node)
311+
local filename_start_at
311312

312313
if node.entries then
313314
local has_children = #node.entries ~= 0 or node.has_children
@@ -323,10 +324,15 @@ local function update_draw_data(tree, depth, markers)
323324
end
324325
if not has_children then folder_hl = "NvimTreeEmptyFolderName" end
325326
if node.open then folder_hl = "NvimTreeOpenedFolderName" end
327+
filename_start_at = offset+#git_icon+#icon
326328
set_folder_hl(index, offset, #icon, #name+#git_icon, folder_hl)
327329
if git_hl then
328330
set_folder_hl(index, offset, #icon, #name+#git_icon, git_hl)
329331
end
332+
if node.marked then
333+
table.insert(hl, { 'NvimTreeMarkedFile', index, filename_start_at, -1 })
334+
end
335+
330336
index = index + 1
331337
if node.open then
332338
table.insert(lines, padding..icon..git_icon..name..(vim.g.nvim_tree_add_trailing == 1 and '/' or ''))
@@ -338,7 +344,8 @@ local function update_draw_data(tree, depth, markers)
338344
local icon = get_symlink_icon()
339345
local link_hl = git_hl or 'NvimTreeSymlink'
340346
local arrow = vim.g.nvim_tree_symlink_arrow or ''
341-
table.insert(hl, { link_hl, index, offset, -1 })
347+
filename_start_at = offset
348+
table.insert(hl, { link_hl, index, filename_start_at, -1 })
342349
table.insert(lines, padding..icon..node.name..arrow..node.link_to)
343350
index = index + 1
344351

@@ -355,10 +362,11 @@ local function update_draw_data(tree, depth, markers)
355362
end
356363
table.insert(lines, padding..icon..git_icons..node.name)
357364

365+
filename_start_at = offset+#git_icons+#icon
358366
if node.executable then
359-
table.insert(hl, {'NvimTreeExecFile', index, offset+#icon+#git_icons, -1 })
367+
table.insert(hl, {'NvimTreeExecFile', index, filename_start_at, -1 })
360368
elseif picture[node.extension] then
361-
table.insert(hl, {'NvimTreeImageFile', index, offset+#icon+#git_icons, -1 })
369+
table.insert(hl, {'NvimTreeImageFile', index, filename_start_at, -1 })
362370
end
363371

364372
if should_hl_opened_files then
@@ -378,6 +386,9 @@ local function update_draw_data(tree, depth, markers)
378386
end
379387
index = index + 1
380388
end
389+
if not node.entries and node.marked then
390+
table.insert(hl, { 'nvimtreemarkedfile', index-1, filename_start_at, -1 })
391+
end
381392
end
382393
end
383394

lua/nvim-tree/view.lua

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -73,7 +73,9 @@ M.View = {
7373
{ key = "-", cb = M.nvim_tree_callback("dir_up") },
7474
{ key = "s", cb = M.nvim_tree_callback("system_open") },
7575
{ key = "q", cb = M.nvim_tree_callback("close") },
76-
{ key = "g?", cb = M.nvim_tree_callback("toggle_help") }
76+
{ key = "g?", cb = M.nvim_tree_callback("toggle_help") },
77+
{ key = 'm', cb = ":lua require'nvim-tree.marks'.toggle_mark()<cr>" },
78+
{ key = 'M', cb = ":lua require'nvim-tree.marks'.toggle_all_off()<cr>" },
7779
}
7880
}
7981

0 commit comments

Comments
 (0)