Skip to content

Commit 144bce7

Browse files
committed
refacto: move renderer git into file
also view.is_root_folder_modifier_visible don't take the tree as parameter
1 parent 60a9c86 commit 144bce7

File tree

6 files changed

+200
-195
lines changed

6 files changed

+200
-195
lines changed

lua/nvim-tree/actions/find-file.lua

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ function M.fn(fname)
1212
end
1313
running[fname] = true
1414

15-
local i = view.is_root_folder_visible(TreeExplorer) and 1 or 0
15+
local i = view.is_root_folder_visible() and 1 or 0
1616
local tree_altered = false
1717

1818
local function iterate_nodes(nodes)

lua/nvim-tree/actions/movements.lua

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,7 @@ function M.parent_node(should_close)
5656
parent.open = false
5757
altered_tree = true
5858
end
59-
if not view.is_root_folder_visible(TreeExplorer) then
59+
if not view.is_root_folder_visible() then
6060
line = line - 1
6161
end
6262
view.set_cursor { line, 0 }
@@ -109,7 +109,7 @@ function M.sibling(direction)
109109
local target_node = parent.nodes[index]
110110

111111
line, _ = get_line_from_node(target_node)(TreeExplorer.nodes, true)
112-
if not view.is_root_folder_visible(TreeExplorer) then
112+
if not view.is_root_folder_visible() then
113113
line = line - 1
114114
end
115115
view.set_cursor { line, 0 }

lua/nvim-tree/actions/search-node.lua

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -77,7 +77,7 @@ function M.fn()
7777
end
7878

7979
if found_something and view.is_visible() then
80-
if view.is_root_folder_visible(TreeExplorer) then
80+
if view.is_root_folder_visible() then
8181
index = index + 1
8282
end
8383

lua/nvim-tree/renderer/git.lua

Lines changed: 169 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,169 @@
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

Comments
 (0)