Skip to content
This repository has been archived by the owner on Jan 9, 2025. It is now read-only.

Commit

Permalink
fix(theme): do not rely on hl's values when loading theme
Browse files Browse the repository at this point in the history
Relying on those causes issues (#44), as they might not be defined yet
due to being called on `ColorScheme` event.
  • Loading branch information
utilyre committed Jan 7, 2023
1 parent 71b2143 commit 4c43a3c
Showing 1 changed file with 30 additions and 13 deletions.
43 changes: 30 additions & 13 deletions lua/barbecue/theme.lua
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,9 @@ local default = require("barbecue.theme.default")

local M = {}

---@type barbecue.Theme|nil
local current_theme

---an abstraction layer for highlight groups
M.highlights = {
normal = "barbecue_normal",
Expand Down Expand Up @@ -45,6 +48,9 @@ M.highlights = {
context_type_parameter = "barbecue_context_type_parameter",
}

---@type { name: string, icon: string, color: string }[]
local file_icons = {}

---loads theme from module `barbecue.theme` by `name`
---@param name string?
---@return barbecue.Theme
Expand Down Expand Up @@ -79,6 +85,7 @@ function M.load()
theme = vim.tbl_deep_extend("force", get_theme(), config.user.theme)
end
normalize_theme(theme)
current_theme = theme

for key, name in pairs(M.highlights) do
vim.api.nvim_set_hl(
Expand All @@ -87,6 +94,14 @@ function M.load()
vim.tbl_extend("force", theme.normal, theme[key])
)
end

for _, icon in pairs(file_icons) do
vim.api.nvim_set_hl(
0,
string.format("barbecue_fi_%s", icon.name),
vim.tbl_extend("force", theme.normal, { foreground = icon.color })
)
end
end

---returns and caches the icon of `filename`
Expand All @@ -99,31 +114,33 @@ function M.get_file_icon(filename, filetype)
local basename = vim.fn.fnamemodify(filename, ":t")
local extension = vim.fn.fnamemodify(filename, ":e")

local devicons_icon, devicons_highlight =
devicons.get_icon(basename, extension, { default = false })
if devicons_icon == nil then
devicons_icon, devicons_highlight = devicons.get_icon_by_filetype(filetype)
if devicons_icon == nil then return nil end
local icons = devicons.get_icons()
local icon = icons[basename] or icons[extension]
if icon == nil then
-- FIXME: respect devicons' default_icon
local name = devicons.get_icon_name_by_filetype(filetype)
icon = icons[name]
if icon == nil then return nil end
end

local key = string.format("filetype_%s", devicons_highlight)
if M.highlights[key] == nil then
M.highlights[key] = string.format("barbecue_%s", key)
local highlight = string.format("barbecue_fi_%s", icon.name)
if file_icons[icon.name] == nil then
file_icons[icon.name] = icon

vim.api.nvim_set_hl(
0,
M.highlights[key],
highlight,
vim.tbl_extend(
"force",
utils.get_hl_by_name(M.highlights.normal),
utils.get_hl_by_name(devicons_highlight)
current_theme ~= nil and current_theme.normal or {},
{ foreground = icon.color }
)
)
end

return {
devicons_icon,
highlight = M.highlights[key],
icon.icon,
highlight = highlight,
}
end

Expand Down

0 comments on commit 4c43a3c

Please sign in to comment.