-
Notifications
You must be signed in to change notification settings - Fork 2
/
statusline.lua
427 lines (394 loc) · 11.3 KB
/
statusline.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
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
local statusline = {}
local utils = require('utils')
local groupid = vim.api.nvim_create_augroup('StatusLine', {})
local diag_signs_default_text = { 'E', 'W', 'I', 'H' }
local diag_severity_map = {
[1] = 'ERROR',
[2] = 'WARN',
[3] = 'INFO',
[4] = 'HINT',
ERROR = 1,
WARN = 2,
INFO = 3,
HINT = 4,
}
---@param severity integer|string
---@return string
local function get_diag_sign_text(severity)
local diag_config = vim.diagnostic.config()
local signs_text = diag_config
and diag_config.signs
and type(diag_config.signs) == 'table'
and diag_config.signs.text
return signs_text
and (signs_text[severity] or signs_text[diag_severity_map[severity]])
or (
diag_signs_default_text[severity]
or diag_signs_default_text[diag_severity_map[severity]]
)
end
-- stylua: ignore start
local modes = {
['n'] = 'NO',
['no'] = 'OP',
['nov'] = 'OC',
['noV'] = 'OL',
['no\x16'] = 'OB',
['\x16'] = 'VB',
['niI'] = 'IN',
['niR'] = 'RE',
['niV'] = 'RV',
['nt'] = 'NT',
['ntT'] = 'TM',
['v'] = 'VI',
['vs'] = 'VI',
['V'] = 'VL',
['Vs'] = 'VL',
['\x16s'] = 'VB',
['s'] = 'SE',
['S'] = 'SL',
['\x13'] = 'SB',
['i'] = 'IN',
['ic'] = 'IC',
['ix'] = 'IX',
['R'] = 'RE',
['Rc'] = 'RC',
['Rx'] = 'RX',
['Rv'] = 'RV',
['Rvc'] = 'RC',
['Rvx'] = 'RX',
['c'] = 'CO',
['cv'] = 'CV',
['r'] = 'PR',
['rm'] = 'PM',
['r?'] = 'P?',
['!'] = 'SH',
['t'] = 'TE',
}
-- stylua: ignore end
---Get string representation of the current mode
---@return string
function statusline.mode()
local hl = vim.bo.mod and 'StatusLineHeaderModified' or 'StatusLineHeader'
local mode = vim.fn.mode()
local mode_str = (mode == 'n' and (vim.bo.ro or not vim.bo.ma)) and 'RO'
or modes[mode]
return utils.stl.hl(string.format(' %s ', mode_str), hl) .. ' '
end
---Get diff stats for current buffer
---@return string
function statusline.gitdiff()
-- Integration with gitsigns.nvim
---@diagnostic disable-next-line: undefined-field
local diff = vim.b.gitsigns_status_dict or utils.git.diffstat()
local added = diff.added or 0
local changed = diff.changed or 0
local removed = diff.removed or 0
if added == 0 and removed == 0 and changed == 0 then
return ''
end
return string.format(
'+%s~%s-%s',
utils.stl.hl(tostring(added), 'StatusLineGitAdded'),
utils.stl.hl(tostring(changed), 'StatusLineGitChanged'),
utils.stl.hl(tostring(removed), 'StatusLineGitRemoved')
)
end
---Get string representation of current git branch
---@return string
function statusline.branch()
---@diagnostic disable-next-line: undefined-field
local branch = vim.b.gitsigns_status_dict and vim.b.gitsigns_status_dict.head
or utils.git.branch()
return branch == '' and '' or '#' .. branch
end
---Get current filetype
---@return string
function statusline.ft()
return vim.bo.ft == '' and '' or vim.bo.ft:gsub('^%l', string.upper)
end
---@return string
function statusline.wordcount()
local words, wordcount = 0, nil
if vim.b.wc_words and vim.b.wc_changedtick == vim.b.changedtick then
words = vim.b.wc_words
else
wordcount = vim.fn.wordcount()
words = wordcount.words
vim.b.wc_words = words
vim.b.wc_changedtick = vim.b.changedtick
end
local vwords = vim.fn.mode():find('^[vsVS\x16\x13]')
and (wordcount or vim.fn.wordcount()).visual_words
return words == 0 and ''
or (vwords and vwords > 0 and vwords .. '/' or '')
.. words
.. (words > 1 and ' words' or ' word')
end
---Text filetypes
---@type table<string, true>
local ft_text = {
[''] = true,
['tex'] = true,
['markdown'] = true,
['text'] = true,
}
---Additional info for the current buffer enclosed in parentheses
---@return string
function statusline.info()
if vim.bo.bt ~= '' then
return ''
end
local info = {}
---@param section string
local function add_section(section)
if section ~= '' then
table.insert(info, section)
end
end
add_section(statusline.ft())
if ft_text[vim.bo.ft] and not vim.b.bigfile then
add_section(statusline.wordcount())
end
add_section(statusline.branch())
add_section(statusline.gitdiff())
return vim.tbl_isempty(info) and ''
or string.format('(%s) ', table.concat(info, ', '))
end
vim.api.nvim_create_autocmd('DiagnosticChanged', {
group = groupid,
desc = 'Update diagnostics cache for the status line.',
callback = function(info)
local b = vim.b[info.buf]
local diag_cnt_cache = { 0, 0, 0, 0 }
for _, diagnostic in ipairs(info.data.diagnostics) do
diag_cnt_cache[diagnostic.severity] = diag_cnt_cache[diagnostic.severity]
+ 1
end
b.diag_str_cache = nil
b.diag_cnt_cache = diag_cnt_cache
end,
})
---Get string representation of diagnostics for current buffer
---@return string
function statusline.diag()
if vim.b.diag_str_cache then
return vim.b.diag_str_cache
end
local str = ''
local buf_cnt = vim.b.diag_cnt_cache or {}
for serverity_nr, severity in ipairs({ 'Error', 'Warn', 'Info', 'Hint' }) do
local cnt = buf_cnt[serverity_nr] or 0
if cnt > 0 then
local icon_text = get_diag_sign_text(serverity_nr)
local icon_hl = 'StatusLineDiagnostic' .. severity
str = str
.. (str == '' and '' or ' ')
.. utils.stl.hl(icon_text, icon_hl)
.. cnt
end
end
if str:find('%S') then
str = str .. ' '
end
vim.b.diag_str_cache = str
return str
end
local spinner_end_keep = 2000 -- ms
local spinner_status_keep = 600 -- ms
local spinner_progress_keep = 80 -- ms
local spinner_timer = vim.uv.new_timer()
local spinner_icons ---@type string[]
local spinner_icon_done ---@type string
if vim.g.no_nf then
spinner_icon_done = '[done]'
spinner_icons = {
'[ ]',
'[= ]',
'[== ]',
'[=== ]',
'[ ===]',
'[ ==]',
'[ =]',
}
else
spinner_icon_done = vim.trim(utils.static.icons.Ok)
spinner_icons = {
'⠋',
'⠙',
'⠹',
'⠸',
'⠼',
'⠴',
'⠦',
'⠧',
'⠇',
'⠏',
}
end
---Id and additional info of language servers in progress
---@type table<integer, { name: string, timestamp: integer, type: 'begin'|'report'|'end' }>
local server_info_in_progress = {}
vim.api.nvim_create_autocmd('LspProgress', {
desc = 'Update LSP progress info for the status line.',
group = groupid,
callback = function(info)
if spinner_timer then
spinner_timer:start(
spinner_progress_keep,
spinner_progress_keep,
vim.schedule_wrap(vim.cmd.redrawstatus)
)
end
local id = info.data.client_id
local now = vim.uv.now()
server_info_in_progress[id] = {
name = vim.lsp.get_client_by_id(id).name,
timestamp = now,
type = info.data.result and info.data.result.value.kind,
} -- Update LSP progress data
-- Clear client message after a short time if no new message is received
vim.defer_fn(function()
-- No new report since the timer was set
local last_timestamp = (server_info_in_progress[id] or {}).timestamp
if not last_timestamp or last_timestamp == now then
server_info_in_progress[id] = nil
if vim.tbl_isempty(server_info_in_progress) and spinner_timer then
spinner_timer:stop()
end
vim.cmd.redrawstatus()
end
end, spinner_end_keep)
end,
})
---@return string
function statusline.lsp_progress()
if vim.tbl_isempty(server_info_in_progress) then
return ''
end
local buf = vim.api.nvim_get_current_buf()
local server_ids = {}
for id, _ in pairs(server_info_in_progress) do
if vim.tbl_contains(vim.lsp.get_buffers_by_client_id(id), buf) then
table.insert(server_ids, id)
end
end
if vim.tbl_isempty(server_ids) then
return ''
end
local now = vim.uv.now()
---@return boolean
local function allow_changing_state()
return not vim.b.spinner_state_changed
or now - vim.b.spinner_state_changed > spinner_status_keep
end
if
#server_ids == 1
and server_info_in_progress[server_ids[1]].type == 'end'
then
if vim.b.spinner_icon ~= spinner_icon_done and allow_changing_state() then
vim.b.spinner_state_changed = now
vim.b.spinner_icon = spinner_icon_done
end
else
local spinner_icon_progress = spinner_icons[math.ceil(
now / spinner_progress_keep
) % #spinner_icons + 1]
if vim.b.spinner_icon ~= spinner_icon_done then
vim.b.spinner_icon = spinner_icon_progress
elseif allow_changing_state() then
vim.b.spinner_state_changed = now
vim.b.spinner_icon = spinner_icon_progress
end
end
return string.format(
'%s %s ',
table.concat(
vim.tbl_map(function(id)
return server_info_in_progress[id].name
end, server_ids),
', '
),
vim.b.spinner_icon
)
end
-- stylua: ignore start
---Statusline components
---@type table<string, string>
local components = {
align = [[%=]],
diag = [[%{%v:lua.require'plugin.statusline'.diag()%}]],
fname = [[%{%&bt==#''?'%t':(&bt==#'terminal'?'[Terminal] '.bufname()->substitute('^term://.\{-}//\d\+:\s*','',''):'%F')%} ]],
info = [[%{%v:lua.require'plugin.statusline'.info()%}]],
lsp_progress = [[%{%v:lua.require'plugin.statusline'.lsp_progress()%}]],
mode = [[%{%v:lua.require'plugin.statusline'.mode()%}]],
padding = [[ ]],
pos = [[%{%&ru?"%l:%c ":""%}]],
truncate = [[%<]],
}
-- stylua: ignore end
local stl = table.concat({
components.mode,
components.fname,
components.info,
components.align,
components.truncate,
components.lsp_progress,
components.diag,
components.pos,
})
local stl_nc = table.concat({
components.padding,
components.fname,
components.align,
components.truncate,
components.pos,
})
---Get statusline string
---@return string
function statusline.get()
return vim.g.statusline_winid == vim.api.nvim_get_current_win() and stl
or stl_nc
end
vim.api.nvim_create_autocmd(
{ 'FileChangedShellPost', 'DiagnosticChanged', 'LspProgress' },
{
group = groupid,
command = 'redrawstatus',
}
)
---Set default highlight groups for statusline components
---@return nil
local function set_default_hlgroups()
local default_attr = utils.hl.get(0, {
name = 'StatusLine',
link = false,
winhl_link = false,
})
---@param hlgroup_name string
---@param attr table
---@return nil
local function sethl(hlgroup_name, attr)
local merged_attr = vim.tbl_deep_extend('keep', attr, default_attr)
utils.hl.set_default(0, hlgroup_name, merged_attr)
end
sethl('StatusLineGitAdded', { fg = 'GitSignsAdd' })
sethl('StatusLineGitChanged', { fg = 'GitSignsChange' })
sethl('StatusLineGitRemoved', { fg = 'GitSignsDelete' })
sethl('StatusLineDiagnosticHint', { fg = 'DiagnosticSignHint' })
sethl('StatusLineDiagnosticInfo', { fg = 'DiagnosticSignInfo' })
sethl('StatusLineDiagnosticWarn', { fg = 'DiagnosticSignWarn' })
sethl('StatusLineDiagnosticError', { fg = 'DiagnosticSignError' })
sethl('StatusLineHeader', { fg = 'TabLine', bg = 'fg', reverse = true })
sethl('StatusLineHeaderModified', {
fg = 'Special',
bg = 'fg',
reverse = true,
})
end
set_default_hlgroups()
vim.api.nvim_create_autocmd('ColorScheme', {
group = groupid,
callback = set_default_hlgroups,
})
return statusline