From 144bce74a57261ec67e46fe3d2cf7a79e79e876e Mon Sep 17 00:00:00 2001 From: kiyan Date: Wed, 9 Mar 2022 21:32:15 +0100 Subject: [PATCH] refacto: move renderer git into file also view.is_root_folder_modifier_visible don't take the tree as parameter --- lua/nvim-tree/actions/find-file.lua | 2 +- lua/nvim-tree/actions/movements.lua | 4 +- lua/nvim-tree/actions/search-node.lua | 2 +- lua/nvim-tree/renderer/git.lua | 169 ++++++++++++++++++++ lua/nvim-tree/renderer/init.lua | 214 +++----------------------- lua/nvim-tree/view.lua | 4 +- 6 files changed, 200 insertions(+), 195 deletions(-) create mode 100644 lua/nvim-tree/renderer/git.lua diff --git a/lua/nvim-tree/actions/find-file.lua b/lua/nvim-tree/actions/find-file.lua index b97850bf..f3177232 100644 --- a/lua/nvim-tree/actions/find-file.lua +++ b/lua/nvim-tree/actions/find-file.lua @@ -12,7 +12,7 @@ function M.fn(fname) end 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 function iterate_nodes(nodes) diff --git a/lua/nvim-tree/actions/movements.lua b/lua/nvim-tree/actions/movements.lua index 71fdbd23..e341e766 100644 --- a/lua/nvim-tree/actions/movements.lua +++ b/lua/nvim-tree/actions/movements.lua @@ -56,7 +56,7 @@ function M.parent_node(should_close) parent.open = false altered_tree = true end - if not view.is_root_folder_visible(TreeExplorer) then + if not view.is_root_folder_visible() then line = line - 1 end view.set_cursor { line, 0 } @@ -109,7 +109,7 @@ function M.sibling(direction) local target_node = parent.nodes[index] 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 end view.set_cursor { line, 0 } diff --git a/lua/nvim-tree/actions/search-node.lua b/lua/nvim-tree/actions/search-node.lua index 8c0db537..91036ec6 100644 --- a/lua/nvim-tree/actions/search-node.lua +++ b/lua/nvim-tree/actions/search-node.lua @@ -77,7 +77,7 @@ function M.fn() end 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 end diff --git a/lua/nvim-tree/renderer/git.lua b/lua/nvim-tree/renderer/git.lua new file mode 100644 index 00000000..1970697b --- /dev/null +++ b/lua/nvim-tree/renderer/git.lua @@ -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 diff --git a/lua/nvim-tree/renderer/init.lua b/lua/nvim-tree/renderer/init.lua index a8241209..55696cb3 100644 --- a/lua/nvim-tree/renderer/init.lua +++ b/lua/nvim-tree/renderer/init.lua @@ -3,6 +3,7 @@ local view = require "nvim-tree.view" local _padding = require "nvim-tree.renderer.padding" local _help = require "nvim-tree.renderer.help" local _icons = require "nvim-tree.renderer.icons" +local git = require "nvim-tree.renderer.git" local api = vim.api @@ -104,179 +105,6 @@ if icon_state.show_file_icon then 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 = { jpg = true, @@ -285,25 +113,18 @@ local picture = { gif = true, } -local function update_draw_data(tree, depth, markers) - local root_folder_modifier = vim.g.nvim_tree_root_folder_modifier or ":~" - local special = vim.g.nvim_tree_special_files +local function get_special_files_map() + return vim.g.nvim_tree_special_files or { ["Cargo.toml"] = true, Makefile = true, ["README.md"] = true, ["readme.md"] = true, } +end - if view.is_root_folder_visible(tree) then - local root_name = utils.path_join { - 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 +local function update_draw_data(tree, depth, markers) + local special = get_special_files_map() for idx, node in ipairs(tree.nodes) do 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 }) end - local git_hl = get_git_hl(node) + local git_hl = git.get_highlight(node) if node.nodes then local has_children = #node.nodes ~= 0 or node.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) local folder_hl = "NvimTreeFolderName" local name = node.name @@ -358,11 +179,11 @@ local function update_draw_data(tree, depth, markers) local git_icons if special[node.absolute_path] or special[node.name] then 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 }) else 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 table.insert(lines, padding .. icon .. git_icons .. node.name) @@ -399,6 +220,19 @@ end 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() local bufnr = view.get_bufnr() 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_arrows _padding.reload_padding_function() + git.reload() + compute_header() update_draw_data(TreeExplorer, show_arrows and 2 or 0, {}) if view.is_help_ui() then diff --git a/lua/nvim-tree/view.lua b/lua/nvim-tree/view.lua index e4ea41a4..7ba8dfac 100644 --- a/lua/nvim-tree/view.lua +++ b/lua/nvim-tree/view.lua @@ -342,8 +342,8 @@ function M._prevent_buffer_override() end) end -function M.is_root_folder_visible(tree) - return tree.cwd and tree.cwd ~= "/" and not M.View.hide_root_folder +function M.is_root_folder_visible() + return TreeExplorer.cwd ~= "/" and not M.View.hide_root_folder end local DEFAULT_CONFIG = {