Skip to content

Commit

Permalink
Merge pull request stevearc#157 from JeremyWells227/master
Browse files Browse the repository at this point in the history
Add support for text highlight groups in lualine module
  • Loading branch information
stevearc authored Oct 14, 2022
2 parents 89a61da + 90d775c commit 193fb2f
Show file tree
Hide file tree
Showing 2 changed files with 74 additions and 14 deletions.
30 changes: 30 additions & 0 deletions lua/aerial/highlight.lua
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,37 @@ M.create_highlight_groups = function()
highlight default link AerialStruct NONE
highlight default link AerialTypeParameter NONE
highlight default link AerialVariable NONE
]])
end

M.identifiers = {
"Array",
"Boolean",
"Class",
"Constant",
"Constructor",
"Enum",
"EnumMember",
"Event",
"Field",
"File",
"Function",
"Interface",
"Key",
"Method",
"Module",
"Namespace",
"Null",
"Number",
"Object",
"Operator",
"Package",
"Property",
"String",
"Struct",
"TypeParameter",
"Variable",
}

return M
58 changes: 44 additions & 14 deletions lua/lualine/components/aerial.lua
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,8 @@

local M = require("lualine.component"):extend()
local aerial = require("aerial")
local utils = require("lualine.utils.utils")
local identifiers = require("aerial.highlight").identifiers

local default_options = {
sep = "",
Expand All @@ -53,15 +55,21 @@ local default_options = {
exact = true,
}

local function color_icon(symbol_kind, icon, colored)
if colored then
return string.format("%%#%s#%s%%##", "Aerial" .. symbol_kind .. "Icon", icon)
else
return icon
function M:color_for_lualine()
self.highlight_groups = {}
for _, symbol_kind in ipairs(identifiers) do
local hl = "Aerial" .. symbol_kind
local hl_icon = "Aerial" .. symbol_kind .. "Icon"
local color = { fg = utils.extract_highlight_colors(hl, "fg") }
local color_icon = { fg = utils.extract_highlight_colors(hl_icon, "fg") }
self.highlight_groups[symbol_kind] = {
icon = self:create_hl(color_icon, symbol_kind .. "Icon"),
text = self:create_hl(color, symbol_kind),
}
end
end

local function format_status(symbols, depth, separator, icons_enabled, colored)
function M:format_status(symbols, depth, separator, icons_enabled, colored)
local parts = {}
depth = depth or #symbols

Expand All @@ -72,14 +80,20 @@ local function format_status(symbols, depth, separator, icons_enabled, colored)
end

for _, symbol in ipairs(symbols) do
local name = symbol.name
if colored then
name = self:format_hl(self.highlight_groups[symbol.kind].text) .. name
end
if icons_enabled then
local icon = color_icon(symbol.kind, symbol.icon, colored)
table.insert(parts, string.format("%s %s", icon, symbol.name))
local icon = symbol.icon
if colored then
icon = self:format_hl(self.highlight_groups[symbol.kind].icon) .. icon
end
table.insert(parts, string.format("%s %s", icon, name))
else
table.insert(parts, symbol.name)
table.insert(parts, name)
end
end

return table.concat(parts, separator)
end

Expand All @@ -90,6 +104,18 @@ function M:init(options)
if self.options.colored == nil then
self.options.colored = true
end
if self.options.colored then
require("aerial.highlight").create_highlight_groups()
self:color_for_lualine()
vim.api.nvim_create_autocmd("ColorScheme", {
desc = "Update lualine aerial component colors",
pattern = "*",
callback = function()
require("aerial.highlight").create_highlight_groups()
self:color_for_lualine()
end,
})
end
self.get_status = self.get_status_normal

if self.options.dense then
Expand All @@ -103,7 +129,7 @@ end

function M:get_status_normal()
local symbols = aerial.get_location(self.options.exact)
local status = format_status(
local status = self:format_status(
symbols,
self.options.depth,
self.options.sep,
Expand All @@ -115,18 +141,22 @@ end

function M:get_status_dense()
local symbols = aerial.get_location(self.options.exact)
local status = format_status(
local status = self:format_status(
symbols,
self.options.depth,
self.options.dense_sep,
-- In dense mode icons aren't rendered togeter with symbols. A single icon
-- at the beginning of status line is rendered instead. See below.
false
false,
self.options.colored
)

if self.options.icons_enabled and not vim.tbl_isempty(symbols) then
local symbol = symbols[#symbols]
local icon = color_icon(symbol.kind, symbol.icon, self.options.colored)
local icon = symbol.icon
if self.options.colored then
icon = self:format_hl(self.highlight_groups[symbol.kind].icon) .. icon
end
status = string.format("%s %s", icon, status)
end
return status
Expand Down

0 comments on commit 193fb2f

Please sign in to comment.