From 540362f44bc17ae8101828eec00fd00ffc4148bb Mon Sep 17 00:00:00 2001 From: Alvaro Sevilla Date: Tue, 6 Jul 2021 12:19:22 +0100 Subject: Fix devicon highlighting --- lua/luatab/highlight.lua | 40 ++++++++++++++++++++++++++++++++++++++++ lua/luatab/init.lua | 8 ++++++-- 2 files changed, 46 insertions(+), 2 deletions(-) create mode 100644 lua/luatab/highlight.lua diff --git a/lua/luatab/highlight.lua b/lua/luatab/highlight.lua new file mode 100644 index 0000000..c696135 --- /dev/null +++ b/lua/luatab/highlight.lua @@ -0,0 +1,40 @@ +-- Shamelessly stolen from +-- https://github.com/hoob3rt/lualine.nvim/blob/master/lua/lualine/utils/utils.lua + +local M = {} + +M.highlight = function(name, foreground, background) + local command = {'highlight', name} + if foreground and foreground ~= 'none' then + table.insert(command, 'guifg=' .. foreground) + end + if background and background ~= 'none' then + table.insert(command, 'guibg=' .. background) + end + vim.cmd(table.concat(command, ' ')) +end + +M.create_component_highlight_group = function(color, highlight_tag) + if color.bg and color.fg then + local highlight_group_name = table.concat({'luatab', highlight_tag}, '_') + M.highlight(highlight_group_name, color.fg, color.bg) + return highlight_group_name + end +end + +M.extract_highlight_colors = function(color_group, scope) + if vim.fn.hlexists(color_group) == 0 then return nil end + local color = vim.api.nvim_get_hl_by_name(color_group, true) + if color.background ~= nil then + color.bg = string.format('#%06x', color.background) + color.background = nil + end + if color.foreground ~= nil then + color.fg = string.format('#%06x', color.foreground) + color.foreground = nil + end + if scope then return color[scope] end + return color +end + +return M diff --git a/lua/luatab/init.lua b/lua/luatab/init.lua index 2141bbc..597af3a 100644 --- a/lua/luatab/init.lua +++ b/lua/luatab/init.lua @@ -44,13 +44,17 @@ local function tabDevicon(bufnr, isSelected) dev, devhl = require'nvim-web-devicons'.get_icon(file, vim.fn.getbufvar(bufnr, '&filetype')) end if dev then - return (isSelected and '%#'..devhl..'#' or '') .. dev .. (isSelected and '%#TabLineSel#' or '') + local h = require'luatab.highlight' + local fg = h.extract_highlight_colors(devhl, 'fg') + local bg = h.extract_highlight_colors('TabLineSel', 'bg') + local hl = h.create_component_highlight_group({bg = bg, fg = fg}, devhl) + return (isSelected and '%#'..hl..'#' or '') .. dev .. (isSelected and '%#TabLineSel#' or '') .. ' ' end return '' end local function tabSeparator(current) - return ' ' .. (current < vim.fn.tabpagenr('$') and '%#TabLine#|' or '') + return (current < vim.fn.tabpagenr('$') and '%#TabLine#|' or '') end local function formatTab(current) -- cgit v1.2.3