@@ -2,11 +2,16 @@ local notify = require "nvim-tree.notify"
2
2
local explorer_node = require " nvim-tree.explorer.node"
3
3
4
4
local HL_POSITION = require (" nvim-tree.enum" ).HL_POSITION
5
+ local ICON_PLACEMENT = require (" nvim-tree.enum" ).ICON_PLACEMENT
5
6
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 ()
10
15
11
16
local function build_icons_table (i )
12
17
local icons = {
@@ -106,14 +111,37 @@ local function setup_signs(i)
106
111
vim .fn .sign_define (" NvimTreeGitIgnoredIcon" , { text = i .ignored , texthl = " NvimTreeGitIgnoredIcon" })
107
112
end
108
113
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
111
138
end
112
139
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
117
145
return nil
118
146
end
119
147
@@ -126,10 +154,10 @@ function M.get_icons(node)
126
154
local iconss = {}
127
155
128
156
for _ , s in pairs (git_status ) do
129
- local icons = M .git_icons [s ]
157
+ local icons = self .git_icons [s ]
130
158
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 ) )
133
161
end
134
162
return nil
135
163
end
@@ -156,39 +184,22 @@ function M.get_icons(node)
156
184
return iconss
157
185
end
158
186
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
166
191
end
167
192
168
193
local git_status = explorer_node .get_git_status (node )
169
194
if not git_status then
170
- return HL_POSITION . none , nil
195
+ return nil
171
196
end
172
197
173
198
if node .nodes then
174
- return M . HL_POS , M .folder_hl [git_status [1 ]]
199
+ return self .folder_hl [git_status [1 ]]
175
200
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 ]]
191
202
end
192
203
end
193
204
194
- return M
205
+ return DecoratorGit
0 commit comments