Skip to content

feat: add config option for a tree indent width #1505

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 8 commits into from
Aug 29, 2022
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
10 changes: 8 additions & 2 deletions doc/nvim-tree-lua.txt
Original file line number Diff line number Diff line change
Expand Up @@ -213,13 +213,15 @@ Subsequent calls to setup will replace the previous configuration.
full_name = false,
highlight_opened_files = "none",
root_folder_modifier = ":~",
indent_width = 2,
indent_markers = {
enable = false,
inline_arrows = true,
icons = {
corner = "└",
edge = "│",
item = "│",
bottom = "─",
none = " ",
},
},
Expand Down Expand Up @@ -709,6 +711,10 @@ UI rendering setup
available options.
Type: `string`, Default: `":~"`

*nvim-tree.renderer.indent_width*
Number of spaces for an each tree nesting level. Minimum 1.
Type: `number`, Default: `2`

*nvim-tree.renderer.indent_markers*
Configuration options for tree indent markers.

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

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

*nvim-tree.renderer.icons*
Configuration options for icons.
Expand Down
2 changes: 2 additions & 0 deletions lua/nvim-tree.lua
Original file line number Diff line number Diff line change
Expand Up @@ -476,13 +476,15 @@ local DEFAULT_OPTS = { -- BEGIN_DEFAULT_OPTS
full_name = false,
highlight_opened_files = "none",
root_folder_modifier = ":~",
indent_width = 2,
indent_markers = {
enable = false,
inline_arrows = true,
icons = {
corner = "└",
edge = "│",
item = "│",
bottom = "─",
none = " ",
},
},
Expand Down
4 changes: 2 additions & 2 deletions lua/nvim-tree/renderer/builder.lua
Original file line number Diff line number Diff line change
Expand Up @@ -258,9 +258,9 @@ function Builder:_build_line(node, idx, num_children)
self.index = self.index + 1

if node.open then
self.depth = self.depth + 2
self.depth = self.depth + 1
self:build(node)
self.depth = self.depth - 2
self.depth = self.depth - 1
end
end

Expand Down
52 changes: 36 additions & 16 deletions lua/nvim-tree/renderer/components/padding.lua
Original file line number Diff line number Diff line change
Expand Up @@ -25,30 +25,33 @@ local function get_padding_indent_markers(depth, idx, nodes_number, markers, wit

if depth > 0 then
local has_folder_sibling = check_siblings_for_folder(node, with_arrows)
local rdepth = depth / 2
markers[rdepth] = idx ~= nodes_number
for i = 1, rdepth do
local indent = string.rep(" ", M.config.indent_width - 1)
markers[depth] = idx ~= nodes_number
for i = 1, depth do
local glyph
if idx == nodes_number and i == rdepth then
if idx == nodes_number and i == depth then
local bottom_width = M.config.indent_width
- 2
+ (with_arrows and not inline_arrows and has_folder_sibling and 2 or 0)
glyph = M.config.indent_markers.icons.corner
elseif markers[i] and i == rdepth then
glyph = M.config.indent_markers.icons.item
.. string.rep(M.config.indent_markers.icons.bottom, bottom_width)
.. (M.config.indent_width > 1 and " " or "")
elseif markers[i] and i == depth then
glyph = M.config.indent_markers.icons.item .. indent
elseif markers[i] then
glyph = M.config.indent_markers.icons.edge
glyph = M.config.indent_markers.icons.edge .. indent
else
glyph = M.config.indent_markers.icons.none
glyph = M.config.indent_markers.icons.none .. indent
end

if not with_arrows or (inline_arrows and (rdepth ~= i or not node.nodes)) then
padding = padding .. glyph .. " "
if not with_arrows or (inline_arrows and (depth ~= i or not node.nodes)) then
padding = padding .. glyph
elseif inline_arrows then
padding = padding
elseif idx == nodes_number and i == rdepth and has_folder_sibling then
padding = padding .. base_padding .. glyph .. "── "
elseif rdepth == i and not node.nodes and has_folder_sibling then
padding = padding .. base_padding .. glyph .. " " .. base_padding
elseif idx ~= nodes_number and depth == i and not node.nodes and has_folder_sibling then
padding = padding .. base_padding .. glyph .. base_padding
else
padding = padding .. base_padding .. glyph .. " "
padding = padding .. base_padding .. glyph
end
end
end
Expand All @@ -71,11 +74,12 @@ function M.get_padding(depth, idx, nodes_number, node, markers)
local show_arrows = M.config.icons.show.folder_arrow
local show_markers = M.config.indent_markers.enable
local inline_arrows = M.config.indent_markers.inline_arrows
local indent_width = M.config.indent_width

if show_markers then
padding = padding .. get_padding_indent_markers(depth, idx, nodes_number, markers, show_arrows, inline_arrows, node)
else
padding = padding .. string.rep(" ", depth)
padding = padding .. string.rep(" ", depth * indent_width)
end

if show_arrows then
Expand All @@ -87,6 +91,22 @@ end

function M.setup(opts)
M.config = opts.renderer

if M.config.indent_width < 1 then
M.config.indent_width = 1
end

local function check_marker(symbol)
if #symbol == 0 then
return " "
end
-- return the first character from the UTF-8 encoded string; we may use utf8.codes from Lua 5.3 when available
return symbol:match "[%z\1-\127\194-\244][\128-\191]*"
end

for k, v in pairs(M.config.indent_markers.icons) do
M.config.indent_markers.icons[k] = check_marker(v)
end
end

return M