-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathcolorcolumn.lua
281 lines (257 loc) · 7.79 KB
/
colorcolumn.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
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
local hl = require('utils.hl')
---Resolve the colorcolumn value
---@param cc string|nil
---@return integer|nil cc_number smallest integer >= 0 or nil
local function cc_resolve(cc)
if not cc or cc == '' then
return nil
end
local cc_tbl = vim.split(cc, ',')
local cc_min = nil
for _, cc_str in ipairs(cc_tbl) do
local cc_number = tonumber(cc_str)
if vim.startswith(cc_str, '+') or vim.startswith(cc_str, '-') then
cc_number = vim.bo.tw > 0 and vim.bo.tw + cc_number or nil
end
if cc_number and cc_number > 0 and (not cc_min or cc_number < cc_min) then
cc_min = cc_number
end
end
return cc_min
end
---Default options
---@class cc_opts_t
local opts = {
-- Dynamically adjusts the colorcolumn behavior based on editing mode:
-- 1. In insert/replace/selection mode: update the color gradually based on
-- current line length
-- 2. In other modes: update the color based on longest visible line, if
-- there's any line that exceeds the colorcolumn limit, show the
-- colorcolumn with warning color, else conceal the colorcolumn entirely
scope = function()
-- Don't show in read-only buffers
if not vim.bo.ma or vim.bo.ro then
return 0
end
if vim.fn.mode():find('^[iRss\x13]') ~= nil then
return vim.fn.strdisplaywidth(vim.fn.getline('.'))
end
-- Find maximum length within visible range
local max_len = math.max(
unpack(
vim.tbl_map(
vim.fn.strdisplaywidth,
vim.api.nvim_buf_get_lines(
0,
vim.fn.line('w0') - 1,
vim.fn.line('w$'),
false
)
)
)
)
if max_len >= cc_resolve(vim.wo.cc) then
return max_len
end
return 0
end,
modes = true, ---@type string[]|boolean|fun(mode: string): boolean
warning_modes = function() ---@type string[]|boolean|fun(mode: string): boolean
-- Show warning colors only in insert/replace/selection mode
return vim.fn.mode():find('^[iRss\x13]')
end,
blending = {
threshold = 0.5,
colorcode = '#000000',
hlgroup = { 'Normal', 'bg' },
},
warning = {
alpha = 0.4,
offset = 0,
colorcode = '#FF0000',
hlgroup = { 'Error', 'fg' },
},
extra = {
---@type string?
follow_tw = nil,
},
}
local C_NORMAL, C_CC, C_ERROR
---Get background color in hex
---@param hlgroup_name string
---@param field 'fg'|'bg'
---@param fallback string|nil fallback color in hex, default to '#000000' if &bg is 'dark' and '#FFFFFF' if &bg is 'light'
---@return string hex color
local function get_hl_hex(hlgroup_name, field, fallback)
fallback = fallback or vim.opt.bg == 'dark' and '#000000' or '#FFFFFF'
if not vim.fn.hlexists(hlgroup_name) then
return fallback
end
-- Do not use link = false here, because nvim will return the highlight
-- attributes of the remapped hlgroup if link = false when winhl is set
-- e.g. when winhl=ColorColumn:FooBar, nvim will return the attributes of
-- FooBar instead of ColorColumn with link = false, but return the
-- attributes of ColorColumn with link = true
-- EDIT: solved by manually traversing the hlgroup, see implementation
-- of `utils.hl.get()`
local attr_val =
hl.get(0, { name = hlgroup_name, winhl_link = false })[field]
return attr_val and hl.dec2hex(attr_val, 6) or fallback
end
---Update base colors: bg color of Normal & ColorColumn, and fg of Error
---@return nil
local function update_hl_hex()
C_NORMAL = get_hl_hex(
opts.blending.hlgroup[1],
opts.blending.hlgroup[2],
opts.blending.colorcode
)
C_ERROR = get_hl_hex(
opts.warning.hlgroup[1],
opts.warning.hlgroup[2],
opts.warning.colorcode
)
C_CC = get_hl_hex('ColorColumn', 'bg')
end
---Hide colorcolumn
---@param winid integer? window handler
local function cc_conceal(winid)
vim.api.nvim_win_call(winid or 0, function()
if vim.opt_local.winhl:get().ColorColumn ~= '' then ---@diagnostic disable-line: undefined-field
vim.opt_local.winhl:append({ ColorColumn = '' }) ---@diagnostic disable-line: undefined-field
end
end)
end
---Show colorcolumn
---@param winid integer? window handler
local function cc_show(winid)
vim.api.nvim_win_call(winid or 0, function()
if vim.opt_local.winhl:get().ColorColumn ~= '_ColorColumn' then ---@diagnostic disable-line: undefined-field
vim.opt_local.winhl:append({ ColorColumn = '_ColorColumn' }) ---@diagnostic disable-line: undefined-field
end
end)
end
---@param opt_name string name of the mode option
---@return fun(): boolean function to check the mode option
local function check_mode_fn(opt_name)
return function()
local opt = opts[opt_name] ---@type string[]|boolean|fun(mode: string): boolean
if type(opt) == 'boolean' then
return opt ---@type boolean
end
if type(opt) == 'function' then
return opt(vim.fn.mode())
end
return type(opt) == 'table'
and vim.tbl_contains(opt --[=[@as string[]]=], vim.fn.mode())
or false
end
end
local check_mode = check_mode_fn('modes')
local check_warning_mode = check_mode_fn('warning_modes')
local cc_bg = nil
local cc_link = nil
---Update colorcolumn highlight or conceal it
---@param winid integer? handler, default 0
---@return nil
local function cc_update(winid)
winid = winid or 0
local cc = cc_resolve(vim.wo[winid].cc)
if not check_mode() or not cc then
cc_conceal(winid)
return
end
-- Fix 'E976: using Blob as a String' after select a snippet
-- entry from LSP server using omnifunc `<C-x><C-o>`
---@diagnostic disable-next-line: param-type-mismatch
local length = opts.scope()
local thresh = opts.blending.threshold
if 0 < thresh and thresh <= 1 then
thresh = math.floor(thresh * cc)
end
if length < thresh then
cc_conceal(winid)
return
end
-- Show blended color when len < cc
local show_warning = check_warning_mode()
and length >= cc + opts.warning.offset
if vim.go.termguicolors then
if not C_CC or not C_NORMAL or not C_ERROR then
update_hl_hex()
end
local new_cc_color = show_warning
and hl.cblend(C_ERROR, C_NORMAL, opts.warning.alpha).dec
or hl.cblend(
C_CC,
C_NORMAL,
math.min(1, (length - thresh) / (cc - thresh))
).dec
if new_cc_color ~= cc_bg then
cc_bg = new_cc_color
vim.api.nvim_set_hl(0, '_ColorColumn', { bg = cc_bg })
end
else
local link = show_warning and opts.warning.hlgroup[1] or 'ColorColumn'
if cc_link ~= link then
cc_link = link
vim.api.nvim_set_hl(0, '_ColorColumn', { link = cc_link })
end
end
cc_show(winid)
end
---Setup colorcolumn
---@param o cc_opts_t?
local function setup(o)
if vim.g.loaded_colorcolumn ~= nil then
return
end
vim.g.loaded_colorcolumn = true
if o then
opts = vim.tbl_deep_extend('force', opts, o)
end
---Conceal colorcolumn in each window
for _, win in ipairs(vim.api.nvim_list_wins()) do
cc_conceal(win)
end
---Create autocmds for concealing / showing colorcolumn
local id = vim.api.nvim_create_augroup('AutoColorColumn', {})
vim.api.nvim_create_autocmd('WinLeave', {
desc = 'Conceal colorcolumn in other windows.',
group = id,
callback = function()
cc_conceal()
end,
})
vim.api.nvim_create_autocmd('ColorScheme', {
desc = 'Update base colors.',
group = id,
callback = update_hl_hex,
})
vim.api.nvim_create_autocmd({
'BufEnter',
'ColorScheme',
'CursorMoved',
'CursorMovedI',
'ModeChanged',
'TextChanged',
'TextChangedI',
'WinEnter',
'WinScrolled',
}, {
desc = 'Update colorcolumn color.',
group = id,
callback = function()
cc_update()
end,
})
vim.api.nvim_create_autocmd('OptionSet', {
desc = 'Update colorcolumn color.',
pattern = { 'colorcolumn', 'textwidth' },
group = id,
callback = function()
cc_update()
end,
})
end
setup()