From 7988dd407c373489c6383918aa826188f9ef304f Mon Sep 17 00:00:00 2001 From: kiyan42 Date: Fri, 19 Jun 2020 19:14:54 +0200 Subject: [PATCH 1/2] add indent markers --- lua/lib/renderer.lua | 49 ++++++++++++++++++++++++++++++++++---------- 1 file changed, 38 insertions(+), 11 deletions(-) diff --git a/lua/lib/renderer.lua b/lua/lib/renderer.lua index 39e6f3e9..4dce3e52 100644 --- a/lua/lib/renderer.lua +++ b/lua/lib/renderer.lua @@ -87,6 +87,32 @@ if icon_state.show_git_icon then end end +local get_padding = function(depth) + return string.rep(' ', depth), 0 +end + +local add_padding_glyph = true +if vim.g.lua_tree_indent_markers == 1 then + get_padding = function(depth, idx, tree, node) + local padding = "" + local offset = 0 + if depth ~= 0 then + padding = string.rep(" ", depth-2) + if add_padding_glyph and (idx == #tree.entries or node.open) then + padding = padding..'└ ' + offset = string.len('└ ') -2 + add_padding_glyph = false + elseif add_padding_glyph then + padding = padding..'│ ' + offset = string.len('│ ') -2 + else + padding = padding..' ' + end + end + return padding, offset + end +end + local picture = { jpg = true, jpeg = true, @@ -108,12 +134,13 @@ local function update_draw_data(tree, depth) index = 1 end - for _, node in ipairs(tree.entries) do - local padding = string.rep(" ", depth) + add_padding_glyph = true + for idx, node in ipairs(tree.entries) do + local padding, offset = get_padding(depth, idx, tree, node, add_padding_glyph) if node.entries then local icon = get_folder_icon(node.open) - local git_icon = get_git_icons(node, index, depth+#node.name, #icon+1) - set_folder_hl(index, depth, #icon, #node.name) + local git_icon = get_git_icons(node, index, depth+offset+#node.name, #icon+1) + set_folder_hl(index, depth+offset, #icon, #node.name) index = index + 1 if node.open then table.insert(lines, padding..icon..node.name.." "..git_icon) @@ -122,7 +149,7 @@ local function update_draw_data(tree, depth) table.insert(lines, padding..icon..node.name.." "..git_icon) end elseif node.link_to then - table.insert(hl, { 'LuaTreeSymlink', index, depth, -1 }) + table.insert(hl, { 'LuaTreeSymlink', index, depth+offset, -1 }) table.insert(lines, padding..node.name.." ➛ "..node.link_to) index = index + 1 @@ -131,17 +158,17 @@ local function update_draw_data(tree, depth) local git_icons if special[node.name] then icon = "" - git_icons = get_git_icons(node, index, depth, 0) - table.insert(hl, {'LuaTreeSpecialFile', index, depth+#git_icons, -1}) + git_icons = get_git_icons(node, index, depth+offset, 0) + table.insert(hl, {'LuaTreeSpecialFile', index, depth+offset+#git_icons, -1}) else - icon = get_file_icon(node.name, node.extension, index, depth) - git_icons = get_git_icons(node, index, depth, #icon) + icon = get_file_icon(node.name, node.extension, index, depth+offset) + git_icons = get_git_icons(node, index, depth+offset, #icon) end table.insert(lines, padding..icon..git_icons..node.name) if node.executable then - table.insert(hl, {'LuaTreeExecFile', index, depth+#icon+#git_icons, -1 }) + table.insert(hl, {'LuaTreeExecFile', index, depth+offset+#icon+#git_icons, -1 }) elseif picture[node.extension] then - table.insert(hl, {'LuaTreeImageFile', index, depth+#icon+#git_icons, -1 }) + table.insert(hl, {'LuaTreeImageFile', index, depth+offset+#icon+#git_icons, -1 }) end index = index + 1 end From 4f499d9e53d7c1860c7be10c1a864718ae7c2850 Mon Sep 17 00:00:00 2001 From: kiyan42 Date: Sat, 20 Jun 2020 15:34:06 +0200 Subject: [PATCH 2/2] finish indent markers properly --- README.md | 2 ++ doc/nvim-tree-lua.txt | 4 ++++ doc/tags | 1 + lua/lib/colors.lua | 1 + lua/lib/renderer.lua | 47 ++++++++++++++++++++----------------------- 5 files changed, 30 insertions(+), 25 deletions(-) diff --git a/README.md b/README.md index e3de66d8..cfe6656c 100644 --- a/README.md +++ b/README.md @@ -30,6 +30,7 @@ let g:lua_tree_ignore = [ '.git', 'node_modules', '.cache' ] "empty by default let g:lua_tree_auto_open = 1 "0 by default, opens the tree when typing `vim $DIR` or `vim` let g:lua_tree_auto_close = 1 "0 by default, closes the tree when it's the last window let g:lua_tree_follow = 1 "0 by default, this option allows the cursor to be updated when entering a buffer +let g:lua_tree_indent_markers = 1 "0 by default, this option shows indent markers when folders are open let g:lua_tree_show_icons = { \ 'git': 1, \ 'folders': 0, @@ -113,6 +114,7 @@ This plugin is very fast because it uses the `libuv` `scandir` and `scandir_next - Change directory with `.` - Add / Rename / delete files - Git integration +- Indent markers - Mouse support - It's fast diff --git a/doc/nvim-tree-lua.txt b/doc/nvim-tree-lua.txt index 411276df..3becdce3 100644 --- a/doc/nvim-tree-lua.txt +++ b/doc/nvim-tree-lua.txt @@ -121,6 +121,9 @@ Can be `0` or `1`. When `1`, will disable all keybindings by the plugin. |g:lua_tree_bindings| as well as default bindings will not take effect. Default is 0 +|g:lua_tree_indent_markers| *g:lua_tree_indent_markers* + +Can be `0` or `1`. When `1`, will display indent markers when folders are open ============================================================================== INFORMATIONS *nvim-tree-info* @@ -209,6 +212,7 @@ LuaTreeExecFile LuaTreeSpecialFile LuaTreeImageFile LuaTreeMarkdownFile +LuaTreeIndentMarker LuaTreeLicenseIcon LuaTreeYamlIcon diff --git a/doc/tags b/doc/tags index 516eddf1..4c8b1c24 100644 --- a/doc/tags +++ b/doc/tags @@ -9,6 +9,7 @@ g:lua_tree_bindings nvim-tree-lua.txt /*g:lua_tree_bindings* g:lua_tree_follow nvim-tree-lua.txt /*g:lua_tree_follow* g:lua_tree_icons nvim-tree-lua.txt /*g:lua_tree_icons* g:lua_tree_ignore nvim-tree-lua.txt /*g:lua_tree_ignore* +g:lua_tree_indent_markers nvim-tree-lua.txt /*g:lua_tree_indent_markers* g:lua_tree_show_icons nvim-tree-lua.txt /*g:lua_tree_show_icons* g:lua_tree_side nvim-tree-lua.txt /*g:lua_tree_side* g:lua_tree_size nvim-tree-lua.txt /*g:lua_tree_size* diff --git a/lua/lib/colors.lua b/lua/lib/colors.lua index 0e691bed..53d1c72b 100644 --- a/lua/lib/colors.lua +++ b/lua/lib/colors.lua @@ -29,6 +29,7 @@ local function get_hl_groups() local colors = get_colors() return { + IndentMarker = { fg = '#90a4ae' }, Symlink = { gui = 'bold', fg = colors.cyan }, FolderIcon = { fg = '#90a4ae' }, diff --git a/lua/lib/renderer.lua b/lua/lib/renderer.lua index 4dce3e52..82e34178 100644 --- a/lua/lib/renderer.lua +++ b/lua/lib/renderer.lua @@ -88,28 +88,22 @@ if icon_state.show_git_icon then end local get_padding = function(depth) - return string.rep(' ', depth), 0 + return string.rep(' ', depth) end -local add_padding_glyph = true if vim.g.lua_tree_indent_markers == 1 then get_padding = function(depth, idx, tree, node) local padding = "" - local offset = 0 if depth ~= 0 then - padding = string.rep(" ", depth-2) - if add_padding_glyph and (idx == #tree.entries or node.open) then - padding = padding..'└ ' - offset = string.len('└ ') -2 - add_padding_glyph = false - elseif add_padding_glyph then - padding = padding..'│ ' - offset = string.len('│ ') -2 - else - padding = padding..' ' + for i=1,depth/2 do + if idx == #tree.entries and i == depth/2 then + padding = padding..'└ ' + else + padding = padding..'│ ' + end end end - return padding, offset + return padding end end @@ -134,13 +128,16 @@ local function update_draw_data(tree, depth) index = 1 end - add_padding_glyph = true for idx, node in ipairs(tree.entries) do - local padding, offset = get_padding(depth, idx, tree, node, add_padding_glyph) + local padding = get_padding(depth, idx, tree, node) + local offset = string.len(padding) + if depth > 0 then + table.insert(hl, { 'LuaTreeIndentMarker', index, 0, offset }) + end if node.entries then local icon = get_folder_icon(node.open) - local git_icon = get_git_icons(node, index, depth+offset+#node.name, #icon+1) - set_folder_hl(index, depth+offset, #icon, #node.name) + local git_icon = get_git_icons(node, index, offset+#node.name, #icon+1) + set_folder_hl(index, offset, #icon, #node.name) index = index + 1 if node.open then table.insert(lines, padding..icon..node.name.." "..git_icon) @@ -149,7 +146,7 @@ local function update_draw_data(tree, depth) table.insert(lines, padding..icon..node.name.." "..git_icon) end elseif node.link_to then - table.insert(hl, { 'LuaTreeSymlink', index, depth+offset, -1 }) + table.insert(hl, { 'LuaTreeSymlink', index, offset, -1 }) table.insert(lines, padding..node.name.." ➛ "..node.link_to) index = index + 1 @@ -158,17 +155,17 @@ local function update_draw_data(tree, depth) local git_icons if special[node.name] then icon = "" - git_icons = get_git_icons(node, index, depth+offset, 0) - table.insert(hl, {'LuaTreeSpecialFile', index, depth+offset+#git_icons, -1}) + git_icons = get_git_icons(node, index, offset, 0) + table.insert(hl, {'LuaTreeSpecialFile', index, offset+#git_icons, -1}) else - icon = get_file_icon(node.name, node.extension, index, depth+offset) - git_icons = get_git_icons(node, index, depth+offset, #icon) + icon = get_file_icon(node.name, node.extension, index, offset) + git_icons = get_git_icons(node, index, offset, #icon) end table.insert(lines, padding..icon..git_icons..node.name) if node.executable then - table.insert(hl, {'LuaTreeExecFile', index, depth+offset+#icon+#git_icons, -1 }) + table.insert(hl, {'LuaTreeExecFile', index, offset+#icon+#git_icons, -1 }) elseif picture[node.extension] then - table.insert(hl, {'LuaTreeImageFile', index, depth+offset+#icon+#git_icons, -1 }) + table.insert(hl, {'LuaTreeImageFile', index, offset+#icon+#git_icons, -1 }) end index = index + 1 end