|
| 1 | +local _icons = require "nvim-tree.renderer.icons" |
| 2 | +local utils = require "nvim-tree.utils" |
| 3 | + |
| 4 | +local M = {} |
| 5 | + |
| 6 | +local function build_icons_table() |
| 7 | + return { |
| 8 | + ["M "] = { { icon = M.icon_state.icons.git_icons.staged, hl = "NvimTreeGitStaged" } }, |
| 9 | + [" M"] = { { icon = M.icon_state.icons.git_icons.unstaged, hl = "NvimTreeGitDirty" } }, |
| 10 | + ["C "] = { { icon = M.icon_state.icons.git_icons.staged, hl = "NvimTreeGitStaged" } }, |
| 11 | + [" C"] = { { icon = M.icon_state.icons.git_icons.unstaged, hl = "NvimTreeGitDirty" } }, |
| 12 | + ["CM"] = { { icon = M.icon_state.icons.git_icons.unstaged, hl = "NvimTreeGitDirty" } }, |
| 13 | + [" T"] = { { icon = M.icon_state.icons.git_icons.unstaged, hl = "NvimTreeGitDirty" } }, |
| 14 | + ["MM"] = { |
| 15 | + { icon = M.icon_state.icons.git_icons.staged, hl = "NvimTreeGitStaged" }, |
| 16 | + { icon = M.icon_state.icons.git_icons.unstaged, hl = "NvimTreeGitDirty" }, |
| 17 | + }, |
| 18 | + ["MD"] = { |
| 19 | + { icon = M.icon_state.icons.git_icons.staged, hl = "NvimTreeGitStaged" }, |
| 20 | + }, |
| 21 | + ["A "] = { |
| 22 | + { icon = M.icon_state.icons.git_icons.staged, hl = "NvimTreeGitStaged" }, |
| 23 | + }, |
| 24 | + ["AD"] = { |
| 25 | + { icon = M.icon_state.icons.git_icons.staged, hl = "NvimTreeGitStaged" }, |
| 26 | + }, |
| 27 | + [" A"] = { |
| 28 | + { icon = M.icon_state.icons.git_icons.untracked, hl = "NvimTreeGitNew" }, |
| 29 | + }, |
| 30 | + -- not sure about this one |
| 31 | + ["AA"] = { |
| 32 | + { icon = M.icon_state.icons.git_icons.unmerged, hl = "NvimTreeGitMerge" }, |
| 33 | + { icon = M.icon_state.icons.git_icons.untracked, hl = "NvimTreeGitNew" }, |
| 34 | + }, |
| 35 | + ["AU"] = { |
| 36 | + { icon = M.icon_state.icons.git_icons.unmerged, hl = "NvimTreeGitMerge" }, |
| 37 | + { icon = M.icon_state.icons.git_icons.untracked, hl = "NvimTreeGitNew" }, |
| 38 | + }, |
| 39 | + ["AM"] = { |
| 40 | + { icon = M.icon_state.icons.git_icons.staged, hl = "NvimTreeGitStaged" }, |
| 41 | + { icon = M.icon_state.icons.git_icons.unstaged, hl = "NvimTreeGitDirty" }, |
| 42 | + }, |
| 43 | + ["??"] = { { icon = M.icon_state.icons.git_icons.untracked, hl = "NvimTreeGitNew" } }, |
| 44 | + ["R "] = { { icon = M.icon_state.icons.git_icons.renamed, hl = "NvimTreeGitRenamed" } }, |
| 45 | + [" R"] = { { icon = M.icon_state.icons.git_icons.renamed, hl = "NvimTreeGitRenamed" } }, |
| 46 | + ["RM"] = { |
| 47 | + { icon = M.icon_state.icons.git_icons.unstaged, hl = "NvimTreeGitDirty" }, |
| 48 | + { icon = M.icon_state.icons.git_icons.renamed, hl = "NvimTreeGitRenamed" }, |
| 49 | + }, |
| 50 | + ["UU"] = { { icon = M.icon_state.icons.git_icons.unmerged, hl = "NvimTreeGitMerge" } }, |
| 51 | + ["UD"] = { { icon = M.icon_state.icons.git_icons.unmerged, hl = "NvimTreeGitMerge" } }, |
| 52 | + ["UA"] = { { icon = M.icon_state.icons.git_icons.unmerged, hl = "NvimTreeGitMerge" } }, |
| 53 | + [" D"] = { { icon = M.icon_state.icons.git_icons.deleted, hl = "NvimTreeGitDeleted" } }, |
| 54 | + ["D "] = { { icon = M.icon_state.icons.git_icons.deleted, hl = "NvimTreeGitDeleted" } }, |
| 55 | + ["RD"] = { { icon = M.icon_state.icons.git_icons.deleted, hl = "NvimTreeGitDeleted" } }, |
| 56 | + ["DD"] = { { icon = M.icon_state.icons.git_icons.deleted, hl = "NvimTreeGitDeleted" } }, |
| 57 | + ["DU"] = { |
| 58 | + { icon = M.icon_state.icons.git_icons.deleted, hl = "NvimTreeGitDeleted" }, |
| 59 | + { icon = M.icon_state.icons.git_icons.unmerged, hl = "NvimTreeGitMerge" }, |
| 60 | + }, |
| 61 | + ["!!"] = { { icon = M.icon_state.icons.git_icons.ignored, hl = "NvimTreeGitIgnored" } }, |
| 62 | + dirty = { { icon = M.icon_state.icons.git_icons.unstaged, hl = "NvimTreeGitDirty" } }, |
| 63 | + } |
| 64 | +end |
| 65 | + |
| 66 | +local function empty() return "" end |
| 67 | +local function nil_() end |
| 68 | + |
| 69 | +local function warn_status(git_status) |
| 70 | + utils.warn( |
| 71 | + 'Unrecognized git state "' |
| 72 | + .. git_status |
| 73 | + .. '". Please open up an issue on https://github.com/kyazdani42/nvim-tree.lua/issues with this message.' |
| 74 | + ) |
| 75 | +end |
| 76 | + |
| 77 | +local function get_icons_(node, line, depth, icon_len, hl) |
| 78 | + local git_status = node.git_status |
| 79 | + if not git_status then |
| 80 | + return "" |
| 81 | + end |
| 82 | + |
| 83 | + local icon = "" |
| 84 | + local icons = M.git_icons[git_status] |
| 85 | + if not icons then |
| 86 | + if vim.g.nvim_tree_git_hl ~= 1 then |
| 87 | + warn_status(git_status) |
| 88 | + end |
| 89 | + return "" |
| 90 | + end |
| 91 | + for _, v in ipairs(icons) do |
| 92 | + if #v.icon > 0 then |
| 93 | + table.insert(hl, { v.hl, line, depth + icon_len + #icon, depth + icon_len + #icon + #v.icon }) |
| 94 | + icon = icon .. v.icon .. M.icon_padding |
| 95 | + end |
| 96 | + end |
| 97 | + |
| 98 | + return icon |
| 99 | +end |
| 100 | + |
| 101 | +local git_hl = { |
| 102 | + ["M "] = "NvimTreeFileStaged", |
| 103 | + ["C "] = "NvimTreeFileStaged", |
| 104 | + ["AA"] = "NvimTreeFileStaged", |
| 105 | + ["AD"] = "NvimTreeFileStaged", |
| 106 | + ["MD"] = "NvimTreeFileStaged", |
| 107 | + [" M"] = "NvimTreeFileDirty", |
| 108 | + ["CM"] = "NvimTreeFileDirty", |
| 109 | + [" C"] = "NvimTreeFileDirty", |
| 110 | + [" T"] = "NvimTreeFileDirty", |
| 111 | + ["MM"] = "NvimTreeFileDirty", |
| 112 | + ["AM"] = "NvimTreeFileDirty", |
| 113 | + dirty = "NvimTreeFileDirty", |
| 114 | + ["A "] = "NvimTreeFileNew", |
| 115 | + ["??"] = "NvimTreeFileNew", |
| 116 | + ["AU"] = "NvimTreeFileMerge", |
| 117 | + ["UU"] = "NvimTreeFileMerge", |
| 118 | + ["UD"] = "NvimTreeFileMerge", |
| 119 | + ["DU"] = "NvimTreeFileMerge", |
| 120 | + ["UA"] = "NvimTreeFileMerge", |
| 121 | + [" D"] = "NvimTreeFileDeleted", |
| 122 | + ["DD"] = "NvimTreeFileDeleted", |
| 123 | + ["RD"] = "NvimTreeFileDeleted", |
| 124 | + ["D "] = "NvimTreeFileDeleted", |
| 125 | + ["R "] = "NvimTreeFileRenamed", |
| 126 | + ["RM"] = "NvimTreeFileRenamed", |
| 127 | + [" R"] = "NvimTreeFileRenamed", |
| 128 | + ["!!"] = "NvimTreeGitIgnored", |
| 129 | + [" A"] = "none", |
| 130 | +} |
| 131 | + |
| 132 | +local function get_highlight_(node) |
| 133 | + local git_status = node.git_status |
| 134 | + if not git_status then |
| 135 | + return |
| 136 | + end |
| 137 | + |
| 138 | + return git_hl[git_status] |
| 139 | +end |
| 140 | + |
| 141 | +function M.get_icons() |
| 142 | + return empty() |
| 143 | +end |
| 144 | + |
| 145 | +function M.get_highlight() |
| 146 | + return nil_() |
| 147 | +end |
| 148 | + |
| 149 | +M.icon_padding = vim.g.nvim_tree_icon_padding or " " |
| 150 | +M.icon_state = _icons.get_config() |
| 151 | +M.git_icons = build_icons_table() |
| 152 | + |
| 153 | +function M.reload() |
| 154 | + M.icon_state = _icons.get_config() |
| 155 | + M.icon_padding = vim.g.nvim_tree_icon_padding or " " |
| 156 | + M.git_icons = build_icons_table() |
| 157 | + if M.icon_state.show_git_icon then |
| 158 | + M.get_icons = get_icons_ |
| 159 | + else |
| 160 | + M.get_icons = empty |
| 161 | + end |
| 162 | + if vim.g.nvim_tree_git_hl == 1 then |
| 163 | + M.get_highlight = get_highlight_ |
| 164 | + else |
| 165 | + M.get_highlight = nil_ |
| 166 | + end |
| 167 | +end |
| 168 | + |
| 169 | +return M |
0 commit comments