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 <alex@courtis.org>
This commit is contained in:
Azad 2023-11-09 11:20:29 +01:00 committed by GitHub
parent 4ee6366ff1
commit a2aaf8b430
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 26 additions and 4 deletions

View File

@ -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

View File

@ -640,6 +640,7 @@ local ACCEPTED_TYPES = {
},
},
renderer = {
group_empty = { "boolean", "function" },
root_folder_label = { "function", "string", "boolean" },
},
actions = {

View File

@ -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"
@ -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
@ -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
@ -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

View File

@ -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()