Skip to content

Commit 80b7f44

Browse files
committed
refactor(interactive): live_colors() refinements
This commit refactors the `live_colors()` function in `interactive.lua`: - Better handling (more thorough) when toggling on/off - Reformat treesitter queries for better readability (indentation) - Change placement of 'Example' text in the buffer to come before fields - Improve handling of `augroups` (delete them when toggling off)
1 parent f610b73 commit 80b7f44

File tree

1 file changed

+63
-58
lines changed

1 file changed

+63
-58
lines changed

lua/github-theme/interactive.lua

+63-58
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,8 @@ local augroup = 'github-theme.interactive'
55
local augroup_live_colors = augroup .. '.live_colors'
66
local M = {}
77

8-
api.nvim_create_augroup(augroup, { clear = true })
9-
api.nvim_create_augroup(augroup_live_colors, { clear = true })
8+
pcall(api.nvim_del_augroup_by_name, augroup)
9+
pcall(api.nvim_del_augroup_by_name, augroup_live_colors)
1010

1111
-- TODO: move to util module?
1212
---@param tbl table the target search in
@@ -70,57 +70,60 @@ function M.live_colors(enable)
7070
local q1 = ts.query.parse(
7171
'lua',
7272
[[
73-
(assignment_statement
74-
.
75-
(variable_list) @k (#vim-match? @k "\\m^\\%(palette\\|pal\\|spec\\)\\%(\\.\\w\\+\\)*$")
76-
.
77-
"="
78-
.
79-
(expression_list
80-
(table_constructor) @defs
81-
)
82-
)
73+
; query
74+
(assignment_statement
75+
.
76+
(variable_list) @k (#vim-match? @k "\\m^\\%(palette\\|pal\\|spec\\)\\%(\\.\\w\\+\\)*$")
77+
.
78+
"="
79+
.
80+
(expression_list
81+
(table_constructor) @defs
82+
)
83+
)
8384
]]
8485
)
8586
local q2 = ts.query.parse(
8687
'lua',
8788
[[
88-
(field
89-
name: [
90-
(_ content: (_) @k)
91-
(_ !content) @k
92-
]
93-
value: (_) @v
94-
)
95-
96-
(table_constructor
97-
"}" @tbl_end
98-
.
99-
)
89+
; query
90+
(field
91+
name: [
92+
(_ content: (_) @k)
93+
(_ !content) @k
94+
]
95+
value: (_) @v
96+
) @field
97+
98+
(table_constructor
99+
"}" @tbl_end
100+
.
101+
)
100102
]]
101103
)
102104

103105
local modquery = ts.query.parse(
104106
'lua',
105107
[[
106-
(function_declaration
107-
name: (_) @fname (#match? @fname "^M[.:]get$")
108-
body: (_
109-
(return_statement
110-
(expression_list
111-
(table_constructor
112-
(field
113-
name: [
114-
(_ content: (_) @k)
115-
(_ !content) @k
116-
]
117-
value: (table_constructor) @v
108+
; query
109+
(function_declaration
110+
name: (_) @fname (#match? @fname "^M[.:]get$")
111+
body: (_
112+
(return_statement
113+
(expression_list
114+
(table_constructor
115+
(field
116+
name: [
117+
(_ content: (_) @k)
118+
(_ !content) @k
119+
]
120+
value: (table_constructor) @v
121+
) @field
122+
)
123+
)
124+
)
118125
)
119126
)
120-
)
121-
)
122-
)
123-
)
124127
]]
125128
)
126129

@@ -165,6 +168,7 @@ function M.live_colors(enable)
165168
table.insert(stack, knode_text)
166169
else
167170
cb({
171+
field = assert(matched_node(match, cap2.field)),
168172
knode = knode,
169173
vnode = vnode,
170174
keypath = table.concat(stack, '.') .. '.' .. knode_text,
@@ -178,7 +182,9 @@ function M.live_colors(enable)
178182
for _pat, match, meta in modquery:iter_matches(node, src, nil, nil, { all = true }) do
179183
local knode = assert(matched_node(match, cap3.k))
180184
local vnode = assert(matched_node(match, cap3.v))
185+
181186
cb({
187+
field = assert(matched_node(match, cap3.field)),
182188
knode = knode,
183189
vnode = vnode,
184190
keypath = ts.get_node_text(knode, src, { metadata = meta[cap3.k] }),
@@ -211,14 +217,14 @@ function M.live_colors(enable)
211217

212218
local function disp_colors(buf, nodes, colors, typ, theme)
213219
for _, v in ipairs(nodes) do
214-
local lnum, col = v.vnode:end_()
220+
local lnum = v.vnode:end_()
215221
local color = typ == 'mod' and '' or keypath_get(colors, v.keypath)
216222

217223
if type(color) == 'string' then
218224
local def, grp
219225

220226
if typ == 'mod' then
221-
grp, def = 'github.' .. v.keypath, vim.deepcopy(colors[v.keypath], true)
227+
grp, def = 'github.' .. v.keypath, vim.deepcopy(colors[v.keypath], true) or {}
222228
if (def.style or 'NONE') ~= 'NONE' then
223229
for s in def.style:gmatch('[^,]+') do
224230
def[s] = true
@@ -235,9 +241,8 @@ function M.live_colors(enable)
235241

236242
def.force = true
237243
api.nvim_set_hl(0, grp, def)
238-
api.nvim_buf_set_extmark(buf, ns, lnum, 0, {
244+
api.nvim_buf_set_extmark(buf, ns, lnum, select(2, v.field:start()), {
239245
end_row = lnum,
240-
end_col = 0,
241246
strict = false,
242247
undo_restore = true,
243248
invalidate = true,
@@ -246,7 +251,7 @@ function M.live_colors(enable)
246251
end_right_gravity = true,
247252
hl_mode = 'combine',
248253
virt_text_pos = 'inline',
249-
virt_text = { { ' ' }, { ' Example ', grp } },
254+
virt_text = { { ' Example ', grp }, { ' ' } },
250255
})
251256
end
252257
end
@@ -307,25 +312,25 @@ function M.live_colors(enable)
307312
end
308313
end
309314

310-
api.nvim_create_augroup(augroup_live_colors, { clear = true })
311-
312315
if enable == false then
313316
for _, buf in ipairs(api.nvim_list_bufs()) do
314317
api.nvim_buf_clear_namespace(buf, ns, 0, -1)
315318
end
316319

317-
return
318-
end
319-
320-
api.nvim_create_autocmd({ 'ColorScheme', 'BufNew', 'BufWritePost' }, {
321-
group = augroup_live_colors,
322-
pattern = '*.lua',
323-
desc = 'Refresh live-displayed colors',
324-
nested = true,
325-
callback = vim.schedule_wrap(refresh),
326-
})
320+
pcall(api.nvim_del_augroup_by_name, augroup_live_colors)
321+
else
322+
for _, buf in ipairs(api.nvim_list_bufs()) do
323+
refresh({ buf = buf })
324+
end
327325

328-
refresh()
326+
api.nvim_create_autocmd({ 'ColorScheme', 'BufNew', 'BufWritePost' }, {
327+
group = api.nvim_create_augroup(augroup_live_colors, { clear = true }),
328+
pattern = '*.lua',
329+
desc = 'Refresh live-displayed colors',
330+
nested = true,
331+
callback = vim.schedule_wrap(refresh),
332+
})
333+
end
329334
end
330335

331336
return M

0 commit comments

Comments
 (0)