Skip to content

Commit

Permalink
Use fg as bg for foreground-only highlight groups (#22)
Browse files Browse the repository at this point in the history
Also drop support for v0.8.0.
  • Loading branch information
MisanthropicBit authored Nov 28, 2024
1 parent 09452dd commit 463f35b
Show file tree
Hide file tree
Showing 9 changed files with 108 additions and 27 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/style.yml
Original file line number Diff line number Diff line change
Expand Up @@ -15,4 +15,4 @@ jobs:
with:
token: ${{ secrets.GITHUB_TOKEN }}
version: 0.16.1
args: --check lua/ tests/
args: --check lua/ tests/ syntax/
2 changes: 1 addition & 1 deletion .github/workflows/tests.yml
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ jobs:
strategy:
fail-fast: false
matrix:
neovim_version: ['v0.8.0', 'v0.9.0', 'v0.10.0', 'nightly']
neovim_version: ['v0.9.0', 'v0.9.5', 'v0.10.0', 'nightly']

steps:
- uses: actions/checkout@v3
Expand Down
7 changes: 6 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ https://github.com/user-attachments/assets/417023dd-9d5d-4ae9-891d-514e0f3038d5

## Requirements

* Neovim 0.8.0+
* Neovim 0.9.0+

## Installing

Expand All @@ -63,6 +63,11 @@ If you are content with the defaults that are shown below, you don't need to
call the `configure` function. No default keymaps are set other than those
active during modes.

> [!NOTE]
> For mode highlight groups that only have a foreground color, `winmove` will
> automatically use the foreground color as a background color for the given
> mode so you do not have to create a custom highlight group yourself.
```lua
require('winmove').configure({
keymaps = {
Expand Down
13 changes: 9 additions & 4 deletions lua/winmove/compat.lua
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,15 @@ function compat.has(value)
return vim.fn.has(value) == 1
end

if compat.has("nvim-0.9.0") then
compat.print = vim.print
else
compat.print = vim.pretty_print
---@param ns_id integer
---@param opts vim.api.keyset.get_highlight
---@return vim.api.keyset.hl_info
function compat.get_hl(ns_id, opts)
if opts.create ~= nil and not compat.has("nvim-0.10.0") then
opts.create = nil
end

return vim.api.nvim_get_hl(ns_id, opts)
end

return compat
2 changes: 1 addition & 1 deletion lua/winmove/health.lua
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ local health = {}
local compat = require("winmove.compat")
local config = require("winmove.config")

local min_neovim_version = "0.8.0"
local min_neovim_version = "0.9.0"

local report_start, report_ok, report_error

Expand Down
47 changes: 42 additions & 5 deletions lua/winmove/highlight.lua
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,16 @@

local highlight = {}

---@alias winmove.Highlight string

local compat = require("winmove.compat")
local config = require("winmove.config")
local str = require("winmove.util.str")

local api = vim.api

---@alias winmove.Highlight string

local global_ns_id = 0

-- Window higlights per mode
local win_highlights = {
move = nil,
Expand Down Expand Up @@ -39,18 +42,52 @@ local highlight_groups = {
"SignColumn",
}

--- If the highlight group only contains a foreground color, return it as
--- the color to use for the background, otherwise use the background color
---@param group string
---@return boolean
---@return table<string, unknown>
local function ensure_background_color(group)
local colors = compat.get_hl(global_ns_id, { name = group, link = false, create = false })

if colors.bg or colors.ctermbg then
return true, colors
end

colors.bg = colors.fg
colors.fg = nil

---@diagnostic disable-next-line: inject-field
colors.ctermbg = colors.ctermfg
---@diagnostic disable-next-line: inject-field
colors.ctermfg = nil

return false, colors
end

--- Generate group highlights for a mode
---@param mode winmove.Mode
---@param groups string[]
local function generate_highlights(mode, groups)
local highlights = {}
local color = config.modes[mode].highlight
local hl_group = config.modes[mode].highlight
local titlecase_mode = str.titlecase(mode)
local has_bg, colors = ensure_background_color(hl_group)

if not has_bg then
-- Create a new highlight group we can link to
hl_group = ("Winmove%sInternal%s"):format(titlecase_mode, hl_group)

-- nvim_get_hl creates the highlight group if it does not exist on <= v0.9.0
if not compat.has("nvim-0.10.0") or vim.fn.hlexists(hl_group) == 0 then
vim.api.nvim_set_hl(global_ns_id, hl_group, colors)
end
end

for _, group in ipairs(groups) do
local titlecase_mode = str.titlecase(mode)
local winmove_group = "Winmove" .. titlecase_mode .. group

vim.cmd(("hi default link %s %s"):format(winmove_group, color))
vim.cmd(("hi default link %s %s"):format(winmove_group, hl_group))
table.insert(highlights, ("%s:%s"):format(group, winmove_group))
end

Expand Down
6 changes: 1 addition & 5 deletions lua/winmove/winutil.lua
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
local winutil = {}

local compat = require("winmove.compat")
local message = require("winmove.message")

local events = {
Expand All @@ -9,16 +8,13 @@ local events = {
"WinNew",
"WinScrolled",
"WinClosed",
"WinResized",
"BufWinEnter",
"BufWinLeave",
"BufEnter",
"BufLeave",
}

if compat.has("nvim-0.8.2") then
table.insert(events, "WinResized")
end

function winutil.get_ignored_events()
return events
end
Expand Down
15 changes: 6 additions & 9 deletions tests/custom_highlights_spec.lua
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
local winmove = require("winmove")
local compat = require("winmove.compat")
local config = require("winmove.config")
local highlight = require("winmove.highlight")
local vader = require("winmove.util.vader")
Expand All @@ -9,11 +8,6 @@ local given = vader.given
local make_layout = test_helpers.make_layout

describe("custom highlights", function()
if not compat.has("nvim-0.9.0") then
pending("Skipped for versions below 0.9.0")
return
end

local function get_expected_winhighlight(prefix)
local template = {
"CursorLine:%sCursorLine",
Expand All @@ -40,7 +34,8 @@ describe("custom highlights", function()
end

it("uses a custom highlight for move mode", function()
vim.cmd(("hi link %s %s"):format("CustomWinmoveMoveMode", "Title"))
vim.cmd.colorscheme("desert")
vim.cmd(("hi link %s %s"):format("CustomWinmoveMoveMode", "Todo"))

---@diagnostic disable-next-line: missing-fields
config.configure({
Expand Down Expand Up @@ -82,7 +77,8 @@ describe("custom highlights", function()
end)

it("uses a custom highlight for swap mode", function()
vim.cmd(("hi link %s %s"):format("CustomWinmoveSwapMode", "Repeat"))
vim.cmd.colorscheme("desert")
vim.cmd(("hi link %s %s"):format("CustomWinmoveSwapMode", "Todo"))

---@diagnostic disable-next-line: missing-fields
config.configure({
Expand Down Expand Up @@ -124,7 +120,8 @@ describe("custom highlights", function()
end)

it("uses a custom highlight for resize mode", function()
vim.cmd(("hi link %s %s"):format("CustomWinmoveResizeMode", "Repeat"))
vim.cmd.colorscheme("desert")
vim.cmd(("hi link %s %s"):format("CustomWinmoveResizeMode", "Todo"))

config.configure({
modes = {
Expand Down
41 changes: 41 additions & 0 deletions tests/highlight_spec.lua
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
local winmove = require("winmove")
local compat = require("winmove.compat")
local config = require("winmove.config")

describe("highlight", function()
it("generates internal highlight groups for foreground-only highlights", function()
vim.cmd.colorscheme("desert")

---@diagnostic disable-next-line: missing-fields
config.configure({
modes = {
move = {
highlight = "Type",
},
},
})

vim.cmd.vnew()

local hl_group = "WinmoveMoveInternalType"
local opts = { name = hl_group }

if compat.has("nvim-0.10.0") then
opts.create = false
end

assert.are.same(#vim.api.nvim_get_hl(0, opts), 0)

winmove.start_mode(winmove.Mode.Move)
winmove.stop_mode()

assert.are.same(vim.api.nvim_get_hl(0, opts), {
bold = true,
cterm = {
bold = true,
},
bg = 12433259,
ctermbg = 143,
})
end)
end)

0 comments on commit 463f35b

Please sign in to comment.