|
| 1 | +local git = require "nvim-tree.git" |
| 2 | +local view = require "nvim-tree.view" |
| 3 | +local core = require "nvim-tree.core" |
| 4 | +local explorer_node = require "nvim-tree.explorer.node" |
| 5 | +local Iterator = require "nvim-tree.iterators.node-iterator" |
| 6 | + |
| 7 | +local M = {} |
| 8 | + |
| 9 | +---@param explorer Explorer|nil |
| 10 | +---@param projects table |
| 11 | +local function refresh_nodes(explorer, projects) |
| 12 | + Iterator.builder({ explorer }) |
| 13 | + :applier(function(n) |
| 14 | + if n.nodes then |
| 15 | + local toplevel = git.get_toplevel(n.cwd or n.link_to or n.absolute_path) |
| 16 | + if explorer then |
| 17 | + explorer:reload(n, projects[toplevel] or {}) |
| 18 | + end |
| 19 | + end |
| 20 | + end) |
| 21 | + :recursor(function(n) |
| 22 | + return n.group_next and { n.group_next } or (n.open and n.nodes) |
| 23 | + end) |
| 24 | + :iterate() |
| 25 | +end |
| 26 | + |
| 27 | +---@param parent_node Node|nil |
| 28 | +---@param projects table |
| 29 | +function M.reload_node_status(parent_node, projects) |
| 30 | + if parent_node == nil then |
| 31 | + return |
| 32 | + end |
| 33 | + |
| 34 | + local toplevel = git.get_toplevel(parent_node.absolute_path) |
| 35 | + local status = projects[toplevel] or {} |
| 36 | + for _, node in ipairs(parent_node.nodes) do |
| 37 | + explorer_node.update_git_status(node, explorer_node.is_git_ignored(parent_node), status) |
| 38 | + if node.nodes and #node.nodes > 0 then |
| 39 | + M.reload_node_status(node, projects) |
| 40 | + end |
| 41 | + end |
| 42 | +end |
| 43 | + |
| 44 | +local event_running = false |
| 45 | +function M.reload_explorer() |
| 46 | + local explorer = core.get_explorer() |
| 47 | + if event_running or not explorer or vim.v.exiting ~= vim.NIL then |
| 48 | + return |
| 49 | + end |
| 50 | + event_running = true |
| 51 | + |
| 52 | + local projects = git.reload() |
| 53 | + refresh_nodes(explorer, projects) |
| 54 | + if view.is_visible() then |
| 55 | + explorer.renderer:draw() |
| 56 | + end |
| 57 | + event_running = false |
| 58 | +end |
| 59 | + |
| 60 | +function M.reload_git() |
| 61 | + local explorer = core.get_explorer() |
| 62 | + if not explorer or not git.config.git.enable or event_running then |
| 63 | + return |
| 64 | + end |
| 65 | + event_running = true |
| 66 | + |
| 67 | + local projects = git.reload() |
| 68 | + M.reload_node_status(explorer, projects) |
| 69 | + explorer.renderer:draw() |
| 70 | + event_running = false |
| 71 | +end |
| 72 | + |
| 73 | +return M |
0 commit comments