-
-
Notifications
You must be signed in to change notification settings - Fork 29
/
init.lua
236 lines (206 loc) · 5.28 KB
/
init.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
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
local utils = require("nvim-highlight-colors.utils")
local table_utils = require("nvim-highlight-colors.table_utils")
local buffer_utils = require("nvim-highlight-colors.buffer_utils")
local colors = require("nvim-highlight-colors.color.utils")
local color_patterns = require("nvim-highlight-colors.color.patterns")
local ns_id = vim.api.nvim_create_namespace("nvim-highlight-colors")
if vim.g.loaded_nvim_highlight_colors ~= nil then
return {}
end
vim.g.loaded_nvim_highlight_colors = 1
local render_options = utils.render_options
local row_offset = 2
local is_loaded = false
local options = {
render = render_options.background,
enable_hex = true,
enable_rgb = true,
enable_hsl = true,
enable_var_usage = true,
enable_named_colors = true,
enable_short_hex = true,
enable_tailwind = false,
custom_colors = nil,
virtual_symbol = "■",
virtual_symbol_prefix = "",
virtual_symbol_suffix = " ",
virtual_symbol_position = "inline",
exclude_filetypes = {},
exclude_buftypes = {}
}
local M = {}
function M.setup(user_options)
is_loaded = true
if (user_options ~= nil and user_options ~= {}) then
for key, _ in pairs(user_options) do
if user_options[key] ~= nil then
options[key] = user_options[key]
end
end
end
end
function M.highlight_colors(min_row, max_row, active_buffer_id)
local patterns = {}
local patterns_config = {
HEX = {
is_enabled = options.enable_hex,
patterns = {
color_patterns.hex_regex,
color_patterns.hex_0x_regex
},
},
RGB = {
is_enabled = options.enable_rgb,
patterns = { color_patterns.rgb_regex },
},
HSL = {
is_enabled = options.enable_hsl,
patterns = { color_patterns.hsl_regex },
},
VAR_USAGE = {
is_enabled = options.enable_var_usage,
patterns = { color_patterns.var_usage_regex }
},
NAMED_COLORS = {
is_enabled = options.enable_named_colors,
patterns = { colors.get_css_named_color_pattern() }
},
TAILWIND = {
is_enabled = options.enable_tailwind and not utils.has_tailwind_css_lsp(),
patterns = { colors.get_tailwind_named_color_pattern() }
}
}
for _, config in pairs(patterns_config) do
if config.is_enabled then
for _, pattern in pairs(config.patterns) do
table.insert(patterns, pattern)
end
end
end
if (options.custom_colors ~= nil) then
for _, custom_color in pairs(options.custom_colors) do
table.insert(patterns, custom_color.label)
end
end
local positions = buffer_utils.get_positions_by_regex(
patterns,
min_row - 1,
max_row,
active_buffer_id,
row_offset
)
for _, data in pairs(positions) do
utils.create_highlight(
active_buffer_id,
ns_id,
data,
options
)
end
utils.highlight_with_lsp(active_buffer_id, ns_id, positions, options)
end
function M.refresh_highlights(active_buffer_id, should_clear_highlights)
local buffer_id = active_buffer_id ~= nil and active_buffer_id or 0
if not vim.api.nvim_buf_is_valid(active_buffer_id)
or vim.bo[buffer_id].buftype == "terminal"
or vim.tbl_contains(options.exclude_filetypes, vim.bo[buffer_id].filetype)
or vim.tbl_contains(options.exclude_buftypes, vim.bo[buffer_id].buftype)
then
return
end
if should_clear_highlights then
M.clear_highlights(buffer_id)
end
local visible_rows = utils.get_visible_rows_by_buffer_id(buffer_id)
local min_row = visible_rows[1]
local max_row = visible_rows[2]
M.highlight_colors(min_row, max_row, buffer_id)
end
function M.turn_on()
local buffers = vim.fn.getbufinfo({ buflisted = true })
for _, buffer in ipairs(buffers) do
M.refresh_highlights(buffer.bufnr)
end
is_loaded = true
end
function M.turn_off()
local buffers = vim.fn.getbufinfo({ buflisted = true })
for _, buffer in ipairs(buffers) do
M.clear_highlights(buffer.bufnr)
end
is_loaded = false
end
function M.clear_highlights(active_buffer_id)
pcall(
function ()
local buffer_id = active_buffer_id ~= nil and active_buffer_id or 0
vim.api.nvim_buf_clear_namespace(buffer_id, ns_id, 0, utils.get_last_row_index())
local virtual_texts = vim.api.nvim_buf_get_extmarks(buffer_id, ns_id, 0, -1, {})
if #virtual_texts then
for _, virtual_text in pairs(virtual_texts) do
local extmart_id = virtual_text[1]
if (tonumber(extmart_id) ~= nil) then
vim.api.nvim_buf_del_extmark(buffer_id, ns_id, extmart_id)
end
end
end
end
)
end
function M.toggle()
if is_loaded then
M.turn_off()
else
M.turn_on()
end
end
function M.handle_change_autocmd_callback(props)
if is_loaded then
M.refresh_highlights(props.buf, true)
end
end
function M.handle_autocmd_callback(props)
if is_loaded then
M.refresh_highlights(props.buf, false)
end
end
vim.api.nvim_create_autocmd({
"TextChanged",
"TextChangedI",
"TextChangedP",
"LspAttach",
"BufEnter",
}, {
callback = M.handle_change_autocmd_callback,
})
vim.api.nvim_create_autocmd({
"VimResized",
"WinScrolled",
}, {
callback = M.handle_autocmd_callback,
})
vim.api.nvim_create_user_command("HighlightColors",
function(opts)
local arg = string.lower(opts.fargs[1])
if arg == "on" then
M.turn_on()
elseif arg == "off" then
M.turn_off()
elseif arg == "toggle" then
M.toggle()
end
end,
{
nargs = 1,
complete = function()
return { "On", "Off", "Toggle" }
end,
desc = "Config color highlight"
}
)
return {
turnOff = M.turn_off,
turnOn = M.turn_on,
setup = M.setup,
toggle = M.toggle,
}