chore: resolve undefined-field warnings, fix link git statuses, rewrite devicons (#2968)
* add todo * refactor(#2886): multi instance: node class refactoring: extract links, *_git_status (#2944) * extract DirectoryLinkNode and FileLinkNode, move Node methods to children * temporarily move DirectoryNode methods into BaseNode for easier reviewing * move mostly unchanged DirectoryNode methods back to BaseNode * tidy * git.git_status_file takes an array * update git status of links * luacheck hack * safer git_status_dir * refactor(#2886): multi instance: node class refactoring: DirectoryNode:expand_or_collapse (#2957) move expand_or_collapse to DirectoryNode * refactor(#2886): multi instance: node group functions refactoring (#2959) * move last_group_node to DirectoryNode * move add BaseNode:as and more doc * revert parameter name changes * revert parameter name changes * add Class * move group methods into DN * tidy group methods * tidy group methods * tidy group methods * tidy group methods * parent is DirectoryNode * tidy expand all * BaseNode -> Node * move watcher to DirectoryNode * last_group_node is DirectoryNode only * simplify create-file * simplify parent * simplify collapse-all * simplify live-filter * style * move lib.get_cursor_position to Explorer * move lib.get_node_at_cursor to Explorer * move lib.get_nodes to Explorer * move place_cursor_on_node to Explorer * resolve resource leak in purge_all_state * move many autocommands into Explorer * post merge tidy * chore: resolve undefined-field * chore: resolve undefined-field * chore: resolve undefined-field * chore: resolve undefined-field * chore: resolve undefined-field * chore: resolve undefined-field * chore: resolve undefined-field * Revert "chore: resolve undefined-field" This reverts commit be546ff18d41f28466b065c857e1e041659bd2c8. * chore: resolve undefined-field * chore: resolve undefined-field * chore: resolve undefined-field * chore: resolve undefined-field * chore: resolve undefined-field * chore: resolve undefined-field * chore: resolve undefined-field * chore: resolve undefined-field * chore: resolve undefined-field * chore: resolve undefined-field * chore: resolve undefined-field * chore: resolve undefined-field * chore: resolve undefined-field * chore: resolve undefined-field * chore: resolve undefined-field * chore: resolve undefined-field * chore: resolve undefined-field * chore: resolve undefined-field * chore: resolve undefined-field * chore: resolve undefined-field * chore: resolve undefined-field * chore: resolve undefined-field * chore: resolve undefined-field * chore: resolve undefined-field * chore: resolve undefined-field * chore: resolve undefined-field * chore: resolve undefined-field * chore: resolve undefined-field * chore: resolve undefined-field * chore: resolve undefined-field * chore: resolve undefined-field * Revert "chore: resolve undefined-field" This reverts commite82db1c44d. * chore: resolve undefined-field * chore: class new is now generic * chore: resolve undefined-field * chore: resolve undefined-field * chore: resolve undefined-field * chore: resolve undefined-field * chore: resolve undefined-field * chore: resolve undefined-field * chore: resolve undefined-field * chore: resolve undefined-field * chore: resolve undefined-field * chore: resolve undefined-field * chore: resolve undefined-field * chore: resolve undefined-field * chore: resolve undefined-field * chore: resolve undefined-field * Revert "chore: resolve undefined-field" This reverts commit0e9b844d22. * move icon builders into node classes * move icon builders into node classes * chore: resolve undefined-field * chore: resolve undefined-field * chore: resolve undefined-field * chore: resolve undefined-field * chore: resolve undefined-field * chore: resolve undefined-field * move folder specifics from icons to Directory * move folder specifics from icons to Directory * move folder specifics from icons to Directory * move folder specifics from icons to Directory * move file specifics from icons to File * clean up sorters * chore: resolve undefined-field * tidy hl icon name * file devicon uses library to fall back * file devicon uses library to fall back * file devicon uses library to fall back
This commit is contained in:
committed by
GitHub
parent
c22124b374
commit
610a1c189b
35
lua/nvim-tree/renderer/components/devicons.lua
Normal file
35
lua/nvim-tree/renderer/components/devicons.lua
Normal file
@@ -0,0 +1,35 @@
|
||||
---@alias devicons_get_icon fun(name: string, ext: string?, opts: table?): string?, string?
|
||||
---@alias devicons_setup fun(opts: table?)
|
||||
|
||||
---@class (strict) DevIcons?
|
||||
---@field setup devicons_setup
|
||||
---@field get_icon devicons_get_icon
|
||||
local devicons
|
||||
|
||||
local M = {}
|
||||
|
||||
---Wrapper around nvim-web-devicons, nils if devicons not available
|
||||
---@type devicons_get_icon
|
||||
function M.get_icon(name, ext, opts)
|
||||
if devicons then
|
||||
return devicons.get_icon(name, ext, opts)
|
||||
else
|
||||
return nil, nil
|
||||
end
|
||||
end
|
||||
|
||||
---Attempt to use nvim-web-devicons if present and enabled for file or folder
|
||||
---@param opts table
|
||||
function M.setup(opts)
|
||||
if opts.renderer.icons.show.file or opts.renderer.icons.show.folder then
|
||||
local ok, di = pcall(require, "nvim-web-devicons")
|
||||
if ok then
|
||||
devicons = di --[[@as DevIcons]]
|
||||
|
||||
-- does nothing if already called i.e. doesn't clobber previous user setup
|
||||
devicons.setup()
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
return M
|
||||
@@ -1,6 +1,8 @@
|
||||
local HL_POSITION = require("nvim-tree.enum").HL_POSITION
|
||||
local diagnostics = require("nvim-tree.diagnostics")
|
||||
|
||||
local DirectoryNode = require("nvim-tree.node.directory")
|
||||
|
||||
local M = {
|
||||
-- highlight strings for the icons
|
||||
HS_ICON = {},
|
||||
@@ -24,7 +26,7 @@ function M.get_highlight(node)
|
||||
|
||||
local group
|
||||
local diag_status = diagnostics.get_diag_status(node)
|
||||
if node.nodes then
|
||||
if node:is(DirectoryNode) then
|
||||
group = M.HS_FOLDER[diag_status and diag_status.value]
|
||||
else
|
||||
group = M.HS_FILE[diag_status and diag_status.value]
|
||||
|
||||
@@ -1,129 +0,0 @@
|
||||
local M = { i = {} }
|
||||
|
||||
local function config_symlinks()
|
||||
M.i.symlink = #M.config.glyphs.symlink > 0 and M.config.glyphs.symlink or ""
|
||||
M.i.symlink_arrow = M.config.symlink_arrow
|
||||
end
|
||||
|
||||
---@return string icon
|
||||
---@return string? name
|
||||
local function empty()
|
||||
return "", nil
|
||||
end
|
||||
|
||||
---@param node Node
|
||||
---@param has_children boolean
|
||||
---@return string icon
|
||||
---@return string? name
|
||||
local function get_folder_icon_default(node, has_children)
|
||||
local is_symlink = node.link_to ~= nil
|
||||
local n
|
||||
if is_symlink and node.open then
|
||||
n = M.config.glyphs.folder.symlink_open
|
||||
elseif is_symlink then
|
||||
n = M.config.glyphs.folder.symlink
|
||||
elseif node.open then
|
||||
if has_children then
|
||||
n = M.config.glyphs.folder.open
|
||||
else
|
||||
n = M.config.glyphs.folder.empty_open
|
||||
end
|
||||
else
|
||||
if has_children then
|
||||
n = M.config.glyphs.folder.default
|
||||
else
|
||||
n = M.config.glyphs.folder.empty
|
||||
end
|
||||
end
|
||||
return n, nil
|
||||
end
|
||||
|
||||
---@param node Node
|
||||
---@param has_children boolean
|
||||
---@return string icon
|
||||
---@return string? name
|
||||
local function get_folder_icon_webdev(node, has_children)
|
||||
local icon, hl_group = M.devicons.get_icon(node.name, node.extension)
|
||||
if not M.config.web_devicons.folder.color then
|
||||
hl_group = nil
|
||||
end
|
||||
if icon ~= nil then
|
||||
return icon, hl_group
|
||||
else
|
||||
return get_folder_icon_default(node, has_children)
|
||||
end
|
||||
end
|
||||
|
||||
---@return string icon
|
||||
---@return string? name
|
||||
local function get_file_icon_default()
|
||||
local hl_group = "NvimTreeFileIcon"
|
||||
local icon = M.config.glyphs.default
|
||||
if #icon > 0 then
|
||||
return icon, hl_group
|
||||
else
|
||||
return "", nil
|
||||
end
|
||||
end
|
||||
|
||||
---@param fname string
|
||||
---@param extension string
|
||||
---@return string icon
|
||||
---@return string? name
|
||||
local function get_file_icon_webdev(fname, extension)
|
||||
local icon, hl_group = M.devicons.get_icon(fname, extension)
|
||||
if not M.config.web_devicons.file.color then
|
||||
hl_group = "NvimTreeFileIcon"
|
||||
end
|
||||
if icon and hl_group ~= "DevIconDefault" then
|
||||
return icon, hl_group
|
||||
elseif string.match(extension, "%.(.*)") then
|
||||
-- If there are more extensions to the file, try to grab the icon for them recursively
|
||||
return get_file_icon_webdev(fname, string.match(extension, "%.(.*)"))
|
||||
else
|
||||
local devicons_default = M.devicons.get_default_icon()
|
||||
if devicons_default and type(devicons_default.icon) == "string" and type(devicons_default.name) == "string" then
|
||||
return devicons_default.icon, "DevIcon" .. devicons_default.name
|
||||
else
|
||||
return get_file_icon_default()
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
local function config_file_icon()
|
||||
if M.config.show.file then
|
||||
if M.devicons and M.config.web_devicons.file.enable then
|
||||
M.get_file_icon = get_file_icon_webdev
|
||||
else
|
||||
M.get_file_icon = get_file_icon_default
|
||||
end
|
||||
else
|
||||
M.get_file_icon = empty
|
||||
end
|
||||
end
|
||||
|
||||
local function config_folder_icon()
|
||||
if M.config.show.folder then
|
||||
if M.devicons and M.config.web_devicons.folder.enable then
|
||||
M.get_folder_icon = get_folder_icon_webdev
|
||||
else
|
||||
M.get_folder_icon = get_folder_icon_default
|
||||
end
|
||||
else
|
||||
M.get_folder_icon = empty
|
||||
end
|
||||
end
|
||||
|
||||
function M.reset_config()
|
||||
config_symlinks()
|
||||
config_file_icon()
|
||||
config_folder_icon()
|
||||
end
|
||||
|
||||
function M.setup(opts)
|
||||
M.config = opts.renderer.icons
|
||||
|
||||
M.devicons = pcall(require, "nvim-web-devicons") and require("nvim-web-devicons") or nil
|
||||
end
|
||||
|
||||
return M
|
||||
@@ -2,13 +2,13 @@ local M = {}
|
||||
|
||||
M.diagnostics = require("nvim-tree.renderer.components.diagnostics")
|
||||
M.full_name = require("nvim-tree.renderer.components.full-name")
|
||||
M.icons = require("nvim-tree.renderer.components.icons")
|
||||
M.devicons = require("nvim-tree.renderer.components.devicons")
|
||||
M.padding = require("nvim-tree.renderer.components.padding")
|
||||
|
||||
function M.setup(opts)
|
||||
M.diagnostics.setup(opts)
|
||||
M.full_name.setup(opts)
|
||||
M.icons.setup(opts)
|
||||
M.devicons.setup(opts)
|
||||
M.padding.setup(opts)
|
||||
end
|
||||
|
||||
|
||||
@@ -1,3 +1,5 @@
|
||||
local DirectoryNode = require("nvim-tree.node.directory")
|
||||
|
||||
local M = {}
|
||||
|
||||
local function check_siblings_for_folder(node, with_arrows)
|
||||
@@ -62,7 +64,7 @@ end
|
||||
---@param node Node
|
||||
---@param markers table
|
||||
---@param early_stop integer?
|
||||
---@return HighlightedString[]
|
||||
---@return HighlightedString
|
||||
function M.get_indent_markers(depth, idx, nodes_number, node, markers, early_stop)
|
||||
local str = ""
|
||||
|
||||
@@ -90,8 +92,9 @@ function M.get_arrows(node)
|
||||
local str
|
||||
local hl = "NvimTreeFolderArrowClosed"
|
||||
|
||||
if node.nodes then
|
||||
if node.open then
|
||||
local dir = node:as(DirectoryNode)
|
||||
if dir then
|
||||
if dir.open then
|
||||
str = M.config.icons.glyphs.folder["arrow_open"] .. " "
|
||||
hl = "NvimTreeFolderArrowOpen"
|
||||
else
|
||||
|
||||
Reference in New Issue
Block a user