feat(renderer): add ability to set git icons in signcolumn (#1242)

This commit is contained in:
Kiyan 2022-05-14 09:54:27 +02:00 committed by GitHub
parent 46014449b6
commit f8312cd06f
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
5 changed files with 42 additions and 11 deletions

View File

@ -432,7 +432,10 @@ UI rendering setup
*nvim-tree.renderer.icons.git_placement* *nvim-tree.renderer.icons.git_placement*
Place where the git icons will be rendered. Place where the git icons will be rendered.
After or before the `filename` (after the file/folders icons). After or before the `filename` (after the file/folders icons).
Type: `after` or `before`, Default: `before` When placed in the signcolumn, the value of signcolumn should be `yes`
for the nvim-tree window.
Note that the diagnostic signs will take precedence over the git signs.
Type: `after`, `before` or `signcolumn`, Default: `before`
*nvim-tree.filters* *nvim-tree.filters*
Filtering options. Filtering options.

View File

@ -32,7 +32,7 @@ local function add_sign(linenr, severity)
return return
end end
local sign_name = sign_names[severity][1] local sign_name = sign_names[severity][1]
vim.fn.sign_place(1, GROUP, sign_name, buf, { lnum = linenr }) vim.fn.sign_place(0, GROUP, sign_name, buf, { lnum = linenr, priority = 2 })
end end
local function from_nvim_lsp() local function from_nvim_lsp()

View File

@ -14,6 +14,7 @@ function Builder.new(root_cwd)
highlights = {}, highlights = {},
lines = {}, lines = {},
markers = {}, markers = {},
signs = {},
root_cwd = root_cwd, root_cwd = root_cwd,
}, Builder) }, Builder)
end end
@ -61,9 +62,11 @@ function Builder:configure_git_icons_padding(padding)
end end
function Builder:configure_git_icons_placement(where) function Builder:configure_git_icons_placement(where)
where = where or "before" if where == "signcolumn" then
self.is_git_before = where == "before" vim.fn.sign_unplace(git.SIGN_GROUP)
self.is_git_after = not self.is_git_before self.is_git_sign = true
end
self.is_git_after = where == "after" and not self.is_git_sign
return self return self
end end
@ -109,7 +112,7 @@ function Builder:_build_folder(node, padding, git_hl, git_icons_tbl)
local foldername = name .. self.trailing_slash local foldername = name .. self.trailing_slash
local git_icons = self:_unwrap_git_data(git_icons_tbl, offset + #icon + (self.is_git_after and #foldername + 1 or 0)) local git_icons = self:_unwrap_git_data(git_icons_tbl, offset + #icon + (self.is_git_after and #foldername + 1 or 0))
local fname_starts_at = offset + #icon + (self.is_git_before and #git_icons or 0) local fname_starts_at = offset + #icon + (self.is_git_after and 0 or #git_icons)
local line = self:_format_line(padding .. icon, foldername, git_icons) local line = self:_format_line(padding .. icon, foldername, git_icons)
self:_insert_line(line) self:_insert_line(line)
@ -226,6 +229,12 @@ function Builder:_build_line(tree, node, idx)
local git_highlight = git.get_highlight(node) local git_highlight = git.get_highlight(node)
local git_icons_tbl = git.get_icons(node) local git_icons_tbl = git.get_icons(node)
if self.is_git_sign and git_icons_tbl and #git_icons_tbl > 0 then
local git_info = git_icons_tbl[1]
table.insert(self.signs, { sign = git_info.hl, lnum = self.index + 1 })
git_icons_tbl = {}
end
local is_folder = node.nodes ~= nil local is_folder = node.nodes ~= nil
local is_symlink = node.link_to ~= nil local is_symlink = node.link_to ~= nil
@ -270,7 +279,7 @@ function Builder:build_header(show_header)
end end
function Builder:unwrap() function Builder:unwrap()
return self.lines, self.highlights return self.lines, self.highlights, self.signs
end end
return Builder return Builder

View File

@ -1,7 +1,9 @@
local _icons = require "nvim-tree.renderer.icon-config" local _icons = require "nvim-tree.renderer.icon-config"
local utils = require "nvim-tree.utils" local utils = require "nvim-tree.utils"
local M = {} local M = {
SIGN_GROUP = "NvimTreeGitSigns",
}
local function build_icons_table() local function build_icons_table()
local i = M.icon_state.icons.git_icons local i = M.icon_state.icons.git_icons
@ -124,6 +126,17 @@ local git_hl = {
[" A"] = "none", [" A"] = "none",
} }
function M.setup_signs()
local i = M.icon_state.icons.git_icons
vim.fn.sign_define("NvimTreeGitDirty", { text = i.unstaged, texthl = "NvimTreeGitDirty" })
vim.fn.sign_define("NvimTreeGitStaged", { text = i.staged, texthl = "NvimTreeGitStaged" })
vim.fn.sign_define("NvimTreeGitMerge", { text = i.unmerged, texthl = "NvimTreeGitMerge" })
vim.fn.sign_define("NvimTreeGitRenamed", { text = i.renamed, texthl = "NvimTreeGitRenamed" })
vim.fn.sign_define("NvimTreeGitNew", { text = i.untracked, texthl = "NvimTreeGitNew" })
vim.fn.sign_define("NvimTreeGitDeleted", { text = i.deleted, texthl = "NvimTreeGitDeleted" })
vim.fn.sign_define("NvimTreeGitIgnored", { text = i.ignored, texthl = "NvimTreeGitIgnored" })
end
local function get_highlight_(node) local function get_highlight_(node)
local git_status = node.git_status local git_status = node.git_status
if not git_status then if not git_status then

