Skip to content

Commit f8312cd

Browse files
authored
feat(renderer): add ability to set git icons in signcolumn (#1242)
1 parent 4601444 commit f8312cd

File tree

5 files changed

+42
-11
lines changed

5 files changed

+42
-11
lines changed

doc/nvim-tree-lua.txt

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -432,7 +432,10 @@ UI rendering setup
432432
*nvim-tree.renderer.icons.git_placement*
433433
Place where the git icons will be rendered.
434434
After or before the `filename` (after the file/folders icons).
435-
Type: `after` or `before`, Default: `before`
435+
When placed in the signcolumn, the value of signcolumn should be `yes`
436+
for the nvim-tree window.
437+
Note that the diagnostic signs will take precedence over the git signs.
438+
Type: `after`, `before` or `signcolumn`, Default: `before`
436439

437440
*nvim-tree.filters*
438441
Filtering options.

lua/nvim-tree/diagnostics.lua

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ local function add_sign(linenr, severity)
3232
return
3333
end
3434
local sign_name = sign_names[severity][1]
35-
vim.fn.sign_place(1, GROUP, sign_name, buf, { lnum = linenr })
35+
vim.fn.sign_place(0, GROUP, sign_name, buf, { lnum = linenr, priority = 2 })
3636
end
3737

3838
local function from_nvim_lsp()

lua/nvim-tree/renderer/builder.lua

Lines changed: 14 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ function Builder.new(root_cwd)
1414
highlights = {},
1515
lines = {},
1616
markers = {},
17+
signs = {},
1718
root_cwd = root_cwd,
1819
}, Builder)
1920
end
@@ -61,9 +62,11 @@ function Builder:configure_git_icons_padding(padding)
6162
end
6263

6364
function Builder:configure_git_icons_placement(where)
64-
where = where or "before"
65-
self.is_git_before = where == "before"
66-
self.is_git_after = not self.is_git_before
65+
if where == "signcolumn" then
66+
vim.fn.sign_unplace(git.SIGN_GROUP)
67+
self.is_git_sign = true
68+
end
69+
self.is_git_after = where == "after" and not self.is_git_sign
6770
return self
6871
end
6972

@@ -109,7 +112,7 @@ function Builder:_build_folder(node, padding, git_hl, git_icons_tbl)
109112

110113
local foldername = name .. self.trailing_slash
111114
local git_icons = self:_unwrap_git_data(git_icons_tbl, offset + #icon + (self.is_git_after and #foldername + 1 or 0))
112-
local fname_starts_at = offset + #icon + (self.is_git_before and #git_icons or 0)
115+
local fname_starts_at = offset + #icon + (self.is_git_after and 0 or #git_icons)
113116
local line = self:_format_line(padding .. icon, foldername, git_icons)
114117
self:_insert_line(line)
115118

@@ -226,6 +229,12 @@ function Builder:_build_line(tree, node, idx)
226229
local git_highlight = git.get_highlight(node)
227230
local git_icons_tbl = git.get_icons(node)
228231

232+
if self.is_git_sign and git_icons_tbl and #git_icons_tbl > 0 then
233+
local git_info = git_icons_tbl[1]
234+
table.insert(self.signs, { sign = git_info.hl, lnum = self.index + 1 })
235+
git_icons_tbl = {}
236+
end
237+
229238
local is_folder = node.nodes ~= nil
230239
local is_symlink = node.link_to ~= nil
231240

@@ -270,7 +279,7 @@ function Builder:build_header(show_header)
270279
end
271280

272281
function Builder:unwrap()
273-
return self.lines, self.highlights
282+
return self.lines, self.highlights, self.signs
274283
end
275284

276285
return Builder

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

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,9 @@
11
local _icons = require "nvim-tree.renderer.icon-config"
22
local utils = require "nvim-tree.utils"
33

4-
local M = {}
4+
local M = {
5+
SIGN_GROUP = "NvimTreeGitSigns",
6+
}
57

68
local function build_icons_table()
79
local i = M.icon_state.icons.git_icons
@@ -124,6 +126,17 @@ local git_hl = {
124126
[" A"] = "none",
125127
}
126128

129+
function M.setup_signs()
130+
local i = M.icon_state.icons.git_icons
131+
vim.fn.sign_define("NvimTreeGitDirty", { text = i.unstaged, texthl = "NvimTreeGitDirty" })
132+
vim.fn.sign_define("NvimTreeGitStaged", { text = i.staged, texthl = "NvimTreeGitStaged" })
133+
vim.fn.sign_define("NvimTreeGitMerge", { text = i.unmerged, texthl = "NvimTreeGitMerge" })
134+
vim.fn.sign_define("NvimTreeGitRenamed", { text = i.renamed, texthl = "NvimTreeGitRenamed" })
135+
vim.fn.sign_define("NvimTreeGitNew", { text = i.untracked, texthl = "NvimTreeGitNew" })
136+
vim.fn.sign_define("NvimTreeGitDeleted", { text = i.deleted, texthl = "NvimTreeGitDeleted" })
137+
vim.fn.sign_define("NvimTreeGitIgnored", { text = i.ignored, texthl = "NvimTreeGitIgnored" })
138+
end
139+
127140
local function get_highlight_(node)
128141
local git_status = node.git_status
129142
if not git_status then

lua/nvim-tree/renderer/init.lua

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -17,11 +17,14 @@ local M = {
1717

1818
local namespace_id = api.nvim_create_namespace "NvimTreeHighlights"
1919

20-
local function _draw(bufnr, lines, hl)
20+
local function _draw(bufnr, lines, hl, signs)
2121
api.nvim_buf_set_option(bufnr, "modifiable", true)
2222
api.nvim_buf_set_lines(bufnr, 0, -1, false, lines)
2323
M.render_hl(bufnr, hl)
2424
api.nvim_buf_set_option(bufnr, "modifiable", false)
25+
for _, sign in pairs(signs) do
26+
vim.fn.sign_place(0, git.SIGN_GROUP, sign.sign, bufnr, { lnum = sign.lnum, priority = 1 })
27+
end
2528
end
2629

2730
function M.render_hl(bufnr, hl)
@@ -71,10 +74,11 @@ function M.draw()
7174
git.reload()
7275

7376
local lines, hl
77+
local signs = {}
7478
if view.is_help_ui() then
7579
lines, hl = help.compute_lines()
7680
else
77-
lines, hl = Builder.new(core.get_cwd())
81+
lines, hl, signs = Builder.new(core.get_cwd())
7882
:configure_initial_depth(should_show_arrows())
7983
:configure_root_modifier(vim.g.nvim_tree_root_folder_modifier)
8084
:configure_trailing_slash(vim.g.nvim_tree_add_trailing == 1)
@@ -88,7 +92,8 @@ function M.draw()
8892
:unwrap()
8993
end
9094

91-
_draw(bufnr, lines, hl)
95+
_draw(bufnr, lines, hl, signs)
96+
9297
M.last_highlights = hl
9398

9499
if cursor and #lines >= cursor[1] then
@@ -111,6 +116,7 @@ function M.setup(opts)
111116
}
112117

113118
_padding.setup(opts)
119+
git.setup_signs()
114120
end
115121

116122
return M

0 commit comments

Comments
 (0)