refacto: move renderer git into file

also view.is_root_folder_modifier_visible don't take the tree as
parameter
This commit is contained in:
kiyan
2022-03-09 21:32:15 +01:00
parent 60a9c86c53
commit 144bce74a5
6 changed files with 200 additions and 195 deletions

View File

@@ -12,7 +12,7 @@ function M.fn(fname)
end end
running[fname] = true running[fname] = true
local i = view.is_root_folder_visible(TreeExplorer) and 1 or 0 local i = view.is_root_folder_visible() and 1 or 0
local tree_altered = false local tree_altered = false
local function iterate_nodes(nodes) local function iterate_nodes(nodes)

View File

@@ -56,7 +56,7 @@ function M.parent_node(should_close)
parent.open = false parent.open = false
altered_tree = true altered_tree = true
end end
if not view.is_root_folder_visible(TreeExplorer) then if not view.is_root_folder_visible() then
line = line - 1 line = line - 1
end end
view.set_cursor { line, 0 } view.set_cursor { line, 0 }
@@ -109,7 +109,7 @@ function M.sibling(direction)
local target_node = parent.nodes[index] local target_node = parent.nodes[index]
line, _ = get_line_from_node(target_node)(TreeExplorer.nodes, true) line, _ = get_line_from_node(target_node)(TreeExplorer.nodes, true)
if not view.is_root_folder_visible(TreeExplorer) then if not view.is_root_folder_visible() then
line = line - 1 line = line - 1
end end
view.set_cursor { line, 0 } view.set_cursor { line, 0 }

View File

@@ -77,7 +77,7 @@ function M.fn()
end end
if found_something and view.is_visible() then if found_something and view.is_visible() then
if view.is_root_folder_visible(TreeExplorer) then if view.is_root_folder_visible() then
index = index + 1 index = index + 1
end end

View File

