From 11af9c5746cb6eea20fe192a22bade9d15d15d02 Mon Sep 17 00:00:00 2001 From: nenikitov Date: Fri, 30 Jun 2023 14:44:47 -0400 Subject: [PATCH 1/2] =?UTF-8?q?=E2=9C=A8=20feature:=20added=20support=20fo?= =?UTF-8?q?r=20passing=20highlight=20group=20names=20directly?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- lua/cokeline/components.lua | 18 ++++++++++++------ lua/cokeline/hlgroups.lua | 11 +++++++++++ 2 files changed, 23 insertions(+), 6 deletions(-) diff --git a/lua/cokeline/components.lua b/lua/cokeline/components.lua index 1cdc2c3..e302cfe 100644 --- a/lua/cokeline/components.lua +++ b/lua/cokeline/components.lua @@ -14,6 +14,7 @@ local map = vim.tbl_map ---@field style string|fun(buffer:Buffer): string ---@field fg string|fun(buffer:Buffer): string ---@field bg string|fun(buffer:Buffer): string +---@field hl_group string|fun(buffer:Buffer): string ---@field on_click ClickHandler | nil ---@field on_mouse_enter MouseEnterHandler | nil ---@field on_mouse_leave MouseLeaveHandler | nil @@ -38,6 +39,7 @@ Component.new = function(c, i, default_hl) fg = c.fg or default_hl.fg or "NONE", bg = c.bg or default_hl.bg or "NONE", style = c.style or default_hl.style or "NONE", + hl_group = c.hl_group, delete_buffer_on_left_click = c.delete_buffer_on_left_click or false, on_click = c.on_click, on_mouse_enter = c.on_mouse_enter, @@ -88,12 +90,16 @@ Component.render = function(self, context) or evaluate(_G.cokeline.config.default_hl.bg) or "NONE" - component.hlgroup = Hlgroup.new( - ("Cokeline_%s_%s"):format(component.bufnr or context.kind, self.index), - style, - fg, - bg - ) + if component.hl_group then + component.hlgroup = Hlgroup.new_existing(evaluate(component.hl_group)) + else + component.hlgroup = Hlgroup.new( + ("Cokeline_%s_%s"):format(component.bufnr or context.kind, self.index), + style, + fg, + bg + ) + end return component end diff --git a/lua/cokeline/hlgroups.lua b/lua/cokeline/hlgroups.lua index f7a8e85..7abace6 100644 --- a/lua/cokeline/hlgroups.lua +++ b/lua/cokeline/hlgroups.lua @@ -31,6 +31,17 @@ Hlgroup.new = function(name, gui, guifg, guibg) return hlgroup end +---Using already existing highlight group, returns a new `Hlgroup` table. +---@param name string +---@return Hlgroup +Hlgroup.new_existing = function(name) + local hlgroup = { + name = name, + } + setmetatable(hlgroup, Hlgroup) + return hlgroup +end + ---Embeds some text in a highlight group. ---@param self Hlgroup ---@param text string From 5521763af83c2ae1937440cfd9d4efa06eb60e16 Mon Sep 17 00:00:00 2001 From: nenikitov Date: Fri, 30 Jun 2023 14:57:49 -0400 Subject: [PATCH 2/2] chore: renamed group name parameter and added documentation --- README.md | 3 +++ doc/cokeline.txt | 3 +++ lua/cokeline/components.lua | 8 ++++---- 3 files changed, 10 insertions(+), 4 deletions(-) diff --git a/README.md b/README.md index 58f5227..baf5f29 100644 --- a/README.md +++ b/README.md @@ -744,6 +744,9 @@ Every component passed to the `components` list has to be a table of the form: bg = '#rrggbb' | function(buffer) -> '#rrggbb', style = 'attr1,attr2,...' | function(buffer) -> 'attr1,attr2,...', + -- Or, alternatively, the name of the highlight group + highlight = 'string' | function(buffer) -> string | nil, + -- If `true` the buffer will be deleted when this component is -- left-clicked (usually used to implement close buttons, overrides `on_click`). -- deprecated, it is recommended to use the Buffer:delete() method in an on_click event diff --git a/doc/cokeline.txt b/doc/cokeline.txt index c151b25..589590c 100644 --- a/doc/cokeline.txt +++ b/doc/cokeline.txt @@ -258,6 +258,9 @@ Every component passed to the `components` list has to be a table of the form: bg = '#rrggbb' | function(buffer) -> '#rrggbb' | nil, style = 'string' | nil, + -- Or, alternatively, the name of the highlight group + highlight = 'string' | function(buffer) -> string | nil, + -- If `true` the buffer will be deleted when this component is -- left-clicked (useful to implement close buttons). delete_buffer_on_left_click = boolean, diff --git a/lua/cokeline/components.lua b/lua/cokeline/components.lua index e302cfe..2cc5784 100644 --- a/lua/cokeline/components.lua +++ b/lua/cokeline/components.lua @@ -14,7 +14,7 @@ local map = vim.tbl_map ---@field style string|fun(buffer:Buffer): string ---@field fg string|fun(buffer:Buffer): string ---@field bg string|fun(buffer:Buffer): string ----@field hl_group string|fun(buffer:Buffer): string +---@field highlight string|fun(buffer:Buffer): string ---@field on_click ClickHandler | nil ---@field on_mouse_enter MouseEnterHandler | nil ---@field on_mouse_leave MouseLeaveHandler | nil @@ -39,7 +39,7 @@ Component.new = function(c, i, default_hl) fg = c.fg or default_hl.fg or "NONE", bg = c.bg or default_hl.bg or "NONE", style = c.style or default_hl.style or "NONE", - hl_group = c.hl_group, + highlight = c.highlight, delete_buffer_on_left_click = c.delete_buffer_on_left_click or false, on_click = c.on_click, on_mouse_enter = c.on_mouse_enter, @@ -90,8 +90,8 @@ Component.render = function(self, context) or evaluate(_G.cokeline.config.default_hl.bg) or "NONE" - if component.hl_group then - component.hlgroup = Hlgroup.new_existing(evaluate(component.hl_group)) + if component.highlight then + component.hlgroup = Hlgroup.new_existing(evaluate(component.highlight)) else component.hlgroup = Hlgroup.new( ("Cokeline_%s_%s"):format(component.bufnr or context.kind, self.index),