Skip to content

Commit 1b6ee72

Browse files
committed
feat!: remove dev mode and move to automatic dev detection using mini.hipatterns
1 parent d83b89c commit 1b6ee72

File tree

4 files changed

+97
-32
lines changed

4 files changed

+97
-32
lines changed

.lazy.lua

Lines changed: 94 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,94 @@
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+
}

lua/astrotheme/init.lua

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -39,11 +39,6 @@ end
3939

4040
--- Set up AstroTheme with provided user configured options
4141
---@param opts AstroThemeOpts
42-
function M.setup(opts)
43-
M.config = require("astrotheme.lib.config").user_config(opts)
44-
M.config.plugins = util.get_plugin_list(M.config)
45-
46-
if opts.dev then util.live_reload() end
47-
end
42+
function M.setup(opts) M.config = require("astrotheme.lib.config").user_config(opts) end
4843

4944
return M

lua/astrotheme/lib/config.lua

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,6 @@ M.default = {
1313
palette = "astrodark",
1414
termguicolors = true,
1515
terminal_colors = true,
16-
dev = false,
1716
style = {
1817
transparent = false,
1918
inactive = true,

lua/astrotheme/lib/util.lua

Lines changed: 2 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,6 @@
88
---@class astrotheme.lib.util
99
local M = {}
1010

11-
M.live_reloading = false
12-
1311
--- Reload a given theme
1412
---@param opts AstroThemeOpts
1513
---@param theme string
@@ -63,7 +61,6 @@ end
6361
---@param module string
6462
---@return AstroThemeHighlights?
6563
function M.get_module_highlights(colors, opts, module)
66-
if M.live_reloading then package.loaded[module] = nil end
6764
local file_avail, file = pcall(require, module)
6865
if type(file) == "function" then file = file(colors, opts.style) end
6966
if file_avail then
@@ -87,8 +84,8 @@ function M.get_highlights(colors, opts)
8784
local module_highlights = M.get_module_highlights(colors, opts, "astrotheme.groups." .. base)
8885
if module_highlights then highlights = vim.tbl_deep_extend("force", module_highlights, highlights) end
8986
end
90-
for _, base in ipairs(opts.plugins) do
91-
local module_highlights = M.get_module_highlights(colors, opts, "astrotheme.groups.plugins." .. base)
87+
for _, plugin in ipairs(M.get_plugin_list(opts)) do
88+
local module_highlights = M.get_module_highlights(colors, opts, "astrotheme.groups.plugins." .. plugin)
9289
if module_highlights then highlights = vim.tbl_deep_extend("force", module_highlights, highlights) end
9390
end
9491

@@ -107,7 +104,6 @@ end
107104
---@return AstroThemePalette
108105
function M.set_palettes(opts)
109106
local palette_name = "astrotheme.palettes." .. opts.palette
110-
if M.live_reloading then package.loaded[palette_name] = nil end
111107
local palette = require(palette_name)
112108
palette = vim.tbl_deep_extend("force", palette, opts.palettes.global)
113109
palette = vim.tbl_deep_extend("force", palette, opts.palettes[opts.palette])
@@ -124,25 +120,6 @@ function M.set_highlights(highlights)
124120
end
125121
end
126122

127-
--- Enable live reloading of AstroTheme for development
128-
function M.live_reload()
129-
vim.api.nvim_create_autocmd("BufWritePost", {
130-
pattern = "*/lua/astrotheme/**.lua",
131-
group = vim.api.nvim_create_augroup("AstroThemeDev", { clear = true }),
132-
callback = function()
133-
local theme = vim.g.colors_name
134-
if string.match(theme, "astro") then vim.cmd.colorscheme(theme) end
135-
end,
136-
})
137-
M.live_reloading = true
138-
end
139-
140-
--- Disable live reloading of AstroTheme
141-
function M.live_reload_stop()
142-
vim.api.nvim_del_augroup_by_name "AstroThemeDev"
143-
M.live_reloading = false
144-
end
145-
146123
--- Set terminal colors based on the currently loaded colors
147124
function M.set_terminal_colors(colors)
148125
vim.g.terminal_color_0 = colors.term.black

0 commit comments

Comments
 (0)