feat(renderer): add ability to set git icons in signcolumn (#1242)
This commit is contained in:
parent
46014449b6
commit
f8312cd06f
@ -432,7 +432,10 @@ UI rendering setup
|
||||
*nvim-tree.renderer.icons.git_placement*
|
||||
Place where the git icons will be rendered.
|
||||
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*
|
||||
Filtering options.
|
||||
|
||||
@ -32,7 +32,7 @@ local function add_sign(linenr, severity)
|
||||
return
|
||||
end
|
||||
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
|
||||
|
||||
local function from_nvim_lsp()
|
||||
|
||||
@ -14,6 +14,7 @@ function Builder.new(root_cwd)
|
||||
highlights = {},
|
||||
lines = {},
|
||||
markers = {},
|
||||
signs = {},
|
||||
root_cwd = root_cwd,
|
||||
}, Builder)
|
||||
end
|
||||
@ -61,9 +62,11 @@ function Builder:configure_git_icons_padding(padding)
|
||||
end
|
||||
|
||||
function Builder:configure_git_icons_placement(where)
|
||||
where = where or "before"
|
||||
self.is_git_before = where == "before"
|
||||
self.is_git_after = not self.is_git_before
|
||||
if where == "signcolumn" then
|
||||
vim.fn.sign_unplace(git.SIGN_GROUP)
|
||||
self.is_git_sign = true
|
||||
end
|
||||
self.is_git_after = where == "after" and not self.is_git_sign
|
||||
return self
|
||||
end
|
||||
|
||||
@ -109,7 +112,7 @@ function Builder:_build_folder(node, padding, git_hl, git_icons_tbl)
|
||||
|
||||
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 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)
|
||||
self:_insert_line(line)
|
||||
|
||||
@ -226,6 +229,12 @@ function Builder:_build_line(tree, node, idx)
|
||||
local git_highlight = git.get_highlight(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_symlink = node.link_to ~= nil
|
||||
|
||||
@ -270,7 +279,7 @@ function Builder:build_header(show_header)
|
||||
end
|
||||
|
||||
function Builder:unwrap()
|
||||
return self.lines, self.highlights
|
||||
return self.lines, self.highlights, self.signs
|
||||
end
|
||||
|
||||
return Builder
|
||||
|
||||
@ -1,7 +1,9 @@
|
||||
local _icons = require "nvim-tree.renderer.icon-config"
|
||||
local utils = require "nvim-tree.utils"
|
||||
|
||||
local M = {}
|
||||
local M = {
|
||||
SIGN_GROUP = "NvimTreeGitSigns",
|
||||
}
|
||||
|
||||
local function build_icons_table()
|
||||
local i = M.icon_state.icons.git_icons
|
||||
@ -124,6 +126,17 @@ local git_hl = {
|
||||
[" 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 git_status = node.git_status
|
||||
if not git_status then
|
||||
|
||||
@ -17,11 +17,14 @@ local M = {
|
||||
|
||||
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_lines(bufnr, 0, -1, false, lines)
|
||||
M.render_hl(bufnr, hl)
|
||||
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
|
||||
|
||||
function M.render_hl(bufnr, hl)
|
||||
@ -71,10 +74,11 @@ function M.draw()
|
||||
git.reload()
|
||||
|
||||
local lines, hl
|
||||
local signs = {}
|
||||
if view.is_help_ui() then
|
||||
lines, hl = help.compute_lines()
|
||||
else
|
||||
lines, hl = Builder.new(core.get_cwd())
|
||||
lines, hl, signs = Builder.new(core.get_cwd())
|
||||
:configure_initial_depth(should_show_arrows())
|
||||
:configure_root_modifier(vim.g.nvim_tree_root_folder_modifier)
|
||||
:configure_trailing_slash(vim.g.nvim_tree_add_trailing == 1)
|
||||
@ -88,7 +92,8 @@ function M.draw()
|
||||
:unwrap()
|
||||
end
|
||||
|
||||
_draw(bufnr, lines, hl)
|
||||
_draw(bufnr, lines, hl, signs)
|
||||
|
||||
M.last_highlights = hl
|
||||
|
||||
if cursor and #lines >= cursor[1] then
|
||||
@ -111,6 +116,7 @@ function M.setup(opts)
|
||||
}
|
||||
|
||||
_padding.setup(opts)
|
||||
git.setup_signs()
|
||||
end
|
||||
|
||||
return M
|
||||
|
||||
Loading…
Reference in New Issue
Block a user