Skip to content

Commit e8bf3d7

Browse files
authored
feat(renderer): add renderer.indent_width (#1505)
* feat: add config option for a tree indent width add 'indent_width' option to configure visible indent for tree nesting levels (default is 2). * add 'bottom' char for a corner extension * apply stylua formatting * provide value constraints in documentation * limit minimal indent width * make marker symbols have one utf8 char width * match stylua formatting * add the commentary regarding utf-8 first symbol match
1 parent 757951b commit e8bf3d7

File tree

4 files changed

+48
-20
lines changed

4 files changed

+48
-20
lines changed

doc/nvim-tree-lua.txt

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -213,13 +213,15 @@ Subsequent calls to setup will replace the previous configuration.
213213
full_name = false,
214214
highlight_opened_files = "none",
215215
root_folder_modifier = ":~",
216+
indent_width = 2,
216217
indent_markers = {
217218
enable = false,
218219
inline_arrows = true,
219220
icons = {
220221
corner = "└",
221222
edge = "│",
222223
item = "│",
224+
bottom = "─",
223225
none = " ",
224226
},
225227
},
@@ -709,6 +711,10 @@ UI rendering setup
709711
available options.
710712
Type: `string`, Default: `":~"`
711713

714+
*nvim-tree.renderer.indent_width*
715+
Number of spaces for an each tree nesting level. Minimum 1.
716+
Type: `number`, Default: `2`
717+
712718
*nvim-tree.renderer.indent_markers*
713719
Configuration options for tree indent markers.
714720

@@ -722,8 +728,8 @@ UI rendering setup
722728
Type: `boolean`, Default: `true`
723729

724730
*nvim-tree.renderer.indent_markers.icons*
725-
Icons shown before the file/directory.
726-
Type: `table`, Default: `{ corner = "└", edge = "│", item = "│", none = " ", }`
731+
Icons shown before the file/directory. Length 1.
732+
Type: `table`, Default: `{ corner = "└", edge = "│", item = "│", bottom = "─", none = " ", }`
727733

728734
*nvim-tree.renderer.icons*
729735
Configuration options for icons.

lua/nvim-tree.lua

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -476,13 +476,15 @@ local DEFAULT_OPTS = { -- BEGIN_DEFAULT_OPTS
476476
full_name = false,
477477
highlight_opened_files = "none",
478478
root_folder_modifier = ":~",
479+
indent_width = 2,
479480
indent_markers = {
480481
enable = false,
481482
inline_arrows = true,
482483
icons = {
483484
corner = "",
484485
edge = "",
485486
item = "",
487+
bottom = "",
486488
none = " ",
487489
},
488490
},

lua/nvim-tree/renderer/builder.lua

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -258,9 +258,9 @@ function Builder:_build_line(node, idx, num_children)
258258
self.index = self.index + 1
259259

260260
if node.open then
261-
self.depth = self.depth + 2
261+
self.depth = self.depth + 1
262262
self:build(node)
263-
self.depth = self.depth - 2
263+
self.depth = self.depth - 1
264264
end
265265
end
266266

lua/nvim-tree/renderer/components/padding.lua

Lines changed: 36 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -25,30 +25,33 @@ local function get_padding_indent_markers(depth, idx, nodes_number, markers, wit
2525

2626
if depth > 0 then
2727
local has_folder_sibling = check_siblings_for_folder(node, with_arrows)
28-
local rdepth = depth / 2
29-
markers[rdepth] = idx ~= nodes_number
30-
for i = 1, rdepth do
28+
local indent = string.rep(" ", M.config.indent_width - 1)
29+
markers[depth] = idx ~= nodes_number
30+
for i = 1, depth do
3131
local glyph
32-
if idx == nodes_number and i == rdepth then
32+
if idx == nodes_number and i == depth then
33+
local bottom_width = M.config.indent_width
34+
- 2
35+
+ (with_arrows and not inline_arrows and has_folder_sibling and 2 or 0)
3336
glyph = M.config.indent_markers.icons.corner
34-
elseif markers[i] and i == rdepth then
35-
glyph = M.config.indent_markers.icons.item
37+
.. string.rep(M.config.indent_markers.icons.bottom, bottom_width)
38+
.. (M.config.indent_width > 1 and " " or "")
39+
elseif markers[i] and i == depth then
40+
glyph = M.config.indent_markers.icons.item .. indent
3641
elseif markers[i] then
37-
glyph = M.config.indent_markers.icons.edge
42+
glyph = M.config.indent_markers.icons.edge .. indent
3843
else
39-
glyph = M.config.indent_markers.icons.none
44+
glyph = M.config.indent_markers.icons.none .. indent
4045
end
4146

42-
if not with_arrows or (inline_arrows and (rdepth ~= i or not node.nodes)) then
43-
padding = padding .. glyph .. " "
47+
if not with_arrows or (inline_arrows and (depth ~= i or not node.nodes)) then
48+
padding = padding .. glyph
4449
elseif inline_arrows then
4550
padding = padding
46-
elseif idx == nodes_number and i == rdepth and has_folder_sibling then
47-
padding = padding .. base_padding .. glyph .. "── "
48-
elseif rdepth == i and not node.nodes and has_folder_sibling then
49-
padding = padding .. base_padding .. glyph .. " " .. base_padding
51+
elseif idx ~= nodes_number and depth == i and not node.nodes and has_folder_sibling then
52+
padding = padding .. base_padding .. glyph .. base_padding
5053
else
51-
padding = padding .. base_padding .. glyph .. " "
54+
padding = padding .. base_padding .. glyph
5255
end
5356
end
5457
end
@@ -71,11 +74,12 @@ function M.get_padding(depth, idx, nodes_number, node, markers)
7174
local show_arrows = M.config.icons.show.folder_arrow
7275
local show_markers = M.config.indent_markers.enable
7376
local inline_arrows = M.config.indent_markers.inline_arrows
77+
local indent_width = M.config.indent_width
7478

7579
if show_markers then
7680
padding = padding .. get_padding_indent_markers(depth, idx, nodes_number, markers, show_arrows, inline_arrows, node)
7781
else
78-
padding = padding .. string.rep(" ", depth)
82+
padding = padding .. string.rep(" ", depth * indent_width)
7983
end
8084

8185
if show_arrows then
@@ -87,6 +91,22 @@ end
8791

8892
function M.setup(opts)
8993
M.config = opts.renderer
94+
95+
if M.config.indent_width < 1 then
96+
M.config.indent_width = 1
97+
end
98+
99+
local function check_marker(symbol)
100+
if #symbol == 0 then
101+
return " "
102+
end
103+
-- return the first character from the UTF-8 encoded string; we may use utf8.codes from Lua 5.3 when available
104+
return symbol:match "[%z\1-\127\194-\244][\128-\191]*"
105+
end
106+
107+
for k, v in pairs(M.config.indent_markers.icons) do
108+
M.config.indent_markers.icons[k] = check_marker(v)
109+
end
90110
end
91111

92112
return M

0 commit comments

Comments
 (0)