Skip to content

Commit a2aaf8b

Browse files
feat(#2515): add option to change grouped folders name with custom function (#2521)
* Add option to change grouped folders name with custom function * Fix docs --------- Co-authored-by: Alexander Courtis <[email protected]>
1 parent 4ee6366 commit a2aaf8b

File tree

4 files changed

+26
-4
lines changed

4 files changed

+26
-4
lines changed

doc/nvim-tree-lua.txt

+3-1
Original file line numberDiff line numberDiff line change
@@ -795,7 +795,9 @@ Appends a trailing slash to folder names.
795795

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

800802
*nvim-tree.renderer.full_name*
801803
Display node whose name length is wider than the width of nvim-tree window in

lua/nvim-tree.lua

+1
Original file line numberDiff line numberDiff line change
@@ -640,6 +640,7 @@ local ACCEPTED_TYPES = {
640640
},
641641
},
642642
renderer = {
643+
group_empty = { "boolean", "function" },
643644
root_folder_label = { "function", "string", "boolean" },
644645
},
645646
actions = {

lua/nvim-tree/renderer/builder.lua

+21-3
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
local utils = require "nvim-tree.utils"
22
local core = require "nvim-tree.core"
3+
local notify = require "nvim-tree.notify"
34

45
local git = require "nvim-tree.renderer.components.git"
56
local pad = require "nvim-tree.renderer.components.padding"
@@ -105,6 +106,13 @@ function Builder:configure_symlink_destination(show)
105106
return self
106107
end
107108

109+
function Builder:configure_group_name_modifier(group_name_modifier)
110+
if type(group_name_modifier) == "function" then
111+
self.group_name_modifier = group_name_modifier
112+
end
113+
return self
114+
end
115+
108116
function Builder:_insert_highlight(group, start, end_)
109117
table.insert(self.highlights, { group, self.index, start, end_ or -1 })
110118
end
@@ -113,14 +121,24 @@ function Builder:_insert_line(line)
113121
table.insert(self.lines, line)
114122
end
115123

116-
local function get_folder_name(node)
124+
function Builder:_get_folder_name(node)
117125
local name = node.name
118126
local next = node.group_next
119127
while next do
120128
name = name .. "/" .. next.name
121129
next = next.group_next
122130
end
123-
return name
131+
132+
if node.group_next and self.group_name_modifier then
133+
local new_name = self.group_name_modifier(name)
134+
if type(new_name) == "string" then
135+
name = new_name
136+
else
137+
notify.warn(string.format("Invalid return type for field renderer.group_empty. Expected string, got %s", type(new_name)))
138+
end
139+
end
140+
141+
return name .. self.trailing_slash
124142
end
125143

126144
---@class HighlightedString
@@ -152,7 +170,7 @@ end
152170
function Builder:_build_folder(node)
153171
local has_children = #node.nodes ~= 0 or node.has_children
154172
local icon, icon_hl = icons.get_folder_icon(node, has_children)
155-
local foldername = get_folder_name(node) .. self.trailing_slash
173+
local foldername = self:_get_folder_name(node)
156174

157175
if #icon > 0 and icon_hl == nil then
158176
if node.open then

lua/nvim-tree/renderer/init.lua

+1
Original file line numberDiff line numberDiff line change
@@ -80,6 +80,7 @@ function M.draw(unloaded_bufnr)
8080
:configure_modified_placement(M.config.icons.modified_placement)
8181
:configure_symlink_destination(M.config.symlink_destination)
8282
:configure_filter(live_filter.filter, live_filter.prefix)
83+
:configure_group_name_modifier(M.config.group_empty)
8384
:build_header(view.is_root_folder_visible(core.get_cwd()))
8485
:build(core.get_explorer(), unloaded_bufnr)
8586
:unwrap()

0 commit comments

Comments
 (0)