diff --git a/lua/nvim-tree/actions/movements.lua b/lua/nvim-tree/actions/movements.lua index c2459c7755e..49a36c7760b 100644 --- a/lua/nvim-tree/actions/movements.lua +++ b/lua/nvim-tree/actions/movements.lua @@ -105,7 +105,7 @@ end function M.find_git_item(where) return function() local node_cur = lib.get_node_at_cursor() - local nodes_by_line = lib.get_nodes_by_line(core.get_explorer().nodes, core.get_nodes_starting_line()) + local nodes_by_line = utils.get_nodes_by_line(core.get_explorer().nodes, core.get_nodes_starting_line()) local cur, first, prev, nex = nil, nil, nil, nil for line, node in pairs(nodes_by_line) do diff --git a/lua/nvim-tree/diagnostics.lua b/lua/nvim-tree/diagnostics.lua index d6e23961179..5cf69d460bb 100644 --- a/lua/nvim-tree/diagnostics.lua +++ b/lua/nvim-tree/diagnostics.lua @@ -32,7 +32,7 @@ local function add_sign(linenr, severity) return end local sign_name = sign_names[severity][1] - vim.fn.sign_place(1, GROUP, sign_name, buf, { lnum = linenr + 1 }) + vim.fn.sign_place(1, GROUP, sign_name, buf, { lnum = linenr }) end local function from_nvim_lsp() @@ -131,18 +131,17 @@ function M.update() local bufpath = utils.canonical_path(bufname) log.line("diagnostics", " bufpath '%s' severity %d", bufpath, severity) if 0 < severity and severity < 5 then - local node, line = utils.find_node(core.get_explorer().nodes, function(node) + local nodes_by_line = utils.get_nodes_by_line(core.get_explorer().nodes, core.get_nodes_starting_line()) + for line, node in pairs(nodes_by_line) do local nodepath = utils.canonical_path(node.absolute_path) - log.line("diagnostics", " checking nodepath '%s'", nodepath) - if M.show_on_dirs and not node.open then - return vim.startswith(bufpath, nodepath) - else - return nodepath == bufpath + log.line("diagnostics", " %d checking nodepath '%s'", line, nodepath) + if M.show_on_dirs and vim.startswith(bufpath, nodepath) then + log.line("diagnostics", " matched fold node '%s'", node.absolute_path) + add_sign(line, severity) + elseif nodepath == bufpath then + log.line("diagnostics", " matched file node '%s'", node.absolute_path) + add_sign(line, severity) end - end) - if node then - log.line("diagnostics", " matched node '%s'", node.absolute_path) - add_sign(line, severity) end end end diff --git a/lua/nvim-tree/lib.lua b/lua/nvim-tree/lib.lua index 37e0b0c2c1b..649a468bbc3 100644 --- a/lua/nvim-tree/lib.lua +++ b/lua/nvim-tree/lib.lua @@ -3,30 +3,12 @@ local api = vim.api local renderer = require "nvim-tree.renderer" local view = require "nvim-tree.view" local core = require "nvim-tree.core" +local utils = require "nvim-tree.utils" local M = { target_winid = nil, } -function M.get_nodes_by_line(nodes_all, line_start) - local nodes_by_line = {} - local line = line_start - local function iter(nodes) - for _, node in ipairs(nodes) do - nodes_by_line[line] = node - line = line + 1 - if node.open == true then - local child = iter(node.nodes) - if child ~= nil then - return child - end - end - end - end - iter(nodes_all) - return nodes_by_line -end - function M.get_node_at_cursor() if not core.get_explorer() then return @@ -39,14 +21,14 @@ function M.get_node_at_cursor() local line = cursor[1] if view.is_help_ui() then local help_lines = require("nvim-tree.renderer.help").compute_lines() - local help_text = M.get_nodes_by_line(help_lines, 1)[line] + local help_text = utils.get_nodes_by_line(help_lines, 1)[line] return { name = help_text } else if line == 1 and core.get_explorer().cwd ~= "/" and view.is_root_folder_visible() then return { name = ".." } end - return M.get_nodes_by_line(core.get_explorer().nodes, core.get_nodes_starting_line())[line] + return utils.get_nodes_by_line(core.get_explorer().nodes, core.get_nodes_starting_line())[line] end end diff --git a/lua/nvim-tree/utils.lua b/lua/nvim-tree/utils.lua index f557c9332dd..017c5765c22 100644 --- a/lua/nvim-tree/utils.lua +++ b/lua/nvim-tree/utils.lua @@ -123,6 +123,29 @@ function M.find_node(nodes, fn) return node, i end +-- return visible nodes indexed by line +-- @param nodes_all list of node +-- @param line_start first index +---@return table +function M.get_nodes_by_line(nodes_all, line_start) + local nodes_by_line = {} + local line = line_start + local function iter(nodes) + for _, node in ipairs(nodes) do + nodes_by_line[line] = node + line = line + 1 + if node.open == true then + local child = iter(node.nodes) + if child ~= nil then + return child + end + end + end + end + iter(nodes_all) + return nodes_by_line +end + ---Matching executable files in Windows. ---@param ext string ---@return boolean