marleyvim/nvim/lua/plugins/util/mini-hipatterns.lua

86 lines
2.3 KiB
Lua
Raw Normal View History

2025-01-04 17:28:51 -08:00
return {
'mini.hipatterns',
event = { 'BufReadPost', 'BufWritePost', 'BufNewFile' },
after = function()
local hipatterns = require('mini.hipatterns')
-- Tailwind CSS support.
local hls = {}
local tailwindFts = {
'astro',
'css',
'heex',
'html',
'html-eex',
'javascript',
'javascriptreact',
'rust',
'svelte',
'typescript',
'typescriptreact',
'vue',
}
-- Reset hl groups when colorscheme changes.
vim.api.nvim_create_autocmd('ColorScheme', {
callback = function()
hls = {}
end,
})
hipatterns.setup({
highlighters = {
hex_color = hipatterns.gen_highlighter.hex_color({ priority = 2000 }),
shorthand = {
pattern = '()#%x%x%x()%f[^%x%w]',
group = function(_, _, data)
---@type string
local match = data.full_match
local r, g, b = match:sub(2, 2), match:sub(3, 3), match:sub(4, 4)
local hex_color = '#' .. r .. r .. g .. g .. b .. b
return MiniHipatterns.compute_hex_color_group(hex_color, 'bg')
end,
extmark_opts = { priority = 2000 },
},
tailwind = {
pattern = function()
if not vim.tbl_contains(tailwindFts, vim.bo.filetype) then
return
end
return '%f[%w:-]()[%w:-]+%-[a-z%-]+%-%d+()%f[^%w:-]'
end,
group = function(_, _, m)
---@type string
local match = m.full_match
---@type string, number?
local color, shade = match:match('[%w-]+%-([a-z%-]+)%-(%d+)')
shade = tonumber(shade)
local c = require('colors')
local bg = vim.tbl_get(c.tailwind, color, shade)
if bg then
local hl = 'MiniHipatternsTailwind' .. color .. shade
if not hls[hl] then
hls[hl] = true
local bg_shade = shade == 500 and 950
or shade < 500 and 900
or 100
local fg = vim.tbl_get(c.tailwind, color, bg_shade)
vim.api.nvim_set_hl(0, hl, { bg = '#' .. bg, fg = '#' .. fg })
end
return hl
end
end,
extmark_opts = { priority = 2000 },
},
},
})
end,
}