Skip to content

feat: add mark capabilites #612

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -263,6 +263,8 @@ local list = {
{ key = "s", cb = tree_cb("system_open") },
{ key = "q", cb = tree_cb("close") },
{ key = "g?", cb = tree_cb("toggle_help") },
{ key = 'm', cb = ":lua require'nvim-tree.marks'.toggle_mark()<cr>" },
{ key = 'M', cb = ":lua require'nvim-tree.marks'.disable_all()<cr>" },
}
```

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

Expand Down
33 changes: 33 additions & 0 deletions doc/nvim-tree-lua.txt
Original file line number Diff line number Diff line change
Expand Up @@ -591,6 +591,8 @@ Defaults to:
{ key = "s", cb = tree_cb("system_open") },
{ key = "q", cb = tree_cb("close") },
{ key = "g?", cb = tree_cb("toggle_help") },
{ key = 'm', cb = ":lua require'nvim-tree.marks'.toggle_mark()<cr>" },
{ key = 'M', cb = ":lua require'nvim-tree.marks'.disable_all()<cr>" },
}
<

Expand Down Expand Up @@ -774,4 +776,35 @@ on_folder_removed({handler})
{handler} (function) Handler function, with the
signature `function(payload)`.

==============================================================================
MARKS *nvim-tree-marks*

|nvim-tree.marks| *nvim-tree.marks*

You can toggle marks on file/folders to batch a function on multiple
nodes with the `marks` module.

>
local nvim_tree_marks = require "nvim-tree.marks"
local marks = nvim_tree_marks.get_marks()
<

toggle_mark() *nvim-tree.marks.toggle_mark()*

Toggle a mark on a file or folder under the cursor.
Mapped to `m` by default.

get_marks() *nvim-tree.marks.get_marks()*

Get the list of current marked files/folders.
Returns a list of nodes.

disable_all() *nvim-tree.marks.disable_all()*

Remove all marks currently enabled.
Mapped to `M` by default.

You can highlight |NvimTreeMarkedFile| to override the default highlight
for files and folders with marks.

vim:tw=78:ts=8:noet:ft=help:norl:
3 changes: 2 additions & 1 deletion lua/nvim-tree/colors.lua
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ local function get_hl_groups()
GitRenamed = { fg = colors.purple },
GitNew = { fg = colors.yellow },

WindowPicker = { gui = "bold", fg = "#ededed", bg = "#4493c8" }
WindowPicker = { gui = "bold", fg = "#ededed", bg = "#4493c8" },
}
end

Expand All @@ -73,6 +73,7 @@ local function get_links()
StatusLine = "StatusLine",
StatusLineNC = "StatusLineNC",
SignColumn = 'NvimTreeNormal',
MarkedFile = "Visual"
}
end

Expand Down
52 changes: 52 additions & 0 deletions lua/nvim-tree/marks.lua
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
local lib = require'nvim-tree.lib'
local view = require'nvim-tree.view'
local renderer = require'nvim-tree.renderer'

local M = {}

local function iterate_get_marks(node)
local marks = {}
for _, n in pairs(node.entries) do
if n.marked then
table.insert(marks, n)
end
if n.entries then
vim.fn.extend(marks, iterate_get_marks(n))
end
end
return marks
end

function M.get_marks()
return iterate_get_marks(lib.Tree)
end

function M.toggle_mark()
local node = lib.get_node_at_cursor()
if not node then
return
end

node.marked = not node.marked
if view.win_open() then
renderer.draw(lib.Tree, true)
end
end

local function iterate_toggle_off(node)
for _, n in pairs(node.entries) do
n.marked = false
if n.entries then
iterate_toggle_off(n)
end
end
end

function M.disable_all()
iterate_toggle_off(lib.Tree)
if view.win_open() then
renderer.draw(lib.Tree, true)
end
end

return M
17 changes: 14 additions & 3 deletions lua/nvim-tree/renderer/init.lua
Original file line number Diff line number Diff line change
Expand Up @@ -292,6 +292,7 @@ local function update_draw_data(tree, depth, markers)
end

local git_hl = get_git_hl(node)
local filename_start_at

if node.entries then
local has_children = #node.entries ~= 0 or node.has_children
Expand All @@ -310,10 +311,15 @@ local function update_draw_data(tree, depth, markers)
if special[node.absolute_path] then
folder_hl = "NvimTreeSpecialFolderName"
end
filename_start_at = offset+#git_icon+#icon
set_folder_hl(index, offset, #icon, #name+#git_icon, folder_hl)
if git_hl then
set_folder_hl(index, offset, #icon, #name+#git_icon, git_hl)
end
if node.marked then
table.insert(hl, { 'NvimTreeMarkedFile', index, filename_start_at, -1 })
end

index = index + 1
if node.open then
table.insert(lines, padding..icon..git_icon..name..(vim.g.nvim_tree_add_trailing == 1 and '/' or ''))
Expand All @@ -325,7 +331,8 @@ local function update_draw_data(tree, depth, markers)
local icon = get_symlink_icon()
local link_hl = git_hl or 'NvimTreeSymlink'
local arrow = vim.g.nvim_tree_symlink_arrow or ' ➛ '
table.insert(hl, { link_hl, index, offset, -1 })
filename_start_at = offset
table.insert(hl, { link_hl, index, filename_start_at, -1 })
table.insert(lines, padding..icon..node.name..arrow..node.link_to)
index = index + 1

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

filename_start_at = offset+#git_icons+#icon
if node.executable then
table.insert(hl, {'NvimTreeExecFile', index, offset+#icon+#git_icons, -1 })
table.insert(hl, {'NvimTreeExecFile', index, filename_start_at, -1 })
elseif picture[node.extension] then
table.insert(hl, {'NvimTreeImageFile', index, offset+#icon+#git_icons, -1 })
table.insert(hl, {'NvimTreeImageFile', index, filename_start_at, -1 })
end

if should_hl_opened_files then
Expand All @@ -365,6 +373,9 @@ local function update_draw_data(tree, depth, markers)
end
index = index + 1
end
if not node.entries and node.marked then
table.insert(hl, { 'NvimTreeMarkedFile', index-1, filename_start_at, -1 })
end
end
end

Expand Down
4 changes: 3 additions & 1 deletion lua/nvim-tree/view.lua
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,9 @@ M.View = {
{ key = "-", cb = M.nvim_tree_callback("dir_up") },
{ key = "s", cb = M.nvim_tree_callback("system_open") },
{ key = "q", cb = M.nvim_tree_callback("close") },
{ key = "g?", cb = M.nvim_tree_callback("toggle_help") }
{ key = "g?", cb = M.nvim_tree_callback("toggle_help") },
{ key = 'm', cb = ":lua require'nvim-tree.marks'.toggle_mark()<cr>" },
{ key = 'M', cb = ":lua require'nvim-tree.marks'.disable_all()<cr>" },
}
}

Expand Down