Skip to content

#1172 add renderer.icons.webdev_colors default true #1175

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 1 commit into from
Apr 18, 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
3 changes: 3 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -151,6 +151,9 @@ require'nvim-tree'.setup { -- BEGIN_DEFAULT_OPTS
none = " ",
},
},
icons = {
webdev_colors = true,
},
},
hijack_directories = {
enable = true,
Expand Down
9 changes: 9 additions & 0 deletions doc/nvim-tree-lua.txt
Original file line number Diff line number Diff line change
Expand Up @@ -120,6 +120,9 @@ function.
none = " ",
},
},
icons = {
webdev_colors = true,
},
},
hijack_directories = {
enable = true,
Expand Down Expand Up @@ -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

Expand Down
3 changes: 3 additions & 0 deletions lua/nvim-tree.lua
Original file line number Diff line number Diff line change
Expand Up @@ -345,6 +345,9 @@ local DEFAULT_OPTS = { -- BEGIN_DEFAULT_OPTS
none = " ",
},
},
icons = {
webdev_colors = true,
},
},
hijack_directories = {
enable = true,
Expand Down
59 changes: 33 additions & 26 deletions lua/nvim-tree/renderer/init.lua
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down Expand Up @@ -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 ""
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This has been replaced by get_file_icon_default, so that we get the hl group colour on files that do not have a web dev icon.

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
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is the key change.

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

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