@@ -0,0 +1,169 @@
local _icons = require "nvim-tree.renderer.icons"
local utils = require "nvim-tree.utils"
local M = {}
local function build_icons_table()
return {
["M "] = { { icon = M.icon_state.icons.git_icons.staged, hl = "NvimTreeGitStaged" } },
[" M"] = { { icon = M.icon_state.icons.git_icons.unstaged, hl = "NvimTreeGitDirty" } },
["C "] = { { icon = M.icon_state.icons.git_icons.staged, hl = "NvimTreeGitStaged" } },
[" C"] = { { icon = M.icon_state.icons.git_icons.unstaged, hl = "NvimTreeGitDirty" } },
["CM"] = { { icon = M.icon_state.icons.git_icons.unstaged, hl = "NvimTreeGitDirty" } },
[" T"] = { { icon = M.icon_state.icons.git_icons.unstaged, hl = "NvimTreeGitDirty" } },
["MM"] = {
{ icon = M.icon_state.icons.git_icons.staged, hl = "NvimTreeGitStaged" },
{ icon = M.icon_state.icons.git_icons.unstaged, hl = "NvimTreeGitDirty" },
},
["MD"] = {
{ icon = M.icon_state.icons.git_icons.staged, hl = "NvimTreeGitStaged" },
},
["A "] = {
{ icon = M.icon_state.icons.git_icons.staged, hl = "NvimTreeGitStaged" },
},
["AD"] = {
{ icon = M.icon_state.icons.git_icons.staged, hl = "NvimTreeGitStaged" },
},
[" A"] = {
{ icon = M.icon_state.icons.git_icons.untracked, hl = "NvimTreeGitNew" },
},
-- not sure about this one
["AA"] = {
{ icon = M.icon_state.icons.git_icons.unmerged, hl = "NvimTreeGitMerge" },
{ icon = M.icon_state.icons.git_icons.untracked, hl = "NvimTreeGitNew" },
},
["AU"] = {
{ icon = M.icon_state.icons.git_icons.unmerged, hl = "NvimTreeGitMerge" },
{ icon = M.icon_state.icons.git_icons.untracked, hl = "NvimTreeGitNew" },
},
["AM"] = {
{ icon = M.icon_state.icons.git_icons.staged, hl = "NvimTreeGitStaged" },
{ icon = M.icon_state.icons.git_icons.unstaged, hl = "NvimTreeGitDirty" },
},
["??"] = { { icon = M.icon_state.icons.git_icons.untracked, hl = "NvimTreeGitNew" } },
["R "] = { { icon = M.icon_state.icons.git_icons.renamed, hl = "NvimTreeGitRenamed" } },
[" R"] = { { icon = M.icon_state.icons.git_icons.renamed, hl = "NvimTreeGitRenamed" } },
["RM"] = {
{ icon = M.icon_state.icons.git_icons.unstaged, hl = "NvimTreeGitDirty" },
{ icon = M.icon_state.icons.git_icons.renamed, hl = "NvimTreeGitRenamed" },
},
["UU"] = { { icon = M.icon_state.icons.git_icons.unmerged, hl = "NvimTreeGitMerge" } },
["UD"] = { { icon = M.icon_state.icons.git_icons.unmerged, hl = "NvimTreeGitMerge" } },
["UA"] = { { icon = M.icon_state.icons.git_icons.unmerged, hl = "NvimTreeGitMerge" } },
[" D"] = { { icon = M.icon_state.icons.git_icons.deleted, hl = "NvimTreeGitDeleted" } },
["D "] = { { icon = M.icon_state.icons.git_icons.deleted, hl = "NvimTreeGitDeleted" } },
["RD"] = { { icon = M.icon_state.icons.git_icons.deleted, hl = "NvimTreeGitDeleted" } },
["DD"] = { { icon = M.icon_state.icons.git_icons.deleted, hl = "NvimTreeGitDeleted" } },
["DU"] = {
{ icon = M.icon_state.icons.git_icons.deleted, hl = "NvimTreeGitDeleted" },
{ icon = M.icon_state.icons.git_icons.unmerged, hl = "NvimTreeGitMerge" },
},
["!!"] = { { icon = M.icon_state.icons.git_icons.ignored, hl = "NvimTreeGitIgnored" } },
dirty = { { icon = M.icon_state.icons.git_icons.unstaged, hl = "NvimTreeGitDirty" } },
}
end
local function empty() return "" end
local function nil_() end
local function warn_status(git_status)
utils.warn(
'Unrecognized git state "'
.. git_status
.. '". Please open up an issue on https://github.com/kyazdani42/nvim-tree.lua/issues with this message.'
)
end
local function get_icons_(node, line, depth, icon_len, hl)
local git_status = node.git_status
if not git_status then
return ""
end
local icon = ""
local icons = M.git_icons[git_status]
if not icons then
if vim.g.nvim_tree_git_hl ~= 1 then
warn_status(git_status)
end
return ""
end
for _, v in ipairs(icons) do
if #v.icon > 0 then
table.insert(hl, { v.hl, line, depth + icon_len + #icon, depth + icon_len + #icon + #v.icon })
icon = icon .. v.icon .. M.icon_padding
end
end
return icon
end
local git_hl = {
["M "] = "NvimTreeFileStaged",
["C "] = "NvimTreeFileStaged",
["AA"] = "NvimTreeFileStaged",
["AD"] = "NvimTreeFileStaged",
["MD"] = "NvimTreeFileStaged",
[" M"] = "NvimTreeFileDirty",
["CM"] = "NvimTreeFileDirty",
[" C"] = "NvimTreeFileDirty",
[" T"] = "NvimTreeFileDirty",
["MM"] = "NvimTreeFileDirty",
["AM"] = "NvimTreeFileDirty",
dirty = "NvimTreeFileDirty",
["A "] = "NvimTreeFileNew",
["??"] = "NvimTreeFileNew",
["AU"] = "NvimTreeFileMerge",
["UU"] = "NvimTreeFileMerge",
["UD"] = "NvimTreeFileMerge",
["DU"] = "NvimTreeFileMerge",
["UA"] = "NvimTreeFileMerge",
[" D"] = "NvimTreeFileDeleted",
["DD"] = "NvimTreeFileDeleted",
["RD"] = "NvimTreeFileDeleted",
["D "] = "NvimTreeFileDeleted",
["R "] = "NvimTreeFileRenamed",
["RM"] = "NvimTreeFileRenamed",
[" R"] = "NvimTreeFileRenamed",
["!!"] = "NvimTreeGitIgnored",
[" A"] = "none",
}
local function get_highlight_(node)
local git_status = node.git_status
if not git_status then
return
end
return git_hl[git_status]
end
function M.get_icons()
return empty()
end
function M.get_highlight()
return nil_()
end
M.icon_padding = vim.g.nvim_tree_icon_padding or " "
M.icon_state = _icons.get_config()
M.git_icons = build_icons_table()
function M.reload()
M.icon_state = _icons.get_config()
M.icon_padding = vim.g.nvim_tree_icon_padding or " "
M.git_icons = build_icons_table()
if M.icon_state.show_git_icon then
M.get_icons = get_icons_
else
M.get_icons = empty
end
if vim.g.nvim_tree_git_hl == 1 then
M.get_highlight = get_highlight_
else
M.get_highlight = nil_
end
end
return M

View File

@@ -3,6 +3,7 @@ local view = require "nvim-tree.view"
local _padding = require "nvim-tree.renderer.padding" local _padding = require "nvim-tree.renderer.padding"
local _help = require "nvim-tree.renderer.help" local _help = require "nvim-tree.renderer.help"
local _icons = require "nvim-tree.renderer.icons" local _icons = require "nvim-tree.renderer.icons"
local git = require "nvim-tree.renderer.git"
local api = vim.api local api = vim.api
@@ -104,179 +105,6 @@ if icon_state.show_file_icon then
end end
end end
local get_git_icons = function()
return ""
end
local get_git_hl = function() end
if vim.g.nvim_tree_git_hl == 1 then
local git_hl = {
["M "] = { { hl = "NvimTreeFileStaged" } },
[" M"] = { { hl = "NvimTreeFileDirty" } },
["CM"] = { { hl = "NvimTreeFileDirty" } },
["C "] = { { hl = "NvimTreeFileStaged" } },
[" C"] = { { hl = "NvimTreeFileDirty" } },
[" T"] = { { hl = "NvimTreeFileDirty" } },
["MM"] = {
{ hl = "NvimTreeFileStaged" },
{ hl = "NvimTreeFileDirty" },
},
["A "] = {
{ hl = "NvimTreeFileStaged" },
{ hl = "NvimTreeFileNew" },
},
["AU"] = {
{ hl = "NvimTreeFileMerge" },
{ hl = "NvimTreeFileStaged" },
},
-- not sure about this one
["AA"] = {
{ hl = "NvimTreeFileMerge" },
{ hl = "NvimTreeFileStaged" },
},
["AD"] = {
{ hl = "NvimTreeFileStaged" },
},
["MD"] = {
{ hl = "NvimTreeFileStaged" },
},
["AM"] = {
{ hl = "NvimTreeFileStaged" },
{ hl = "NvimTreeFileNew" },
{ hl = "NvimTreeFileDirty" },
},
["??"] = { { hl = "NvimTreeFileNew" } },
["R "] = { { hl = "NvimTreeFileRenamed" } },
["UU"] = { { hl = "NvimTreeFileMerge" } },
["UD"] = { { hl = "NvimTreeFileMerge" } },
["UA"] = { { hl = "NvimTreeFileMerge" } },
[" D"] = { { hl = "NvimTreeFileDeleted" } },
["DD"] = { { hl = "NvimTreeFileDeleted" } },
["RD"] = { { hl = "NvimTreeFileDeleted" } },
["D "] = {
{ hl = "NvimTreeFileDeleted" },
{ hl = "NvimTreeFileStaged" },
},
["DU"] = {
{ hl = "NvimTreeFileDeleted" },
{ hl = "NvimTreeFileMerge" },
},
[" A"] = { { hl = "none" } },
["RM"] = { { hl = "NvimTreeFileRenamed" } },
[" R"] = { { hl = "NvimTreeFileRenamed" } },
["!!"] = { { hl = "NvimTreeGitIgnored" } },
dirty = { { hl = "NvimTreeFileDirty" } },
}
get_git_hl = function(node)
local git_status = node.git_status
if not git_status then
return
end
local icons = git_hl[git_status]
if icons == nil then
utils.warn(
'Unrecognized git state "'
.. git_status
.. '". Please open up an issue on https://github.com/kyazdani42/nvim-tree.lua/issues with this message.'
)
icons = git_hl.dirty
end
-- TODO: how would we determine hl color when multiple git status are active ?
return icons[1].hl
-- return icons[#icons].hl
end
end
if icon_state.show_git_icon then
local git_icon_state = {
["M "] = { { icon = icon_state.icons.git_icons.staged, hl = "NvimTreeGitStaged" } },
[" M"] = { { icon = icon_state.icons.git_icons.unstaged, hl = "NvimTreeGitDirty" } },
["C "] = { { icon = icon_state.icons.git_icons.staged, hl = "NvimTreeGitStaged" } },
[" C"] = { { icon = icon_state.icons.git_icons.unstaged, hl = "NvimTreeGitDirty" } },
["CM"] = { { icon = icon_state.icons.git_icons.unstaged, hl = "NvimTreeGitDirty" } },
[" T"] = { { icon = icon_state.icons.git_icons.unstaged, hl = "NvimTreeGitDirty" } },
["MM"] = {
{ icon = icon_state.icons.git_icons.staged, hl = "NvimTreeGitStaged" },
{ icon = icon_state.icons.git_icons.unstaged, hl = "NvimTreeGitDirty" },
},
["MD"] = {
{ icon = icon_state.icons.git_icons.staged, hl = "NvimTreeGitStaged" },
},
["A "] = {
{ icon = icon_state.icons.git_icons.staged, hl = "NvimTreeGitStaged" },
},
["AD"] = {
{ icon = icon_state.icons.git_icons.staged, hl = "NvimTreeGitStaged" },
},
[" A"] = {
{ icon = icon_state.icons.git_icons.untracked, hl = "NvimTreeGitNew" },
},
-- not sure about this one
["AA"] = {
{ icon = icon_state.icons.git_icons.unmerged, hl = "NvimTreeGitMerge" },
{ icon = icon_state.icons.git_icons.untracked, hl = "NvimTreeGitNew" },
},
["AU"] = {
{ icon = icon_state.icons.git_icons.unmerged, hl = "NvimTreeGitMerge" },
{ icon = icon_state.icons.git_icons.untracked, hl = "NvimTreeGitNew" },
},
["AM"] = {
{ icon = icon_state.icons.git_icons.staged, hl = "NvimTreeGitStaged" },
{ icon = icon_state.icons.git_icons.unstaged, hl = "NvimTreeGitDirty" },
},
["??"] = { { icon = icon_state.icons.git_icons.untracked, hl = "NvimTreeGitNew" } },
["R "] = { { icon = icon_state.icons.git_icons.renamed, hl = "NvimTreeGitRenamed" } },
[" R"] = { { icon = icon_state.icons.git_icons.renamed, hl = "NvimTreeGitRenamed" } },
["RM"] = {
{ icon = icon_state.icons.git_icons.unstaged, hl = "NvimTreeGitDirty" },
{ icon = icon_state.icons.git_icons.renamed, hl = "NvimTreeGitRenamed" },
},
["UU"] = { { icon = icon_state.icons.git_icons.unmerged, hl = "NvimTreeGitMerge" } },
["UD"] = { { icon = icon_state.icons.git_icons.unmerged, hl = "NvimTreeGitMerge" } },
["UA"] = { { icon = icon_state.icons.git_icons.unmerged, hl = "NvimTreeGitMerge" } },
[" D"] = { { icon = icon_state.icons.git_icons.deleted, hl = "NvimTreeGitDeleted" } },
["D "] = { { icon = icon_state.icons.git_icons.deleted, hl = "NvimTreeGitDeleted" } },
["RD"] = { { icon = icon_state.icons.git_icons.deleted, hl = "NvimTreeGitDeleted" } },
["DD"] = { { icon = icon_state.icons.git_icons.deleted, hl = "NvimTreeGitDeleted" } },
["DU"] = {
{ icon = icon_state.icons.git_icons.deleted, hl = "NvimTreeGitDeleted" },
{ icon = icon_state.icons.git_icons.unmerged, hl = "NvimTreeGitMerge" },
},
["!!"] = { { icon = icon_state.icons.git_icons.ignored, hl = "NvimTreeGitIgnored" } },
dirty = { { icon = icon_state.icons.git_icons.unstaged, hl = "NvimTreeGitDirty" } },
}
get_git_icons = function(node, line, depth, icon_len)
local git_status = node.git_status
if not git_status then
return ""
end
local icon = ""
local icons = git_icon_state[git_status]
if not icons then
if vim.g.nvim_tree_git_hl ~= 1 then
utils.warn(
'Unrecognized git state "'
.. git_status
.. '". Please open up an issue on https://github.com/kyazdani42/nvim-tree.lua/issues with this message.'
)
end
icons = git_icon_state.dirty
end
for _, v in ipairs(icons) do
if #v.icon > 0 then
table.insert(hl, { v.hl, line, depth + icon_len + #icon, depth + icon_len + #icon + #v.icon })
icon = icon .. v.icon .. icon_padding
end
end
return icon
end
end
local picture = { local picture = {
jpg = true, jpg = true,
@@ -285,25 +113,18 @@ local picture = {
gif = true, gif = true,
} }
local function update_draw_data(tree, depth, markers) local function get_special_files_map()
local root_folder_modifier = vim.g.nvim_tree_root_folder_modifier or ":~" return vim.g.nvim_tree_special_files
local special = vim.g.nvim_tree_special_files
or { or {
["Cargo.toml"] = true, ["Cargo.toml"] = true,
Makefile = true, Makefile = true,
["README.md"] = true, ["README.md"] = true,
["readme.md"] = true, ["readme.md"] = true,
} }
end
if view.is_root_folder_visible(tree) then local function update_draw_data(tree, depth, markers)
local root_name = utils.path_join { local special = get_special_files_map()
utils.path_remove_trailing(vim.fn.fnamemodify(tree.cwd, root_folder_modifier)),
"..",
}
table.insert(lines, root_name)
table.insert(hl, { "NvimTreeRootFolder", index, 0, string.len(root_name) })
index = 1
end
for idx, node in ipairs(tree.nodes) do for idx, node in ipairs(tree.nodes) do
local padding = _padding.get_padding(depth, idx, tree, node, markers) local padding = _padding.get_padding(depth, idx, tree, node, markers)
@@ -312,12 +133,12 @@ local function update_draw_data(tree, depth, markers)
table.insert(hl, { "NvimTreeIndentMarker", index, 0, offset }) table.insert(hl, { "NvimTreeIndentMarker", index, 0, offset })
end end
local git_hl = get_git_hl(node) local git_hl = git.get_highlight(node)
if node.nodes then if node.nodes then
local has_children = #node.nodes ~= 0 or node.has_children local has_children = #node.nodes ~= 0 or node.has_children
local icon = get_folder_icon(node.open, node.link_to ~= nil, has_children) local icon = get_folder_icon(node.open, node.link_to ~= nil, has_children)
local git_icon = get_git_icons(node, index, offset, #icon) or "" local git_icon = git.get_icons(node, index, offset, #icon, hl) or ""
-- INFO: this is mandatory in order to keep gui attributes (bold/italics) -- INFO: this is mandatory in order to keep gui attributes (bold/italics)
local folder_hl = "NvimTreeFolderName" local folder_hl = "NvimTreeFolderName"
local name = node.name local name = node.name
@@ -358,11 +179,11 @@ local function update_draw_data(tree, depth, markers)
local git_icons local git_icons
if special[node.absolute_path] or special[node.name] then if special[node.absolute_path] or special[node.name] then
icon = get_special_icon() icon = get_special_icon()
git_icons = get_git_icons(node, index, offset, 0) git_icons = git.get_icons(node, index, offset, 0, hl)
table.insert(hl, { "NvimTreeSpecialFile", index, offset + #git_icons, -1 }) table.insert(hl, { "NvimTreeSpecialFile", index, offset + #git_icons, -1 })
else else
icon = get_file_icon(node.name, node.extension, index, offset) icon = get_file_icon(node.name, node.extension, index, offset)
git_icons = get_git_icons(node, index, offset, #icon) git_icons = git.get_icons(node, index, offset, #icon, hl)
end end
table.insert(lines, padding .. icon .. git_icons .. node.name) table.insert(lines, padding .. icon .. git_icons .. node.name)
@@ -399,6 +220,19 @@ end
local M = {} local M = {}
local function compute_header()
if view.is_root_folder_visible() then
local root_folder_modifier = vim.g.nvim_tree_root_folder_modifier or ":~"
local root_name = utils.path_join {
utils.path_remove_trailing(vim.fn.fnamemodify(TreeExplorer.cwd, root_folder_modifier)),
"..",
}
table.insert(lines, root_name)
table.insert(hl, { "NvimTreeRootFolder", index, 0, string.len(root_name) })
index = 1
end
end
function M.draw() function M.draw()
local bufnr = view.get_bufnr() local bufnr = view.get_bufnr()
if not TreeExplorer or not bufnr or not api.nvim_buf_is_loaded(bufnr) then if not TreeExplorer or not bufnr or not api.nvim_buf_is_loaded(bufnr) then
@@ -417,6 +251,8 @@ function M.draw()
and icon_state.show_folder_icon and icon_state.show_folder_icon
and icon_state.show_folder_arrows and icon_state.show_folder_arrows
_padding.reload_padding_function() _padding.reload_padding_function()
git.reload()
compute_header()
update_draw_data(TreeExplorer, show_arrows and 2 or 0, {}) update_draw_data(TreeExplorer, show_arrows and 2 or 0, {})
if view.is_help_ui() then if view.is_help_ui() then

View File

@@ -342,8 +342,8 @@ function M._prevent_buffer_override()
end) end)
end end
function M.is_root_folder_visible(tree) function M.is_root_folder_visible()
return tree.cwd and tree.cwd ~= "/" and not M.View.hide_root_folder return TreeExplorer.cwd ~= "/" and not M.View.hide_root_folder
end end
local DEFAULT_CONFIG = { local DEFAULT_CONFIG = {