|
| 1 | +--- Enable live highlighting by `mini.hipatterns` |
| 2 | +--- Basically Copy/Paste (with some adjustments) from folke's tokyonight colorscheme: |
| 3 | +--- https://github.com/folke/tokyonight.nvim/blob/c645c1fe778e1428143beca1908b54b78b0b5c1a/.lazy.lua |
| 4 | + |
| 5 | +local util = require "astrotheme.lib.util" |
| 6 | + |
| 7 | +local colors ---@type AstroThemePalette |
| 8 | +local cache ---@type table<string,string>? |
| 9 | +local hl_groups = {} ---@type { [string]: boolean } |
| 10 | + |
| 11 | +---@param hl AstroThemeHighlight |
| 12 | +local function get_hl_group(hl) |
| 13 | + local group = "AstroThemeDev" .. vim.inspect(hl):gsub("%W+", "_") |
| 14 | + if not hl_groups[group] then |
| 15 | + hl = vim.deepcopy(hl, true) |
| 16 | + if not hl.fg then hl.fg = colors.syntax.text end |
| 17 | + vim.api.nvim_set_hl(0, group, hl) |
| 18 | + hl_groups[group] = true |
| 19 | + end |
| 20 | + return group |
| 21 | +end |
| 22 | + |
| 23 | +local function load_highlights() |
| 24 | + if cache then return end |
| 25 | + cache = {} |
| 26 | + local opts = require("astrotheme").config |
| 27 | + colors = util.set_palettes(opts) |
| 28 | + local highlights = util.get_highlights(colors, opts) |
| 29 | + for k, v in pairs(highlights) do |
| 30 | + cache[k] = get_hl_group(v) |
| 31 | + end |
| 32 | +end |
| 33 | + |
| 34 | +vim.api.nvim_create_autocmd("BufWritePost", { |
| 35 | + group = vim.api.nvim_create_augroup("astrotheme_dev", { clear = true }), |
| 36 | + pattern = "*/lua/astrotheme/**.lua", |
| 37 | + callback = vim.schedule_wrap(function() |
| 38 | + local opts = require("astrotheme").config |
| 39 | + for k in pairs(package.loaded) do |
| 40 | + if k:find "^astrotheme" then package.loaded[k] = nil end |
| 41 | + end |
| 42 | + require("astrotheme").setup(opts) |
| 43 | + if vim.g.colors_name then vim.cmd.colorscheme(vim.g.colors_name) end |
| 44 | + hl_groups, cache = {}, nil |
| 45 | + local hi = require "mini.hipatterns" |
| 46 | + vim.tbl_map(hi.update, hi.get_enabled_buffers()) |
| 47 | + end), |
| 48 | +}) |
| 49 | + |
| 50 | +return { |
| 51 | + "echasnovski/mini.hipatterns", |
| 52 | + opts = function(_, opts) |
| 53 | + opts.highlighters = opts.highlighters or {} |
| 54 | + opts.highlighters.astrotheme = { |
| 55 | + pattern = function(buf) |
| 56 | + local fname = vim.fs.normalize(vim.api.nvim_buf_get_name(buf or 0)) |
| 57 | + if fname:find "lua/astrotheme/groups" and vim.fn.fnamemodify(fname, ":t:r") ~= "init" then |
| 58 | + load_highlights() |
| 59 | + return '^%s*%[?"?()[%w%.@]+()"?%]?%s*=' |
| 60 | + end |
| 61 | + end, |
| 62 | + group = function(_, match) |
| 63 | + if cache then return cache[match] end |
| 64 | + end, |
| 65 | + extmark_opts = { priority = 2000 }, |
| 66 | + } |
| 67 | + |
| 68 | + opts.highlighters.astrotheme_colors = { |
| 69 | + pattern = { |
| 70 | + "%f[%w]()c%.[%w_%.]+()%f[%W]", |
| 71 | + "%f[%w]()colors%.[%w_%.]+()%f[%W]", |
| 72 | + "%f[%w]()vim%.g%.terminal_color_%d+()%f[%W]", |
| 73 | + }, |
| 74 | + group = function(_, match) |
| 75 | + local parts = vim.split(match, ".", { plain = true }) |
| 76 | + local t = _G --[[@as table]] |
| 77 | + if parts[1]:sub(1, 1) == "c" then |
| 78 | + table.remove(parts, 1) |
| 79 | + if not colors then colors = util.set_palettes(require("astrotheme").config) end |
| 80 | + t = colors |
| 81 | + end |
| 82 | + local color = vim.tbl_get(t, unpack(parts)) |
| 83 | + return type(color) == "string" and get_hl_group { fg = color } |
| 84 | + end, |
| 85 | + extmark_opts = function(_, _, data) |
| 86 | + return { |
| 87 | + virt_text = { { "⬤ ", data.hl_group } }, |
| 88 | + virt_text_pos = "inline", |
| 89 | + priority = 2000, |
| 90 | + } |
| 91 | + end, |
| 92 | + } |
| 93 | + end, |
| 94 | +} |
0 commit comments