From 1bdef08cfa38a6509a789e7655cce5748d1d8dfa Mon Sep 17 00:00:00 2001 From: Alexander Courtis Date: Sat, 9 Apr 2022 22:59:38 +1000 Subject: [PATCH] #1059 protect against duplicates (#1143) --- lua/nvim-tree/explorer/explore.lua | 7 ++++++- lua/nvim-tree/explorer/reload.lua | 10 +--------- lua/nvim-tree/utils.lua | 8 ++++++++ 3 files changed, 15 insertions(+), 10 deletions(-) diff --git a/lua/nvim-tree/explorer/explore.lua b/lua/nvim-tree/explorer/explore.lua index cd73db44..78cb7137 100644 --- a/lua/nvim-tree/explorer/explore.lua +++ b/lua/nvim-tree/explorer/explore.lua @@ -21,9 +21,14 @@ local function populate_children(handle, cwd, node, status) break end + local nodes_by_path = utils.key_by(node.nodes, "absolute_path") local abs = utils.path_join { cwd, name } t = get_type_from(t, abs) - if not filters.should_ignore(abs) and not filters.should_ignore_git(abs, status.files) then + if + not filters.should_ignore(abs) + and not filters.should_ignore_git(abs, status.files) + and not nodes_by_path[abs] + then if t == "directory" and uv.fs_access(abs, "R") then table.insert(node.nodes, builders.folder(abs, name, status, node_ignored)) elseif t == "file" then diff --git a/lua/nvim-tree/explorer/reload.lua b/lua/nvim-tree/explorer/reload.lua index d362c517..054d0bd9 100644 --- a/lua/nvim-tree/explorer/reload.lua +++ b/lua/nvim-tree/explorer/reload.lua @@ -9,14 +9,6 @@ local sorters = require "nvim-tree.explorer.sorters" local M = {} -local function key_by(nodes, key) - local v = {} - for _, node in ipairs(nodes) do - v[node[key]] = node - end - return v -end - local function update_status(nodes_by_path, node_ignored, status) return function(node) if nodes_by_path[node.absolute_path] then @@ -46,7 +38,7 @@ function M.reload(node, status) local child_names = {} local node_ignored = node.git_status == "!!" - local nodes_by_path = key_by(node.nodes, "absolute_path") + local nodes_by_path = utils.key_by(node.nodes, "absolute_path") while true do local name, t = uv.fs_scandir_next(handle) if not name then diff --git a/lua/nvim-tree/utils.lua b/lua/nvim-tree/utils.lua index a0dbcb6b..f557c933 100644 --- a/lua/nvim-tree/utils.lua +++ b/lua/nvim-tree/utils.lua @@ -210,4 +210,12 @@ function M.format_bytes(bytes) return (units[pow] == nil) and (bytes .. "B") or (value .. units[pow]) end +function M.key_by(tbl, key) + local keyed = {} + for _, val in ipairs(tbl) do + keyed[val[key]] = val + end + return keyed +end + return M