|
| 1 | +local M = {} |
| 2 | + |
| 3 | +-- node.git_status structure: |
| 4 | +-- { |
| 5 | +-- file = string | nil, |
| 6 | +-- dir = { |
| 7 | +-- direct = { string } | nil, |
| 8 | +-- indirect = { string } | nil, |
| 9 | +-- } | nil, |
| 10 | +-- } |
| 11 | + |
| 12 | +local function get_dir_git_status(parent_ignored, status, absolute_path) |
| 13 | + if parent_ignored then |
| 14 | + return { file = "!!" } |
| 15 | + end |
| 16 | + |
| 17 | + return { |
| 18 | + file = status.files and status.files[absolute_path], |
| 19 | + dir = status.dirs and { |
| 20 | + direct = status.dirs.direct[absolute_path], |
| 21 | + indirect = status.dirs.indirect[absolute_path], |
| 22 | + }, |
| 23 | + } |
| 24 | +end |
| 25 | + |
| 26 | +local function get_git_status(parent_ignored, status, absolute_path) |
| 27 | + local file_status = parent_ignored and "!!" or status.files and status.files[absolute_path] |
| 28 | + return { file = file_status } |
| 29 | +end |
| 30 | + |
| 31 | +function M.has_one_child_folder(node) |
| 32 | + return #node.nodes == 1 and node.nodes[1].nodes and vim.loop.fs_access(node.nodes[1].absolute_path, "R") |
| 33 | +end |
| 34 | + |
| 35 | +function M.update_git_status(node, parent_ignored, status) |
| 36 | + local get_status |
| 37 | + if node.nodes then |
| 38 | + get_status = get_dir_git_status |
| 39 | + else |
| 40 | + get_status = get_git_status |
| 41 | + end |
| 42 | + |
| 43 | + -- status of the node's absolute path |
| 44 | + node.git_status = get_status(parent_ignored, status, node.absolute_path) |
| 45 | + |
| 46 | + -- status of the link target, if the link itself is not dirty |
| 47 | + if node.link_to and not node.git_status then |
| 48 | + node.git_status = get_status(parent_ignored, status, node.link_to) |
| 49 | + end |
| 50 | +end |
| 51 | + |
| 52 | +function M.get_git_status(node) |
| 53 | + local git_status = node.git_status |
| 54 | + if not git_status then |
| 55 | + -- status doesn't exist |
| 56 | + return nil |
| 57 | + end |
| 58 | + |
| 59 | + if not node.nodes then |
| 60 | + -- file |
| 61 | + return git_status.file and { git_status.file } |
| 62 | + end |
| 63 | + |
| 64 | + -- dir |
| 65 | + if not M.config.git.show_on_dirs then |
| 66 | + return nil |
| 67 | + end |
| 68 | + |
| 69 | + local status = {} |
| 70 | + if not node.open or M.config.git.show_on_open_dirs then |
| 71 | + -- dir is closed or we should show on open_dirs |
| 72 | + if git_status.file ~= nil then |
| 73 | + table.insert(status, git_status.file) |
| 74 | + end |
| 75 | + if git_status.dir ~= nil then |
| 76 | + if git_status.dir.direct ~= nil then |
| 77 | + for _, s in pairs(node.git_status.dir.direct) do |
| 78 | + table.insert(status, s) |
| 79 | + end |
| 80 | + end |
| 81 | + if git_status.dir.indirect ~= nil then |
| 82 | + for _, s in pairs(node.git_status.dir.indirect) do |
| 83 | + table.insert(status, s) |
| 84 | + end |
| 85 | + end |
| 86 | + end |
| 87 | + else |
| 88 | + -- dir is open and we shouldn't show on open_dirs |
| 89 | + if git_status.file ~= nil then |
| 90 | + table.insert(status, git_status.file) |
| 91 | + end |
| 92 | + if git_status.dir ~= nil and git_status.dir.direct ~= nil then |
| 93 | + local deleted = { |
| 94 | + [" D"] = true, |
| 95 | + ["D "] = true, |
| 96 | + ["RD"] = true, |
| 97 | + ["DD"] = true, |
| 98 | + } |
| 99 | + for _, s in pairs(node.git_status.dir.direct) do |
| 100 | + if deleted[s] then |
| 101 | + table.insert(status, s) |
| 102 | + end |
| 103 | + end |
| 104 | + end |
| 105 | + end |
| 106 | + if #status == 0 then |
| 107 | + return nil |
| 108 | + else |
| 109 | + return status |
| 110 | + end |
| 111 | +end |
| 112 | + |
| 113 | +function M.is_git_ignored(node) |
| 114 | + return node.git_status and node.git_status.file == "!!" |
| 115 | +end |
| 116 | + |
| 117 | +function M.node_destroy(node) |
| 118 | + if not node then |
| 119 | + return |
| 120 | + end |
| 121 | + |
| 122 | + if node.watcher then |
| 123 | + node.watcher:destroy() |
| 124 | + end |
| 125 | +end |
| 126 | + |
| 127 | +function M.setup(opts) |
| 128 | + M.config = { |
| 129 | + git = opts.git, |
| 130 | + } |
| 131 | +end |
| 132 | + |
| 133 | +return M |
0 commit comments