feat(hipatterns): made tailwind ft and style configurable

This commit is contained in:
Folke Lemaitre
2023-05-26 16:42:07 +02:00
parent b7a925904e
commit f7a63042f9

View File

@ -10,15 +10,25 @@ M.plugin = {
local hi = require("mini.hipatterns")
return {
-- custom LazyVim option to enable the tailwind integration
tailwind = true,
tailwind = {
enabled = true,
ft = { "typescriptreact", "javascriptreact", "css", "javascript", "typescript", "html" },
-- full: the whole css class will be highlighted
-- compact: only the color will be highlighted
style = "full",
},
highlighters = {
hex_color = hi.gen_highlighter.hex_color({ priority = 2000 }),
},
}
end,
config = function(_, opts)
if opts.tailwind then
local tailwind_ft = { "typescriptreact", "javascriptreact", "css", "javascript", "typescript", "html" }
-- backward compatibility
if opts.tailwind == true then
opts.tailwind =
{ enabled = true, ft = { "typescriptreact", "javascriptreact", "css", "javascript", "typescript", "html" } }
end
if type(opts.tailwind) == "table" and opts.tailwind.enabled then
-- reset hl groups when colorscheme changes
vim.api.nvim_create_autocmd("ColorScheme", {
callback = function()
@ -27,9 +37,18 @@ M.plugin = {
})
opts.highlighters.tailwind = {
pattern = function()
return vim.tbl_contains(tailwind_ft, vim.bo.filetype) and "%f[%w:-]()[%w:-]+%-[a-z%-]+%-%d+()%f[^%w:-]"
if not vim.tbl_contains(opts.tailwind.ft, vim.bo.filetype) then
return
end
if opts.tailwind.style == "full" then
return "%f[%w:-]()[%w:-]+%-[a-z%-]+%-%d+()%f[^%w:-]"
elseif opts.tailwind.style == "compact" then
return "%f[%w:-][%w:-]+%-()[a-z%-]+%-%d+()%f[^%w:-]"
end
end,
group = function(_, match)
group = function(_, _, m)
---@type string
local match = m.full_match
---@type string, number
local color, shade = match:match("[%w-]+%-([a-z%-]+)%-(%d+)")
shade = assert(tonumber(shade))