Skip to content

Commit 5edcaee

Browse files
authored
fix(autocmd-patterns): filter out default-colors (#45)
* test(option): add spec for `autocmd_patterns` option * refactor(composer): extract `ignored-definition?` * fix(autocmd-patterns): filter off default-colors * refactor(composer): reduce redundant arrow list * fix(autocmd-patterns): ignore empty autocmds after filtering * test(option): add spec for autocmd_patterns which generates nothing
1 parent cd785b5 commit 5edcaee

File tree

3 files changed

+132
-98
lines changed

3 files changed

+132
-98
lines changed

fnl/ex-colors/composer.fnl

Lines changed: 41 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,14 @@
1111

1212
(local default-colors (require :ex-colors.default-colors))
1313

14+
(fn ignored-definition? [hl-name hl-map]
15+
(let [ignore-default-colors? config.ignore_default_colors
16+
ignore-clear? config.ignore_clear]
17+
(or (and ignore-default-colors? ;
18+
(vim.deep_equal hl-map (. default-colors hl-name)))
19+
(and ignore-clear? ;
20+
(not (next hl-map))))))
21+
1422
(fn extend-sequence! [dst ...]
1523
"Extend `dst` sequence with any number of the following sequences.
1624
Any `nil`s are ignored.
@@ -82,31 +90,36 @@ corresponding options are enabled.
8290
hl-names
8391
(let [hl-maps (collect [_ hl-name (ipairs hl-names)]
8492
(remap-hl-opts hl-name))
85-
hi-cmds (doto (icollect [hl-name hl-opts (pairs hl-maps)]
86-
(when (next hl-opts)
87-
(.. indent
88-
(format-nvim-set-hl hl-name hl-opts))))
89-
(table.sort))
90-
;; Note: \n is unavailable due to the restriction of
91-
;; vim.api.nvim_buf_set_lines.
92-
callback-lines (flatten ["callback = function()"
93-
hi-cmds
94-
"end,"])
95-
au-opt-lines (if (= "*" au-pattern)
96-
callback-lines
97-
(let [pattern-line (: " pattern = %s,"
98-
:format
99-
(->oneliner au-pattern))]
100-
(flatten [pattern-line callback-lines])))
101-
[first-line &as lines] (vim.deepcopy autocmd-template-lines)
102-
event-arg (case (type au-event)
103-
:string (: "%q" :format au-event)
104-
:table au-event
105-
else (error (.. "expected string or table, got "
106-
else)))]
107-
(tset lines 1 (first-line:format event-arg))
108-
(table.insert lines (length lines) au-opt-lines)
109-
(table.insert autocmd-list (flatten lines))))))
93+
filtered-hl-maps (collect [hl-name hl-map (pairs hl-maps)]
94+
(when-not (ignored-definition? hl-name
95+
hl-map)
96+
(values hl-name hl-map)))]
97+
(when (next filtered-hl-maps)
98+
(let [hi-cmds (doto (icollect [hl-name hl-opts (pairs filtered-hl-maps)]
99+
(when (next hl-opts)
100+
(.. indent
101+
(format-nvim-set-hl hl-name hl-opts))))
102+
(table.sort))
103+
;; Note: \n is unavailable due to the restriction of
104+
;; vim.api.nvim_buf_set_lines.
105+
callback-lines (flatten ["callback = function()"
106+
hi-cmds
107+
"end,"])
108+
au-opt-lines (if (= "*" au-pattern)
109+
callback-lines
110+
(let [pattern-line (: " pattern = %s,"
111+
:format
112+
(->oneliner au-pattern))]
113+
(flatten [pattern-line callback-lines])))
114+
[first-line &as lines] (vim.deepcopy autocmd-template-lines)
115+
event-arg (case (type au-event)
116+
:string (: "%q" :format au-event)
117+
:table au-event
118+
else (error (.. "expected string or table, got "
119+
else)))]
120+
(tset lines 1 (first-line:format event-arg))
121+
(table.insert lines (length lines) au-opt-lines)
122+
(table.insert autocmd-list (flatten lines))))))))
110123
(doto autocmd-list
111124
(table.sort (fn [[cmd-line1] [cmd-line2]]
112125
;; Sort by the first arg of nvim_create_autocmd, i.e., by
@@ -117,15 +130,6 @@ corresponding options are enabled.
117130
(fn compose-hi-cmd-lines [highlights dump-all?]
118131
(let [included-patterns config.included_patterns
119132
included-hlgroups (filter-by-included-hlgroups highlights)
120-
ignore-default-colors? config.ignore_default_colors
121-
ignore-clear? config.ignore_clear
122-
ignored-definition? (fn [hl-name hl-map]
123-
(or (and ignore-default-colors? ;
124-
(vim.deep_equal hl-map
125-
(. default-colors
126-
hl-name)))
127-
(and ignore-clear? ;
128-
(not (next hl-map)))))
129133
filtered-hl-maps (if dump-all?
130134
(collect [_ hl-name (ipairs highlights)]
131135
(let [hl-map (vim.api.nvim_get_hl 0
@@ -136,10 +140,9 @@ corresponding options are enabled.
136140
(vim.list_extend included-hlgroups))
137141
hl-maps (collect [_ hl-name (ipairs filtered-highlights)]
138142
(remap-hl-opts hl-name))]
139-
(-> (collect [hl-name hl-map (pairs hl-maps)]
140-
(when-not (ignored-definition? hl-name
141-
hl-map)
142-
(values hl-name hl-map))))))
143+
(collect [hl-name hl-map (pairs hl-maps)]
144+
(when-not (ignored-definition? hl-name hl-map)
145+
(values hl-name hl-map)))))
143146
cmd-list (-> (icollect [hl-name hl-map (pairs filtered-hl-maps)]
144147
(format-nvim-set-hl hl-name hl-map))
145148
(flatten))]

lua/ex-colors/composer.lua

Lines changed: 78 additions & 60 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,11 @@ local filter_by_included_hlgroups = _local_2_["filter-by-included-hlgroups"]
88
local _local_3_ = require("ex-colors.remap")
99
local remap_hl_opts = _local_3_["remap-hl-opts"]
1010
local default_colors = require("ex-colors.default-colors")
11+
local function ignored_definition_3f(hl_name, hl_map)
12+
local ignore_default_colors_3f = config.ignore_default_colors
13+
local ignore_clear_3f = config.ignore_clear
14+
return ((ignore_default_colors_3f and vim.deep_equal(hl_map, default_colors[hl_name])) or (ignore_clear_3f and not next(hl_map)))
15+
end
1116
local function extend_sequence_21(dst, ...)
1217
for i, _3flist in pairs({...}) do
1318
assert(("number" == type(i)), ("expected number, got " .. i))
@@ -76,82 +81,95 @@ local function compose_autocmd_lines(highlights)
7681
end
7782
hl_maps = tbl_16_auto
7883
end
79-
local hi_cmds
84+
local filtered_hl_maps
8085
do
81-
local tmp_9_auto
86+
local tbl_16_auto = {}
87+
for hl_name, hl_map in pairs(hl_maps) do
88+
local k_17_auto, v_18_auto = nil, nil
89+
if not ignored_definition_3f(hl_name, hl_map) then
90+
k_17_auto, v_18_auto = hl_name, hl_map
91+
else
92+
k_17_auto, v_18_auto = nil
93+
end
94+
if ((k_17_auto ~= nil) and (v_18_auto ~= nil)) then
95+
tbl_16_auto[k_17_auto] = v_18_auto
96+
else
97+
end
98+
end
99+
filtered_hl_maps = tbl_16_auto
100+
end
101+
if next(filtered_hl_maps) then
102+
local hi_cmds
82103
do
83-
local tbl_21_auto = {}
84-
local i_22_auto = 0
85-
for hl_name, hl_opts in pairs(hl_maps) do
86-
local val_23_auto
87-
if next(hl_opts) then
88-
val_23_auto = (indent .. format_nvim_set_hl(hl_name, hl_opts))
89-
else
90-
val_23_auto = nil
91-
end
92-
if (nil ~= val_23_auto) then
93-
i_22_auto = (i_22_auto + 1)
94-
tbl_21_auto[i_22_auto] = val_23_auto
95-
else
104+
local tmp_9_auto
105+
do
106+
local tbl_21_auto = {}
107+
local i_22_auto = 0
108+
for hl_name, hl_opts in pairs(filtered_hl_maps) do
109+
local val_23_auto
110+
if next(hl_opts) then
111+
val_23_auto = (indent .. format_nvim_set_hl(hl_name, hl_opts))
112+
else
113+
val_23_auto = nil
114+
end
115+
if (nil ~= val_23_auto) then
116+
i_22_auto = (i_22_auto + 1)
117+
tbl_21_auto[i_22_auto] = val_23_auto
118+
else
119+
end
96120
end
121+
tmp_9_auto = tbl_21_auto
97122
end
98-
tmp_9_auto = tbl_21_auto
123+
table.sort(tmp_9_auto)
124+
hi_cmds = tmp_9_auto
99125
end
100-
table.sort(tmp_9_auto)
101-
hi_cmds = tmp_9_auto
102-
end
103-
local callback_lines = flatten({"callback = function()", hi_cmds, "end,"})
104-
local au_opt_lines
105-
if ("*" == au_pattern) then
106-
au_opt_lines = callback_lines
107-
else
108-
local pattern_line = (" pattern = %s,"):format(__3eoneliner(au_pattern))
109-
au_opt_lines = flatten({pattern_line, callback_lines})
110-
end
111-
local _let_14_ = vim.deepcopy(autocmd_template_lines)
112-
local first_line = _let_14_[1]
113-
local lines = _let_14_
114-
local event_arg
115-
do
116-
local _15_ = type(au_event)
117-
if (_15_ == "string") then
118-
event_arg = ("%q"):format(au_event)
119-
elseif (_15_ == "table") then
120-
event_arg = au_event
121-
elseif (nil ~= _15_) then
122-
local _else = _15_
123-
event_arg = error(("expected string or table, got " .. _else))
126+
local callback_lines = flatten({"callback = function()", hi_cmds, "end,"})
127+
local au_opt_lines
128+
if ("*" == au_pattern) then
129+
au_opt_lines = callback_lines
124130
else
125-
event_arg = nil
131+
local pattern_line = (" pattern = %s,"):format(__3eoneliner(au_pattern))
132+
au_opt_lines = flatten({pattern_line, callback_lines})
133+
end
134+
local _let_16_ = vim.deepcopy(autocmd_template_lines)
135+
local first_line = _let_16_[1]
136+
local lines = _let_16_
137+
local event_arg
138+
do
139+
local _17_ = type(au_event)
140+
if (_17_ == "string") then
141+
event_arg = ("%q"):format(au_event)
142+
elseif (_17_ == "table") then
143+
event_arg = au_event
144+
elseif (nil ~= _17_) then
145+
local _else = _17_
146+
event_arg = error(("expected string or table, got " .. _else))
147+
else
148+
event_arg = nil
149+
end
126150
end
151+
lines[1] = first_line:format(event_arg)
152+
table.insert(lines, #lines, au_opt_lines)
153+
table.insert(autocmd_list, flatten(lines))
154+
else
127155
end
128-
lines[1] = first_line:format(event_arg)
129-
table.insert(lines, #lines, au_opt_lines)
130-
table.insert(autocmd_list, flatten(lines))
131156
else
132157
end
133158
end
134159
end
135160
do
136-
local function _20_(_18_, _19_)
137-
local cmd_line1 = _18_[1]
138-
local cmd_line2 = _19_[1]
161+
local function _23_(_21_, _22_)
162+
local cmd_line1 = _21_[1]
163+
local cmd_line2 = _22_[1]
139164
return (cmd_line1 < cmd_line2)
140165
end
141-
table.sort(autocmd_list, _20_)
166+
table.sort(autocmd_list, _23_)
142167
end
143168
return flatten(autocmd_list)
144169
end
145170
local function compose_hi_cmd_lines(highlights, dump_all_3f)
146171
local included_patterns = config.included_patterns
147172
local included_hlgroups = filter_by_included_hlgroups(highlights)
148-
local ignore_default_colors_3f = config.ignore_default_colors
149-
local ignore_clear_3f = config.ignore_clear
150-
local ignored_definition_3f
151-
local function _21_(hl_name, hl_map)
152-
return ((ignore_default_colors_3f and vim.deep_equal(hl_map, default_colors[hl_name])) or (ignore_clear_3f and not next(hl_map)))
153-
end
154-
ignored_definition_3f = _21_
155173
local filtered_hl_maps
156174
if dump_all_3f then
157175
local tbl_16_auto = {}
@@ -197,7 +215,7 @@ local function compose_hi_cmd_lines(highlights, dump_all_3f)
197215
filtered_hl_maps = tbl_16_auto
198216
end
199217
local cmd_list
200-
local function _27_()
218+
local function _29_()
201219
local tbl_21_auto = {}
202220
local i_22_auto = 0
203221
for hl_name, hl_map in pairs(filtered_hl_maps) do
@@ -210,7 +228,7 @@ local function compose_hi_cmd_lines(highlights, dump_all_3f)
210228
end
211229
return tbl_21_auto
212230
end
213-
cmd_list = flatten(_27_())
231+
cmd_list = flatten(_29_())
214232
table.sort(cmd_list)
215233
return cmd_list
216234
end
@@ -263,9 +281,9 @@ local function compose_vim_options_cmd_lines()
263281
for _, vim_option_name in ipairs(vim_options) do
264282
local k_17_auto, v_18_auto = nil, nil
265283
do
266-
local _33_ = vim.api.nvim_get_option_value(vim_option_name, {scope = "global"})
267-
if (nil ~= _33_) then
268-
local val = _33_
284+
local _35_ = vim.api.nvim_get_option_value(vim_option_name, {scope = "global"})
285+
if (nil ~= _35_) then
286+
local val = _35_
269287
if (vim.api.nvim_get_option_info2(vim_option_name, {}).default ~= val) then
270288
k_17_auto, v_18_auto = vim_option_name, val
271289
else

test/option_spec.fnl

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -118,6 +118,19 @@
118118
(vim.api.nvim_set_hl 0 :String {})
119119
(vim.cmd :ExColors)
120120
(assert/buf-contains-pattern (.. "vim%.api%.nvim_set_hl%(.-"))))))
121+
(describe* "autocmd_patterns"
122+
(it* "will generate autocmd"
123+
(clean-setup! {})
124+
(vim.cmd "ExColors | update")
125+
(assert/buf-contains-no-pattern "vim%.api%.nvim_create_autocmd%(.-")
126+
(clean-setup! {:autocmd_patterns {:FileType {:markdown ["^String$"]}}})
127+
(vim.cmd "ExColors | update")
128+
(assert/buf-contains-pattern "vim%.api%.nvim_create_autocmd%(.-"))
129+
(it* "will not generate any autocmds if the matched highlight map is same as default"
130+
(vim.api.nvim_set_hl 0 "@markup.italic" {:italic true})
131+
(clean-setup! {:autocmd_patterns {:FileType {:markdown ["^@markup.italic$"]}}})
132+
(vim.cmd "ExColors | update")
133+
(assert/buf-contains-pattern "vim%.api%.nvim_create_autocmd%(.-")))
121134
(describe* :omit_default
122135
(describe* "discards default field in output;"
123136
(describe* "thus, when <new-hl-name> is {fg='Red',default=true}"

0 commit comments

Comments
 (0)