From dd7fd96975726b8758c3fc2e2899e206a184a589 Mon Sep 17 00:00:00 2001 From: Scott McKendry <39483124+scottmckendry@users.noreply.github.com> Date: Sat, 18 May 2024 13:13:07 +1200 Subject: [PATCH] feat: add grapple window highlight groups (#164) * feat: add grapple window highlight groups * docs: update grapple highlights table --- README.md | 4 ++++ lua/grapple/window.lua | 22 ++++++++++++++++++++++ plugin/grapple.lua | 4 ++++ 3 files changed, 30 insertions(+) diff --git a/README.md b/README.md index 8807e38..14272d9 100644 --- a/README.md +++ b/README.md @@ -936,6 +936,10 @@ require("grapple").open_loaded({ all = true }) | `GrappleName` | `DiagnosticHint` | N/A | Tags window for tag name | | `GrappleNoExist` | `DiagnosticError` | N/A | Tags window for tag status | | `GrappleCurrent` | `SpecialChar` | `gui=bold` | All windows for current status | +| `GrappleFloat` | `NormalFloat` | N/A | All windows for background | +| `GrappleBorder` | `FloatBorder` | N/A | All windows for border | +| `GrappleTitle` | `FloatTitle` | N/A | All windows for title | +| `GrappleFooter` | `FloatFooter` | N/A | All windows for footer | ## Persistent State diff --git a/lua/grapple/window.lua b/lua/grapple/window.lua index 45889e9..51b525f 100644 --- a/lua/grapple/window.lua +++ b/lua/grapple/window.lua @@ -128,6 +128,12 @@ function Window:open() local win_opts = self:window_options() self.win_id = vim.api.nvim_open_win(self.buf_id, true, win_opts) + -- Set window highlights + self:set_highlight("NormalFloat", "GrappleNormal") + self:set_highlight("FloatBorder", "GrappleBorder") + self:set_highlight("FloatTitle", "GrappleTitle") + self:set_highlight("FloatFooter", "GrappleFooter") + -- Setup window to conceal line IDs vim.api.nvim_set_option_value("concealcursor", "nvic", { win = self.win_id }) vim.api.nvim_set_option_value("conceallevel", 3, { win = self.win_id }) @@ -607,4 +613,20 @@ function Window:alternate_path() return Path.fs_absolute(alt_name) end +--- Replaces a highlight group in the window +--- @param new_from string +--- @param new_to string +function Window:set_highlight(new_from, new_to) + local new_entry = new_from .. ":" .. new_to + local replace_pattern = string.format("(%s:[^,]*)", vim.pesc(new_from)) + local new_winhighlight, n_replace = vim.wo[self.win_id].winhighlight:gsub(replace_pattern, new_entry) + if n_replace == 0 then + new_winhighlight = new_winhighlight .. "," .. new_entry + end + + pcall(function() + vim.wo[self.win_id].winhighlight = new_winhighlight + end) +end + return Window diff --git a/plugin/grapple.lua b/plugin/grapple.lua index 1385a48..f2810eb 100644 --- a/plugin/grapple.lua +++ b/plugin/grapple.lua @@ -6,6 +6,10 @@ vim.cmd("highlight default GrappleBold gui=bold cterm=bold") vim.cmd("highlight default link GrappleHint Comment") vim.cmd("highlight default link GrappleName DiagnosticHint") vim.cmd("highlight default link GrappleNoExist DiagnosticError") +vim.cmd("highlight default link GrappleNormal NormalFloat") +vim.cmd("highlight default link GrappleBorder FloatBorder") +vim.cmd("highlight default link GrappleTitle FloatTitle") +vim.cmd("highlight default link GrappleFooter FloatFooter") vim.cmd("highlight default GrappleCurrent gui=bold cterm=bold") vim.cmd("highlight! link GrappleCurrent SpecialChar")