-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathhighlight.lua
104 lines (82 loc) · 2.41 KB
/
highlight.lua
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
require('user_api.types.user.highlight')
local ERROR = vim.log.levels.ERROR
---@type User.Hl
---@diagnostic disable-next-line:missing-fields
local M = {}
---@param name string
---@param opts vim.api.keyset.highlight
---@param bufnr? integer
function M.hl(name, opts, bufnr)
local Value = require('user_api.check.value')
local is_str = Value.is_str
local is_tbl = Value.is_tbl
local is_int = Value.is_int
local empty = Value.empty
if not (is_str(name) and is_tbl(opts)) or empty(name) then
vim.notify('(user_api.highlight.hl): Bad arguments', ERROR)
return
end
bufnr = is_int(bufnr) and bufnr or 0
vim.api.nvim_set_hl(bufnr, name, opts)
end
---@param A HlPair[]
function M.hl_from_arr(A)
local Value = require('user_api.check.value')
local is_str = Value.is_str
local is_tbl = Value.is_tbl
local empty = Value.empty
if not is_tbl(A) or empty(A) then
vim.notify('(user_api.highlight.hl_from_arr): Bad argument', ERROR)
return
end
for _, t in next, A do
if not (is_str(t.name) and is_tbl(t.opts)) or empty(t.name) then
vim.notify(
'(user_api.highlight.hl_from_arr): A highlight value is not permitted, skipping',
ERROR
)
goto continue
end
M.hl(t.name, t.opts)
::continue::
end
end
--- Set hl groups based on a dict input
---
--- Example of a valid table:
--- ```lua
--- local T = { ['HlGroup'] = { fg = '...', ... }, ['HlGroupAlt'] = { ... } }
--- ```
---
--- Which translates into VimScript as:
---
--- ```vim
--- hi HlGroup ctermfg=... ...
--- hi HlGroupAlt ...
--- ```
--- ---
--- See more at `:h nvim_set_hl`
---@param D HlDict
function M.hl_from_dict(D)
local Value = require('user_api.check.value')
local is_str = Value.is_str
local is_tbl = Value.is_tbl
local empty = Value.empty
if not is_tbl(D) or empty(D) then
vim.notify('(user_api.highlight.hl_from_dict): Unable to parse argument', ERROR)
return
end
for k, v in next, D do
if not (is_str(k) and is_tbl(v)) or empty(k) then
vim.notify(
'(user_api.highlight.hl_from_dict): A highlight value is not permitted, skipping',
ERROR
)
goto continue
end
M.hl(k, v)
::continue::
end
end
return M
--- vim:ts=4:sts=4:sw=4:et:ai:si:sta:noci:nopi: