Skip to content

feat(#2515): add option to change grouped folders name with custom function #2521

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

Merged
merged 3 commits into from
Nov 9, 2023
Merged
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
4 changes: 3 additions & 1 deletion doc/nvim-tree-lua.txt
Original file line number Diff line number Diff line change
Expand Up @@ -795,7 +795,9 @@ Appends a trailing slash to folder names.

*nvim-tree.renderer.group_empty*
Compact folders that only contain a single folder into one node.
Type: `boolean`, Default: `false`
Boolean or function that takes one argument (the relative path
of grouped folders) and returns a string to be displayed.
Type: `boolean | function(relative_path):string`, Default: `false`

*nvim-tree.renderer.full_name*
Display node whose name length is wider than the width of nvim-tree window in
Expand Down
1 change: 1 addition & 0 deletions lua/nvim-tree.lua
Original file line number Diff line number Diff line change
Expand Up @@ -640,6 +640,7 @@ local ACCEPTED_TYPES = {
},
},
renderer = {
group_empty = { "boolean", "function" },
root_folder_label = { "function", "string", "boolean" },
},
actions = {
Expand Down
24 changes: 21 additions & 3 deletions lua/nvim-tree/renderer/builder.lua
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
local utils = require "nvim-tree.utils"
local core = require "nvim-tree.core"
local notify = require "nvim-tree.notify"

local git = require "nvim-tree.renderer.components.git"
local pad = require "nvim-tree.renderer.components.padding"
Expand Down Expand Up @@ -105,6 +106,13 @@ function Builder:configure_symlink_destination(show)
return self
end

function Builder:configure_group_name_modifier(group_name_modifier)
if type(group_name_modifier) == "function" then
self.group_name_modifier = group_name_modifier
end
return self
end

function Builder:_insert_highlight(group, start, end_)
table.insert(self.highlights, { group, self.index, start, end_ or -1 })
end
Expand All @@ -113,14 +121,24 @@ function Builder:_insert_line(line)
table.insert(self.lines, line)
end

local function get_folder_name(node)
function Builder:_get_folder_name(node)
local name = node.name
local next = node.group_next
while next do
name = name .. "/" .. next.name
next = next.group_next
end
return name

if node.group_next and self.group_name_modifier then
local new_name = self.group_name_modifier(name)
if type(new_name) == "string" then
name = new_name
else
notify.warn(string.format("Invalid return type for field renderer.group_empty. Expected string, got %s", type(new_name)))
end
end

return name .. self.trailing_slash
end

---@class HighlightedString
Expand Down Expand Up @@ -152,7 +170,7 @@ end
function Builder:_build_folder(node)
local has_children = #node.nodes ~= 0 or node.has_children
local icon, icon_hl = icons.get_folder_icon(node, has_children)
local foldername = get_folder_name(node) .. self.trailing_slash
local foldername = self:_get_folder_name(node)

if #icon > 0 and icon_hl == nil then
if node.open then
Expand Down
1 change: 1 addition & 0 deletions lua/nvim-tree/renderer/init.lua
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,7 @@ function M.draw(unloaded_bufnr)
:configure_modified_placement(M.config.icons.modified_placement)
:configure_symlink_destination(M.config.symlink_destination)
:configure_filter(live_filter.filter, live_filter.prefix)
:configure_group_name_modifier(M.config.group_empty)
:build_header(view.is_root_folder_visible(core.get_cwd()))
:build(core.get_explorer(), unloaded_bufnr)
:unwrap()
Expand Down