@@ -5,8 +5,8 @@ local augroup = 'github-theme.interactive'
5
5
local augroup_live_colors = augroup .. ' .live_colors'
6
6
local M = {}
7
7
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 )
10
10
11
11
-- TODO: move to util module?
12
12
--- @param tbl table the target search in
@@ -70,57 +70,60 @@ function M.live_colors(enable)
70
70
local q1 = ts .query .parse (
71
71
' lua' ,
72
72
[[
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
+ )
83
84
]]
84
85
)
85
86
local q2 = ts .query .parse (
86
87
' lua' ,
87
88
[[
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
+ )
100
102
]]
101
103
)
102
104
103
105
local modquery = ts .query .parse (
104
106
' lua' ,
105
107
[[
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
+ )
118
125
)
119
126
)
120
- )
121
- )
122
- )
123
- )
124
127
]]
125
128
)
126
129
@@ -165,6 +168,7 @@ function M.live_colors(enable)
165
168
table.insert (stack , knode_text )
166
169
else
167
170
cb ({
171
+ field = assert (matched_node (match , cap2 .field )),
168
172
knode = knode ,
169
173
vnode = vnode ,
170
174
keypath = table.concat (stack , ' .' ) .. ' .' .. knode_text ,
@@ -178,7 +182,9 @@ function M.live_colors(enable)
178
182
for _pat , match , meta in modquery :iter_matches (node , src , nil , nil , { all = true }) do
179
183
local knode = assert (matched_node (match , cap3 .k ))
180
184
local vnode = assert (matched_node (match , cap3 .v ))
185
+
181
186
cb ({
187
+ field = assert (matched_node (match , cap3 .field )),
182
188
knode = knode ,
183
189
vnode = vnode ,
184
190
keypath = ts .get_node_text (knode , src , { metadata = meta [cap3 .k ] }),
@@ -211,14 +217,14 @@ function M.live_colors(enable)
211
217
212
218
local function disp_colors (buf , nodes , colors , typ , theme )
213
219
for _ , v in ipairs (nodes ) do
214
- local lnum , col = v .vnode :end_ ()
220
+ local lnum = v .vnode :end_ ()
215
221
local color = typ == ' mod' and ' ' or keypath_get (colors , v .keypath )
216
222
217
223
if type (color ) == ' string' then
218
224
local def , grp
219
225
220
226
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 {}
222
228
if (def .style or ' NONE' ) ~= ' NONE' then
223
229
for s in def .style :gmatch (' [^,]+' ) do
224
230
def [s ] = true
@@ -235,9 +241,8 @@ function M.live_colors(enable)
235
241
236
242
def .force = true
237
243
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 ()) , {
239
245
end_row = lnum ,
240
- end_col = 0 ,
241
246
strict = false ,
242
247
undo_restore = true ,
243
248
invalidate = true ,
@@ -246,7 +251,7 @@ function M.live_colors(enable)
246
251
end_right_gravity = true ,
247
252
hl_mode = ' combine' ,
248
253
virt_text_pos = ' inline' ,
249
- virt_text = { { ' ' }, { ' Example ' , grp } },
254
+ virt_text = { { ' Example ' , grp }, { ' ' } },
250
255
})
251
256
end
252
257
end
@@ -307,25 +312,25 @@ function M.live_colors(enable)
307
312
end
308
313
end
309
314
310
- api .nvim_create_augroup (augroup_live_colors , { clear = true })
311
-
312
315
if enable == false then
313
316
for _ , buf in ipairs (api .nvim_list_bufs ()) do
314
317
api .nvim_buf_clear_namespace (buf , ns , 0 , - 1 )
315
318
end
316
319
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
327
325
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
329
334
end
330
335
331
336
return M
0 commit comments