diff --git a/README.md b/README.md index a3b39a2fc57..50cfbb508c0 100644 --- a/README.md +++ b/README.md @@ -151,6 +151,9 @@ require'nvim-tree'.setup { -- BEGIN_DEFAULT_OPTS none = " ", }, }, + icons = { + webdev_colors = true, + }, }, hijack_directories = { enable = true, diff --git a/doc/nvim-tree-lua.txt b/doc/nvim-tree-lua.txt index a64760a1fc8..9d7ed094742 100644 --- a/doc/nvim-tree-lua.txt +++ b/doc/nvim-tree-lua.txt @@ -120,6 +120,9 @@ function. none = " ", }, }, + icons = { + webdev_colors = true, + }, }, hijack_directories = { enable = true, @@ -431,6 +434,12 @@ Here is a list of the options available in the setup call: type: `table` default: `{ corner = "└ ", edge = "│ ", none = " ", }` + - |renderer.icons|: configuration options for icons + + - |renderer.icons.webdev_colors|: use the webdev icon colors, otherwise `NvimTreeFileIcon`. + type: `boolean` + default: `true` + *nvim-tree.filters* |filters|: filtering options diff --git a/lua/nvim-tree.lua b/lua/nvim-tree.lua index fd34be0f412..fa37a2643e4 100644 --- a/lua/nvim-tree.lua +++ b/lua/nvim-tree.lua @@ -345,6 +345,9 @@ local DEFAULT_OPTS = { -- BEGIN_DEFAULT_OPTS none = " ", }, }, + icons = { + webdev_colors = true, + }, }, hijack_directories = { enable = true, diff --git a/lua/nvim-tree/renderer/init.lua b/lua/nvim-tree/renderer/init.lua index 552513f8c9a..6184798e439 100644 --- a/lua/nvim-tree/renderer/init.lua +++ b/lua/nvim-tree/renderer/init.lua @@ -9,12 +9,15 @@ local core = require "nvim-tree.core" local api = vim.api +local M = {} + local lines = {} local hl = {} local index = 0 local namespace_id = api.nvim_create_namespace "NvimTreeHighlights" local icon_state = _icons.get_config() +local web_devicons = icon_state.has_devicons and require "nvim-web-devicons" or nil local should_hl_opened_files = (vim.g.nvim_tree_highlight_opened_files or 0) ~= 0 @@ -63,34 +66,39 @@ end local get_file_icon = function() return "" end -if icon_state.show_file_icon then - if icon_state.has_devicons then - local web_devicons = require "nvim-web-devicons" - get_file_icon = function(fname, extension, line, depth) - local icon, hl_group = web_devicons.get_icon(fname, extension) +local get_file_icon_default = function(_, _, line, depth) + local hl_group = "NvimTreeFileIcon" + local icon = icon_state.icons.default + if #icon > 0 then + table.insert(hl, { hl_group, line, depth, depth + #icon + 1 }) + end + return #icon > 0 and icon .. icon_padding or "" +end - if icon and hl_group ~= "DevIconDefault" then - if hl_group then - table.insert(hl, { hl_group, line, depth, depth + #icon + 1 }) - end - return icon .. icon_padding - 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(fname, string.match(extension, "%.(.*)"), line, depth) - else - return #icon_state.icons.default > 0 and icon_state.icons.default .. icon_padding or "" - end +local get_file_icon_webdev = function(fname, extension, line, depth) + local icon, hl_group = web_devicons.get_icon(fname, extension) + if not M.config.icons.webdev_colors then + hl_group = "NvimTreeFileIcon" + end + if icon and hl_group ~= "DevIconDefault" then + if hl_group then + table.insert(hl, { hl_group, line, depth, depth + #icon + 1 }) end + return icon .. icon_padding + 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(fname, string.match(extension, "%.(.*)"), line, depth) else - get_file_icon = function(_, _, line, depth) - local hl_group = "NvimTreeFileIcon" - local icon = icon_state.icons.default - if #icon > 0 then - table.insert(hl, { hl_group, line, depth, depth + #icon + 1 }) - end - return #icon > 0 and icon .. icon_padding or "" - end + return get_file_icon_default(fname, extension, line, depth) + end +end + +if icon_state.show_file_icon then + if web_devicons then + get_file_icon = get_file_icon_webdev + else + get_file_icon = get_file_icon_default end end @@ -224,8 +232,6 @@ local function update_draw_data(tree, depth, markers) end end -local M = {} - local function compute_header() if view.is_root_folder_visible(core.get_cwd()) then local root_folder_modifier = vim.g.nvim_tree_root_folder_modifier or ":~" @@ -292,6 +298,7 @@ end function M.setup(opts) M.config = { indent_markers = opts.renderer.indent_markers, + icons = opts.renderer.icons, } require("nvim-tree.renderer.padding").setup(opts)