Skip to content

Commit a25bd8b

Browse files
committed
normalise git sign creation and tidy
1 parent e2fa0ef commit a25bd8b

File tree

1 file changed

+59
-58
lines changed
  • lua/nvim-tree/renderer/decorator

1 file changed

+59
-58
lines changed

lua/nvim-tree/renderer/decorator/git.lua

Lines changed: 59 additions & 58 deletions
Original file line numberDiff line numberDiff line change
@@ -6,23 +6,62 @@ local ICON_PLACEMENT = require("nvim-tree.enum").ICON_PLACEMENT
66

77
local Decorator = require "nvim-tree.renderer.decorator"
88

9+
--- @class HighlightedStringGit: HighlightedString
10+
--- @field ord number decreasing priority
11+
912
--- @class DecoratorGit: Decorator
10-
--- @field file_hl string[]
11-
--- @field folder_hl string[]
12-
--- @field git_icons table
13+
--- @field file_hl table<string, string> by porcelain status e.g. "AM"
14+
--- @field folder_hl table<string, string> by porcelain status
15+
--- @field icons_by_status HighlightedStringGit[] by human status
16+
--- @field icons_by_xy table<string, HighlightedStringGit[]> by porcelain status
1317
local DecoratorGit = Decorator:new()
1418

15-
local function build_icons_table(i)
16-
local icons = {
17-
staged = { str = i.staged, hl = { "NvimTreeGitStagedIcon" }, ord = 1 },
18-
unstaged = { str = i.unstaged, hl = { "NvimTreeGitDirtyIcon" }, ord = 2 },
19-
renamed = { str = i.renamed, hl = { "NvimTreeGitRenamedIcon" }, ord = 3 },
20-
deleted = { str = i.deleted, hl = { "NvimTreeGitDeletedIcon" }, ord = 4 },
21-
unmerged = { str = i.unmerged, hl = { "NvimTreeGitMergeIcon" }, ord = 5 },
22-
untracked = { str = i.untracked, hl = { "NvimTreeGitNewIcon" }, ord = 6 },
23-
ignored = { str = i.ignored, hl = { "NvimTreeGitIgnoredIcon" }, ord = 7 },
19+
--- @param opts table
20+
--- @return DecoratorGit
21+
function DecoratorGit:new(opts)
22+
local o = Decorator.new(self, {
23+
enabled = opts.git.enable,
24+
hl_pos = HL_POSITION[opts.renderer.highlight_git] or HL_POSITION.none,
25+
icon_placement = ICON_PLACEMENT[opts.renderer.icons.git_placement] or ICON_PLACEMENT.none,
26+
})
27+
---@cast o DecoratorGit
28+
29+
if not o.enabled then
30+
return o
31+
end
32+
33+
if o.hl_pos ~= HL_POSITION.none then
34+
o:build_hl_table()
35+
end
36+
37+
if opts.renderer.icons.show.git then
38+
o:build_icons_by_status(opts.renderer.icons.glyphs.git)
39+
o:build_icons_by_xy(o.icons_by_status)
40+
41+
for _, icon in pairs(o.icons_by_status) do
42+
self:define_sign(icon)
43+
end
44+
end
45+
46+
return o
47+
end
48+
49+
--- @param glyphs table<string, string> user glyps
50+
function DecoratorGit:build_icons_by_status(glyphs)
51+
self.icons_by_status = {
52+
staged = { str = glyphs.staged, hl = { "NvimTreeGitStagedIcon" }, ord = 1 },
53+
unstaged = { str = glyphs.unstaged, hl = { "NvimTreeGitDirtyIcon" }, ord = 2 },
54+
renamed = { str = glyphs.renamed, hl = { "NvimTreeGitRenamedIcon" }, ord = 3 },
55+
deleted = { str = glyphs.deleted, hl = { "NvimTreeGitDeletedIcon" }, ord = 4 },
56+
unmerged = { str = glyphs.unmerged, hl = { "NvimTreeGitMergeIcon" }, ord = 5 },
57+
untracked = { str = glyphs.untracked, hl = { "NvimTreeGitNewIcon" }, ord = 6 },
58+
ignored = { str = glyphs.ignored, hl = { "NvimTreeGitIgnoredIcon" }, ord = 7 },
2459
}
25-
return {
60+
end
61+
62+
---@param icons HighlightedStringGit[]
63+
function DecoratorGit:build_icons_by_xy(icons)
64+
self.icons_by_xy = {
2665
["M "] = { icons.staged },
2766
[" M"] = { icons.unstaged },
2867
["C "] = { icons.staged },
@@ -58,8 +97,8 @@ local function build_icons_table(i)
5897
}
5998
end
6099

61-
local function build_hl_table()
62-
local file = {
100+
function DecoratorGit:build_hl_table()
101+
self.file_hl = {
63102
["M "] = "NvimTreeGitFileStagedHL",
64103
["C "] = "NvimTreeGitFileStagedHL",
65104
["AA"] = "NvimTreeGitFileStagedHL",
@@ -92,55 +131,17 @@ local function build_hl_table()
92131
[" A"] = "none",
93132
}
94133

95-
local folder = {}
96-
for k, v in pairs(file) do
97-
folder[k] = v:gsub("File", "Folder")
134+
self.folder_hl = {}
135+
for k, v in pairs(self.file_hl) do
136+
self.folder_hl[k] = v:gsub("File", "Folder")
98137
end
99-
100-
return file, folder
101-
end
102-
103-
local function setup_signs(i)
104-
vim.fn.sign_define("NvimTreeGitDirtyIcon", { text = i.unstaged, texthl = "NvimTreeGitDirtyIcon" })
105-
vim.fn.sign_define("NvimTreeGitStagedIcon", { text = i.staged, texthl = "NvimTreeGitStagedIcon" })
106-
vim.fn.sign_define("NvimTreeGitMergeIcon", { text = i.unmerged, texthl = "NvimTreeGitMergeIcon" })
107-
vim.fn.sign_define("NvimTreeGitRenamedIcon", { text = i.renamed, texthl = "NvimTreeGitRenamedIcon" })
108-
vim.fn.sign_define("NvimTreeGitNewIcon", { text = i.untracked, texthl = "NvimTreeGitNewIcon" })
109-
vim.fn.sign_define("NvimTreeGitDeletedIcon", { text = i.deleted, texthl = "NvimTreeGitDeletedIcon" })
110-
vim.fn.sign_define("NvimTreeGitIgnoredIcon", { text = i.ignored, texthl = "NvimTreeGitIgnoredIcon" })
111-
end
112-
113-
--- @param opts table
114-
--- @return DecoratorGit
115-
function DecoratorGit:new(opts)
116-
local o = Decorator.new(self, {
117-
enabled = opts.git.enable,
118-
hl_pos = HL_POSITION[opts.renderer.highlight_git] or HL_POSITION.none,
119-
icon_placement = ICON_PLACEMENT[opts.renderer.icons.git_placement] or ICON_PLACEMENT.none,
120-
})
121-
---@cast o DecoratorGit
122-
123-
if not o.enabled then
124-
return o
125-
end
126-
127-
if o.hl_pos ~= HL_POSITION.none then
128-
o.file_hl, o.folder_hl = build_hl_table()
129-
end
130-
131-
if opts.renderer.icons.show.git then
132-
o.git_icons = build_icons_table(opts.renderer.icons.glyphs.git)
133-
setup_signs(opts.renderer.icons.glyphs.git)
134-
end
135-
136-
return o
137138
end
138139

139140
--- Git icons: git.enable, renderer.icons.show.git and node has status
140141
--- @param node table
141142
--- @return HighlightedString[]|nil modified icon
142143
function DecoratorGit:calculate_icons(node)
143-
if not node or not self.enabled or not self.git_icons then
144+
if not node or not self.enabled or not self.icons_by_xy then
144145
return nil
145146
end
146147

@@ -153,7 +154,7 @@ function DecoratorGit:calculate_icons(node)
153154
local iconss = {}
154155

155156
for _, s in pairs(git_status) do
156-
local icons = self.git_icons[s]
157+
local icons = self.icons_by_xy[s]
157158
if not icons then
158159
if self.hl_pos == HL_POSITION.none then
159160
notify.warn(string.format("Unrecognized git state '%s'", git_status))

0 commit comments

Comments
 (0)