Skip to content

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

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 4 commits into from
May 14, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 4 additions & 1 deletion doc/nvim-tree-lua.txt
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
2 changes: 1 addition & 1 deletion lua/nvim-tree/diagnostics.lua
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down
19 changes: 14 additions & 5 deletions lua/nvim-tree/renderer/builder.lua
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ function Builder.new(root_cwd)
highlights = {},
lines = {},
markers = {},
signs = {},
root_cwd = root_cwd,
}, Builder)
end
Expand Down Expand Up @@ -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

Expand Down Expand Up @@ -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)

Expand Down Expand Up @@ -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

Expand Down Expand Up @@ -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
15 changes: 14 additions & 1 deletion lua/nvim-tree/renderer/components/git.lua
Original file line number Diff line number Diff line change
@@ -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
Expand Down Expand Up @@ -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
Expand Down
12 changes: 9 additions & 3 deletions lua/nvim-tree/renderer/init.lua
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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)
Expand All @@ -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
Expand All @@ -111,6 +116,7 @@ function M.setup(opts)
}

_padding.setup(opts)
git.setup_signs()
end

return M