View File

@ -17,11 +17,14 @@ local M = {
local namespace_id = api.nvim_create_namespace "NvimTreeHighlights" local namespace_id = api.nvim_create_namespace "NvimTreeHighlights"
local function _draw(bufnr, lines, hl) local function _draw(bufnr, lines, hl, signs)
api.nvim_buf_set_option(bufnr, "modifiable", true) api.nvim_buf_set_option(bufnr, "modifiable", true)
api.nvim_buf_set_lines(bufnr, 0, -1, false, lines) api.nvim_buf_set_lines(bufnr, 0, -1, false, lines)
M.render_hl(bufnr, hl) M.render_hl(bufnr, hl)
api.nvim_buf_set_option(bufnr, "modifiable", false) api.nvim_buf_set_option(bufnr, "modifiable", false)
for _, sign in pairs(signs) do
vim.fn.sign_place(0, git.SIGN_GROUP, sign.sign, bufnr, { lnum = sign.lnum, priority = 1 })
end
end end
function M.render_hl(bufnr, hl) function M.render_hl(bufnr, hl)
@ -71,10 +74,11 @@ function M.draw()
git.reload() git.reload()
local lines, hl local lines, hl
local signs = {}
if view.is_help_ui() then if view.is_help_ui() then
lines, hl = help.compute_lines() lines, hl = help.compute_lines()
else else
lines, hl = Builder.new(core.get_cwd()) lines, hl, signs = Builder.new(core.get_cwd())
:configure_initial_depth(should_show_arrows()) :configure_initial_depth(should_show_arrows())
:configure_root_modifier(vim.g.nvim_tree_root_folder_modifier) :configure_root_modifier(vim.g.nvim_tree_root_folder_modifier)
:configure_trailing_slash(vim.g.nvim_tree_add_trailing == 1) :configure_trailing_slash(vim.g.nvim_tree_add_trailing == 1)
@ -88,7 +92,8 @@ function M.draw()
:unwrap() :unwrap()
end end
_draw(bufnr, lines, hl) _draw(bufnr, lines, hl, signs)
M.last_highlights = hl M.last_highlights = hl
if cursor and #lines >= cursor[1] then if cursor and #lines >= cursor[1] then
@ -111,6 +116,7 @@ function M.setup(opts)
} }
_padding.setup(opts) _padding.setup(opts)
git.setup_signs()
end end
return M return M