REFACTOR: Even more cleanup
This commit is contained in:
@@ -1,52 +1,30 @@
|
||||
local ok, D = pcall(require, 'nvim-web-devicons')
|
||||
local a, u, S = vim.api, vim.uv or vim.loop, vim.diagnostic.severity
|
||||
local a, S = vim.api, vim.diagnostic.severity
|
||||
local now = (vim.uv or vim.loop).now
|
||||
local C = a.nvim_get_current_buf
|
||||
local R = function()
|
||||
vim.cmd.redrawstatus()
|
||||
end
|
||||
local M = '%#StatusLineMod#%m%*%#StatusLineRO#%r%* %='
|
||||
local FR = { '⠋', '⠙', '⠹', '⠸', '⠼', '⠴', '⠦', '⠧', '⠇', '⠏' }
|
||||
local N, SPIN, LOAD = #FR, '%#StatusLspSpinner#', '%* %#StatusLspLoading#'
|
||||
local c, T, JOB, TMR, IDX = {}, {}, nil, nil, 1
|
||||
local function HI()
|
||||
local bg = a.nvim_get_hl(0, { name = 'StatusLine' }).bg or 'none'
|
||||
for _, t in ipairs({
|
||||
{ 'StatusLineFilename', 'Normal', true },
|
||||
{ 'StatusLineMod', 'WarningMsg', true },
|
||||
{ 'StatusLineRO', 'ErrorMsg', true },
|
||||
{ 'StatusDiagError', 'DiagnosticError', true },
|
||||
{ 'StatusDiagWarn', 'DiagnosticWarn', false },
|
||||
{ 'StatusDiagHint', 'DiagnosticHint', false },
|
||||
{ 'StatusLspSpinner', 'DiagnosticWarn', true },
|
||||
{ 'StatusLspLoading', 'DiagnosticInfo', false },
|
||||
{ 'StatusLspReady', 'Comment', false },
|
||||
}) do
|
||||
local h = a.nvim_get_hl(0, { name = t[2], link = false })
|
||||
a.nvim_set_hl(0, t[1], { fg = h.fg, bg = bg, bold = t[3] })
|
||||
end
|
||||
end
|
||||
end -- absorb autocmd ev (.file etc.)
|
||||
local M = '%m%r %='
|
||||
local LOAD = '%#DiagnosticInfo#'
|
||||
local T, JOB = {}, nil
|
||||
local IV = vim.g.statusline_prog_interval or 750 -- ms cap between progress redraws
|
||||
local LAST = 0
|
||||
local function snap(bb)
|
||||
local p = a.nvim_buf_get_name(bb)
|
||||
local nm = ' %#StatusLineFilename#'
|
||||
if p == '' then
|
||||
nm = nm .. '[No Name]%*'
|
||||
else
|
||||
local f = p:match('([^/\\]+)$') or p
|
||||
if ok then
|
||||
local i, hl = D.get_icon(f, p:match('%.([^./\\]+)$'), { default = true })
|
||||
if i then
|
||||
nm = nm .. '%#' .. hl .. '#' .. i .. ' %*'
|
||||
end
|
||||
local nm = ' %F' -- native tail: handles [No Name] and escaping in C
|
||||
if ok then
|
||||
local i, hl = D.get_icon(a.nvim_buf_get_name(bb), nil, { default = true })
|
||||
if i then
|
||||
nm = ' %#' .. hl .. '#' .. i .. ' %*%F'
|
||||
end
|
||||
nm = nm .. f .. '%*'
|
||||
end
|
||||
local x = vim.diagnostic.count(bb)
|
||||
local e, w, h = x[S.ERROR] or 0, x[S.WARN] or 0, x[S.HINT] or 0
|
||||
local dg = ''
|
||||
if e + w + h > 0 then
|
||||
dg = (e > 0 and ('%#StatusDiagError#E' .. e .. '%* ') or '')
|
||||
.. (w > 0 and ('%#StatusDiagWarn#W' .. w .. '%* ') or '')
|
||||
.. (h > 0 and ('%#StatusDiagHint#H' .. h .. '%* ') or '')
|
||||
dg = (e > 0 and ('%#DiagnosticError#E' .. e .. '%* ') or '')
|
||||
.. (w > 0 and ('%#DiagnosticWarn#W' .. w .. '%* ') or '')
|
||||
.. (h > 0 and ('%#DiagnosticHint#H' .. h .. '%* ') or '')
|
||||
end
|
||||
local rd, cs = '', vim.lsp.get_clients({ bufnr = bb })
|
||||
if #cs > 0 then
|
||||
@@ -54,12 +32,12 @@ local function snap(bb)
|
||||
for i = 1, #cs do
|
||||
nm2[i] = cs[i].name
|
||||
end
|
||||
rd = '%#StatusLspReady# ' .. table.concat(nm2, '/') .. '%* '
|
||||
rd = ' ' .. table.concat(nm2, '/') .. ' '
|
||||
end
|
||||
return nm .. M .. dg, rd
|
||||
end
|
||||
local function touch(bb, redraw)
|
||||
local v = c[bb]
|
||||
local v = vim.b[bb] -- per-buffer cache; nvim frees b: vars on unload
|
||||
if v then
|
||||
v.d = true
|
||||
end
|
||||
@@ -67,7 +45,8 @@ local function touch(bb, redraw)
|
||||
R()
|
||||
end
|
||||
end
|
||||
local function SYNC()
|
||||
local function SYNC(force) -- refresh JOB; redraw only when it changed and (force or gate open)
|
||||
local prev = JOB
|
||||
JOB = nil
|
||||
for _, t in pairs(T) do
|
||||
for _, m in pairs(t) do
|
||||
@@ -78,109 +57,57 @@ local function SYNC()
|
||||
break
|
||||
end
|
||||
end
|
||||
if JOB and not TMR then
|
||||
IDX = math.floor(u.now() / 100) % N + 1
|
||||
TMR = u.new_timer()
|
||||
TMR:start(
|
||||
0,
|
||||
100,
|
||||
vim.schedule_wrap(function()
|
||||
if JOB then
|
||||
IDX = IDX % N + 1
|
||||
R()
|
||||
elseif TMR then
|
||||
TMR:stop()
|
||||
TMR:close()
|
||||
TMR = nil
|
||||
end
|
||||
end)
|
||||
)
|
||||
elseif not JOB and TMR then
|
||||
TMR:stop()
|
||||
TMR:close()
|
||||
TMR = nil
|
||||
if JOB ~= prev and (force or now() - LAST >= IV) then
|
||||
LAST = now()
|
||||
R()
|
||||
end
|
||||
end
|
||||
local G = a.nvim_create_augroup('STL5', { clear = true })
|
||||
for _, s in ipairs({
|
||||
{
|
||||
{ 'BufEnter', 'BufFilePost' },
|
||||
function(e)
|
||||
touch(e.buf, true)
|
||||
end,
|
||||
},
|
||||
{
|
||||
'DiagnosticChanged',
|
||||
function(e)
|
||||
touch(e.buf, e.buf == C())
|
||||
end,
|
||||
},
|
||||
{
|
||||
{ 'LspAttach', 'LspDetach' },
|
||||
function(e)
|
||||
if e.event == 'LspDetach' then
|
||||
T[e.data.client_id] = nil
|
||||
SYNC()
|
||||
end
|
||||
touch(e.buf, true)
|
||||
end,
|
||||
},
|
||||
{
|
||||
'LspProgress',
|
||||
function(e)
|
||||
local v = e.data.params.value
|
||||
if type(v) ~= 'table' then
|
||||
return
|
||||
end
|
||||
local id, tk = e.data.client_id, e.data.params.token or 'default'
|
||||
T[id] = T[id] or {}
|
||||
if v.kind == 'end' or (v.kind == 'report' and v.percentage == 100) then
|
||||
T[id][tk] = nil
|
||||
else
|
||||
local m = (v.title and v.title ~= '' and v.title) or v.message or 'Loading...'
|
||||
if v.title and v.message and v.message ~= '' then
|
||||
m = v.title .. ' ' .. v.message
|
||||
end
|
||||
T[id][tk] = m
|
||||
end
|
||||
SYNC()
|
||||
end,
|
||||
},
|
||||
{
|
||||
'BufUnload',
|
||||
function(e)
|
||||
c[e.buf] = nil
|
||||
end,
|
||||
},
|
||||
{
|
||||
'ColorScheme',
|
||||
function()
|
||||
HI()
|
||||
R()
|
||||
end,
|
||||
},
|
||||
{ 'FocusGained', R },
|
||||
}) do
|
||||
a.nvim_create_autocmd(s[1], { group = G, callback = s[2] })
|
||||
local function au(ev, cb)
|
||||
a.nvim_create_autocmd(ev, { group = G, callback = cb })
|
||||
end
|
||||
HI()
|
||||
local function Status()
|
||||
local bb = C()
|
||||
local v = c[bb]
|
||||
if not v or v.d then
|
||||
if not v then
|
||||
v = {}
|
||||
c[bb] = v
|
||||
au({ 'BufEnter', 'BufFilePost' }, function(e)
|
||||
touch(e.buf, true)
|
||||
end)
|
||||
au('DiagnosticChanged', function(e)
|
||||
touch(e.buf, e.buf == C())
|
||||
end)
|
||||
au({ 'LspAttach', 'LspDetach' }, function(e)
|
||||
if e.event == 'LspDetach' then
|
||||
T[e.data.client_id] = nil
|
||||
SYNC(true)
|
||||
end
|
||||
touch(e.buf, true)
|
||||
end)
|
||||
au('LspProgress', function(e)
|
||||
local v = e.data.params.value
|
||||
if type(v) ~= 'table' then
|
||||
return
|
||||
end
|
||||
local id, tk = e.data.client_id, e.data.params.token or 'default'
|
||||
T[id] = T[id] or {}
|
||||
if v.kind == 'end' or (v.kind == 'report' and v.percentage == 100) then
|
||||
T[id][tk] = nil
|
||||
SYNC(true) -- clear immediately, don't gate
|
||||
else
|
||||
local m = (v.title and v.title ~= '' and v.title) or v.message or 'Loading...'
|
||||
if v.title and v.message and v.message ~= '' then
|
||||
m = v.title .. ' ' .. v.message
|
||||
end
|
||||
v.h, v.r = snap(bb)
|
||||
T[id][tk] = m
|
||||
SYNC() -- gated: at most one redraw per IV ms
|
||||
end
|
||||
end)
|
||||
au('ColorScheme', R)
|
||||
au('FocusGained', R)
|
||||
local function Status()
|
||||
local v = vim.b
|
||||
if not v.h or v.d then
|
||||
v.h, v.r = snap(C())
|
||||
v.stl = v.h .. v.r
|
||||
v.d = nil
|
||||
end
|
||||
if JOB then
|
||||
return v.h .. SPIN .. FR[IDX] .. LOAD .. JOB .. '%* '
|
||||
end
|
||||
return v.stl
|
||||
return JOB and (v.h .. LOAD .. JOB .. '%* ') or v.stl
|
||||
end
|
||||
_G.Status = Status
|
||||
vim.opt.statusline = '%!v:lua.Status()'
|
||||
|
||||
Reference in New Issue
Block a user