Skip to content

Commit 3376e77

Browse files
committed
feat(#2415): create DecoratorGit
1 parent 232053f commit 3376e77

File tree

3 files changed

+53
-43
lines changed

3 files changed

+53
-43
lines changed

lua/nvim-tree/renderer/builder.lua

+2-3
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
local utils = require "nvim-tree.utils"
22
local core = require "nvim-tree.core"
33

4-
local git = require "nvim-tree.renderer.components.git"
54
local pad = require "nvim-tree.renderer.components.padding"
65
local icons = require "nvim-tree.renderer.components.icons"
76

@@ -194,7 +193,7 @@ end
194193
---@param node table
195194
---@return HighlightedString[]|nil icon
196195
function Builder:_get_git_icons(node)
197-
local git_icons = git.get_icons(node)
196+
local git_icons = self.decorators.git:get_icons(node)
198197
if git_icons and #git_icons > 0 and self.git_placement == "signcolumn" then
199198
table.insert(self.signs, {
200199
sign = git_icons[1].hl[1],
@@ -398,7 +397,7 @@ function Builder:_build_line(node, idx, num_children, unloaded_bufnr)
398397
end
399398

400399
-- extra highighting
401-
self:_append_highlight(node, git.get_highlight, icon.hl, name.hl)
400+
self:_append_dec_highlight(node, self.decorators.git, icon.hl, name.hl)
402401
self:_append_dec_highlight(node, self.decorators.modified, icon.hl, name.hl)
403402
self:_append_dec_highlight(node, self.decorators.bookmarks, icon.hl, name.hl)
404403
self:_append_dec_highlight(node, self.decorators.diagnostics, icon.hl, name.hl)

lua/nvim-tree/renderer/components/git.lua renamed to lua/nvim-tree/renderer/decorator/git.lua

+49-38
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,16 @@ local notify = require "nvim-tree.notify"
22
local explorer_node = require "nvim-tree.explorer.node"
33

44
local HL_POSITION = require("nvim-tree.enum").HL_POSITION
5+
local ICON_PLACEMENT = require("nvim-tree.enum").ICON_PLACEMENT
56

6-
local M = {
7-
-- position for HL
8-
HL_POS = HL_POSITION.none,
9-
}
7+
local Decorator = require "nvim-tree.renderer.decorator"
8+
9+
--- @class DecoratorGit: Decorator
10+
--- @field enabled boolean
11+
--- @field file_hl string[]
12+
--- @field folder_hl string[]
13+
--- @field git_icons table
14+
local DecoratorGit = Decorator:new()
1015

1116
local function build_icons_table(i)
1217
local icons = {
@@ -106,14 +111,37 @@ local function setup_signs(i)
106111
vim.fn.sign_define("NvimTreeGitIgnoredIcon", { text = i.ignored, texthl = "NvimTreeGitIgnoredIcon" })
107112
end
108113

109-
local function warn_status(git_status)
110-
notify.warn(string.format("Unrecognized git state '%s'", git_status))
114+
--- @param opts table
115+
--- @return DecoratorGit
116+
function DecoratorGit:new(opts)
117+
local o = Decorator.new(self, {
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+
o.enabled = opts.git.enable
124+
if not o.enabled then
125+
return o
126+
end
127+
128+
if o.hl_pos ~= HL_POSITION.none then
129+
o.file_hl, o.folder_hl = build_hl_table()
130+
end
131+
132+
if opts.renderer.icons.show.git then
133+
o.git_icons = build_icons_table(opts.renderer.icons.glyphs.git)
134+
setup_signs(opts.renderer.icons.glyphs.git)
135+
end
136+
137+
return o
111138
end
112139

113-
---@param node table
114-
---@return HighlightedString[]|nil
115-
function M.get_icons(node)
116-
if not M.config.icons.show.git then
140+
--- Git icons: git.enable, renderer.icons.show.git and node has status
141+
--- @param node table
142+
--- @return HighlightedString[]|nil modified icon
143+
function DecoratorGit:get_icons(node)
144+
if not node or not self.enabled or not self.git_icons then
117145
return nil
118146
end
119147

@@ -126,10 +154,10 @@ function M.get_icons(node)
126154
local iconss = {}
127155

128156
for _, s in pairs(git_status) do
129-
local icons = M.git_icons[s]
157+
local icons = self.git_icons[s]
130158
if not icons then
131-
if not M.config.highlight_git then
132-
warn_status(s)
159+
if self.hl_pos == HL_POSITION.none then
160+
notify.warn(string.format("Unrecognized git state '%s'", git_status))
133161
end
134162
return nil
135163
end
@@ -156,39 +184,22 @@ function M.get_icons(node)
156184
return iconss
157185
end
158186

159-
---Git highlight group and position when highlight_git
160-
---@param node table
161-
---@return HL_POSITION position none when no status
162-
---@return string|nil group only when status
163-
function M.get_highlight(node)
164-
if not node or M.HL_POS == HL_POSITION.none then
165-
return HL_POSITION.none, nil
187+
--- Git highlight: git.enable, renderer.highlight_git and node has status
188+
function DecoratorGit:get_highlight(node)
189+
if not node or not self.enabled or self.hl_pos == HL_POSITION.none then
190+
return nil
166191
end
167192

168193
local git_status = explorer_node.get_git_status(node)
169194
if not git_status then
170-
return HL_POSITION.none, nil
195+
return nil
171196
end
172197

173198
if node.nodes then
174-
return M.HL_POS, M.folder_hl[git_status[1]]
199+
return self.folder_hl[git_status[1]]
175200
else
176-
return M.HL_POS, M.file_hl[git_status[1]]
177-
end
178-
end
179-
180-
function M.setup(opts)
181-
M.config = opts.renderer
182-
183-
M.git_icons = build_icons_table(opts.renderer.icons.glyphs.git)
184-
185-
M.file_hl, M.folder_hl = build_hl_table()
186-
187-
setup_signs(opts.renderer.icons.glyphs.git)
188-
189-
if opts.git.enable and opts.renderer.highlight_git then
190-
M.HL_POS = HL_POSITION[opts.renderer.highlight_git]
201+
return self.file_hl[git_status[1]]
191202
end
192203
end
193204

194-
return M
205+
return DecoratorGit

lua/nvim-tree/renderer/init.lua

+2-2
Original file line numberDiff line numberDiff line change
@@ -6,12 +6,12 @@ local events = require "nvim-tree.events"
66
local _padding = require "nvim-tree.renderer.components.padding"
77
local icon_component = require "nvim-tree.renderer.components.icons"
88
local full_name = require "nvim-tree.renderer.components.full-name"
9-
local git = require "nvim-tree.renderer.components.git"
109
local Builder = require "nvim-tree.renderer.builder"
1110
local live_filter = require "nvim-tree.live-filter"
1211

1312
local DecoratorBookmarks = require "nvim-tree.renderer.decorator.bookmarks"
1413
local DecoratorDiagnostics = require "nvim-tree.renderer.decorator.diagnostics"
14+
local DecoratorGit = require "nvim-tree.renderer.decorator.git"
1515
local DecoratorModified = require "nvim-tree.renderer.decorator.modified"
1616

1717
local M = {
@@ -102,11 +102,11 @@ function M.setup(opts)
102102

103103
_padding.setup(opts)
104104
full_name.setup(opts)
105-
git.setup(opts)
106105
icon_component.setup(opts)
107106

108107
-- TODO change to array: precedence should follow order
109108
M.decorators = {
109+
git = DecoratorGit:new(opts),
110110
bookmarks = DecoratorBookmarks:new(opts),
111111
diagnostics = DecoratorDiagnostics:new(opts),
112112
modified = DecoratorModified:new(opts),

0 commit comments

Comments
 (0)