Skip to content

Commit 99d7136

Browse files
davileragegounealex-courtis
authored
feat(renderer): add renderer.root_folder_label (#1746)
* Add new renderer setting `add_root_updir` to fix #1743. * Fix default value in docs. * Remove proposed “add_root_updir” and rename “root_folder_modifier” to “root_folder_label”. Also, “root_folder_label” can be also a function now. * chore: warn users about breaking change * fix(#1743): use silent migration of root_folder_modifier * fix(#1743): add example, document previous renderer.root_folder_modifier * Add check to validate return type of “root_folder_label” is string. * Change “root_folder_label” default value to “:~:s?$?/..?”. * Add missing keyword “local” to local variable “label”. Co-authored-by: David Aguilera <david.aguilera@neliosoftware.com> Co-authored-by: gegoune <dev@clog.rocks> Co-authored-by: Alexander Courtis <alex@courtis.org>
1 parent 68a2a09 commit 99d7136

File tree

5 files changed

+33
-14
lines changed

5 files changed

+33
-14
lines changed

doc/nvim-tree-lua.txt

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -221,7 +221,7 @@ Subsequent calls to setup will replace the previous configuration.
221221
highlight_git = false,
222222
full_name = false,
223223
highlight_opened_files = "none",
224-
root_folder_modifier = ":~",
224+
root_folder_label = ":~:s?$?/..?",
225225
indent_width = 2,
226226
indent_markers = {
227227
enable = false,
@@ -760,11 +760,17 @@ UI rendering setup
760760
Value can be `"none"`, `"icon"`, `"name"` or `"all"`.
761761
Type: `string`, Default: `"none"`
762762

763-
*nvim-tree.renderer.root_folder_modifier*
763+
*nvim-tree.renderer.root_folder_label* (previously `renderer.root_folder_modifier`)
764764
In what format to show root folder. See `:help filename-modifiers` for
765-
available options.
766-
Type: `string`, Default: `":~"`
767-
765+
available `string` options.
766+
Type: `string` or `function(root_cwd)`, Default: `":~:s?$?/..?"`
767+
768+
Function is passed the absolute path of the root folder and should return a string.
769+
e.g. >
770+
my_root_folder_label = function(path)
771+
return ".../" .. vim.fn.fnamemodify(path, ":t")
772+
end
773+
<
768774
*nvim-tree.renderer.indent_width*
769775
Number of spaces for an each tree nesting level. Minimum 1.
770776
Type: `number`, Default: `2`

lua/nvim-tree.lua

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -499,7 +499,7 @@ local DEFAULT_OPTS = { -- BEGIN_DEFAULT_OPTS
499499
highlight_git = false,
500500
full_name = false,
501501
highlight_opened_files = "none",
502-
root_folder_modifier = ":~",
502+
root_folder_label = ":~:s?$?/..?",
503503
indent_width = 2,
504504
indent_markers = {
505505
enable = false,
@@ -679,6 +679,7 @@ local FIELD_OVERRIDE_TYPECHECK = {
679679
remove_keymaps = { boolean = true, table = true },
680680
on_attach = { ["function"] = true, string = true },
681681
sort_by = { ["function"] = true, string = true },
682+
root_folder_label = { ["function"] = true, string = true },
682683
}
683684

684685
local function validate_options(conf)

lua/nvim-tree/legacy.lua

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -294,6 +294,9 @@ local function refactored(opts)
294294
utils.move_missing_val(opts, "", "open_on_tab", opts, "tab.sync", "open", false)
295295
utils.move_missing_val(opts, "", "open_on_tab", opts, "tab.sync", "close")
296296
utils.move_missing_val(opts, "", "ignore_buf_on_tab_change", opts, "tab.sync", "ignore")
297+
298+
-- 2022/11/22
299+
utils.move_missing_val(opts, "renderer", "root_folder_modifier", opts, "renderer", "root_folder_label")
297300
end
298301

299302
local function removed(opts)
@@ -304,8 +307,8 @@ local function removed(opts)
304307

305308
if opts.focus_empty_on_setup then
306309
notify.warn "focus_empty_on_setup has been removed and will be replaced by a new startup configuration. Please remove this option. See https://bit.ly/3yJch2T"
310+
opts.focus_empty_on_setup = nil
307311
end
308-
opts.focus_empty_on_setup = nil
309312
end
310313

311314
function M.migrate_legacy_options(opts)

lua/nvim-tree/renderer/builder.lua

Lines changed: 15 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,8 @@ local icons = require "nvim-tree.renderer.components.icons"
88
local Builder = {}
99
Builder.__index = Builder
1010

11+
local DEFAULT_ROOT_FOLDER_LABEL = ":~:s?$?/..?"
12+
1113
function Builder.new(root_cwd)
1214
return setmetatable({
1315
index = 0,
@@ -20,8 +22,8 @@ function Builder.new(root_cwd)
2022
}, Builder)
2123
end
2224

23-
function Builder:configure_root_modifier(root_folder_modifier)
24-
self.root_folder_modifier = root_folder_modifier or ":~"
25+
function Builder:configure_root_label(root_folder_label)
26+
self.root_folder_label = root_folder_label or DEFAULT_ROOT_FOLDER_LABEL
2527
return self
2628
end
2729

@@ -294,14 +296,21 @@ function Builder:build(tree)
294296
return self
295297
end
296298

297-
local function format_root_name(root_cwd, modifier)
298-
local base_root = utils.path_remove_trailing(vim.fn.fnamemodify(root_cwd, modifier))
299-
return utils.path_join { base_root, ".." }
299+
local function format_root_name(root_cwd, root_label)
300+
if type(root_label) == "function" then
301+
local label = root_label(root_cwd)
302+
if type(label) == "string" then
303+
return label
304+
else
305+
root_label = DEFAULT_ROOT_FOLDER_LABEL
306+
end
307+
end
308+
return utils.path_remove_trailing(vim.fn.fnamemodify(root_cwd, root_label))
300309
end
301310

302311
function Builder:build_header(show_header)
303312
if show_header then
304-
local root_name = format_root_name(self.root_cwd, self.root_folder_modifier)
313+
local root_name = format_root_name(self.root_cwd, self.root_folder_label)
305314
self:_insert_line(root_name)
306315
self:_insert_highlight("NvimTreeRootFolder", 0, string.len(root_name))
307316
self.index = 1

lua/nvim-tree/renderer/init.lua

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,7 @@ function M.draw()
6363
lines, hl = help.compute_lines()
6464
else
6565
lines, hl, signs = Builder.new(core.get_cwd())
66-
:configure_root_modifier(M.config.root_folder_modifier)
66+
:configure_root_label(M.config.root_folder_label)
6767
:configure_trailing_slash(M.config.add_trailing)
6868
:configure_special_files(M.config.special_files)
6969
:configure_picture_map(picture_map)

0 commit comments

Comments
 (0)