REFACTOR: reducing more
This commit is contained in:
@@ -1,71 +1,60 @@
|
|||||||
local ok, D = pcall(require, 'nvim-web-devicons')
|
local D = require('nvim-web-devicons')
|
||||||
local a, S = vim.api, vim.diagnostic.severity
|
local a, S = vim.api, vim.diagnostic.severity
|
||||||
local now = (vim.uv or vim.loop).now
|
local now = (vim.uv or vim.loop).now
|
||||||
local C = a.nvim_get_current_buf
|
local C = a.nvim_get_current_buf
|
||||||
|
local function cn(c)
|
||||||
|
return c.name
|
||||||
|
end
|
||||||
local R = function()
|
local R = function()
|
||||||
vim.cmd.redrawstatus()
|
vim.cmd.redrawstatus()
|
||||||
end -- absorb autocmd ev (.file etc.)
|
end
|
||||||
local M = '%m%r %='
|
|
||||||
local LOAD = '%#DiagnosticInfo#'
|
|
||||||
local T, JOB = {}, nil
|
local T, JOB = {}, nil
|
||||||
local IV = vim.g.statusline_prog_interval or 750 -- ms cap between progress redraws
|
local IV, LAST = 750, 0
|
||||||
local LAST = 0
|
|
||||||
local function snap(bb)
|
local function snap(bb)
|
||||||
local nm = ' %F' -- native tail: handles [No Name] and escaping in C
|
local nm = ' %F'
|
||||||
if ok then
|
|
||||||
local i, hl = D.get_icon(a.nvim_buf_get_name(bb), nil, { default = true })
|
local i, hl = D.get_icon(a.nvim_buf_get_name(bb), nil, { default = true })
|
||||||
if i then
|
if i then
|
||||||
nm = ' %#' .. hl .. '#' .. i .. ' %*%F'
|
nm = ' %#' .. hl .. '#' .. i .. ' %*%F'
|
||||||
end
|
end
|
||||||
end
|
|
||||||
local x = vim.diagnostic.count(bb)
|
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 e, w, h = x[S.ERROR] or 0, x[S.WARN] or 0, x[S.HINT] or 0
|
||||||
local dg = ''
|
local dg = (e > 0 and ('%#DiagnosticError#E' .. e .. '%* ') or '')
|
||||||
if e + w + h > 0 then
|
|
||||||
dg = (e > 0 and ('%#DiagnosticError#E' .. e .. '%* ') or '')
|
|
||||||
.. (w > 0 and ('%#DiagnosticWarn#W' .. w .. '%* ') or '')
|
.. (w > 0 and ('%#DiagnosticWarn#W' .. w .. '%* ') or '')
|
||||||
.. (h > 0 and ('%#DiagnosticHint#H' .. h .. '%* ') or '')
|
.. (h > 0 and ('%#DiagnosticHint#H' .. h .. '%* ') or '')
|
||||||
end
|
local cs = vim.lsp.get_clients({ bufnr = bb })
|
||||||
local rd, cs = '', vim.lsp.get_clients({ bufnr = bb })
|
local rd = #cs > 0 and (' ' .. table.concat(vim.tbl_map(cn, cs), '/') .. ' ') or ''
|
||||||
if #cs > 0 then
|
return nm .. '%m%r %=' .. dg, rd
|
||||||
local nm2 = {}
|
|
||||||
for i = 1, #cs do
|
|
||||||
nm2[i] = cs[i].name
|
|
||||||
end
|
|
||||||
rd = ' ' .. table.concat(nm2, '/') .. ' '
|
|
||||||
end
|
|
||||||
return nm .. M .. dg, rd
|
|
||||||
end
|
end
|
||||||
|
|
||||||
local function touch(bb, redraw)
|
local function touch(bb, redraw)
|
||||||
local v = vim.b[bb] -- per-buffer cache; nvim frees b: vars on unload
|
vim.b[bb].d = true
|
||||||
if v then
|
|
||||||
v.d = true
|
|
||||||
end
|
|
||||||
if redraw then
|
if redraw then
|
||||||
R()
|
R()
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
local function SYNC(force) -- refresh JOB; redraw only when it changed and (force or gate open)
|
|
||||||
|
local function sync(force)
|
||||||
local prev = JOB
|
local prev = JOB
|
||||||
JOB = nil
|
JOB = nil
|
||||||
for _, t in pairs(T) do
|
for _, t in pairs(T) do
|
||||||
for _, m in pairs(t) do
|
local _, m = next(t)
|
||||||
|
if m then
|
||||||
JOB = m
|
JOB = m
|
||||||
break
|
break
|
||||||
end
|
end
|
||||||
if JOB then
|
|
||||||
break
|
|
||||||
end
|
|
||||||
end
|
end
|
||||||
if JOB ~= prev and (force or now() - LAST >= IV) then
|
if JOB ~= prev and (force or now() - LAST >= IV) then
|
||||||
LAST = now()
|
LAST = now()
|
||||||
R()
|
R()
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
local G = a.nvim_create_augroup('STL5', { clear = true })
|
local G = a.nvim_create_augroup('STL5', { clear = true })
|
||||||
local function au(ev, cb)
|
local function au(ev, cb)
|
||||||
a.nvim_create_autocmd(ev, { group = G, callback = cb })
|
a.nvim_create_autocmd(ev, { group = G, callback = cb })
|
||||||
end
|
end
|
||||||
|
|
||||||
au({ 'BufEnter', 'BufFilePost' }, function(e)
|
au({ 'BufEnter', 'BufFilePost' }, function(e)
|
||||||
touch(e.buf, true)
|
touch(e.buf, true)
|
||||||
end)
|
end)
|
||||||
@@ -75,39 +64,32 @@ end)
|
|||||||
au({ 'LspAttach', 'LspDetach' }, function(e)
|
au({ 'LspAttach', 'LspDetach' }, function(e)
|
||||||
if e.event == 'LspDetach' then
|
if e.event == 'LspDetach' then
|
||||||
T[e.data.client_id] = nil
|
T[e.data.client_id] = nil
|
||||||
SYNC(true)
|
sync(true)
|
||||||
end
|
end
|
||||||
touch(e.buf, true)
|
touch(e.buf, true)
|
||||||
end)
|
end)
|
||||||
au('LspProgress', function(e)
|
au('LspProgress', function(e)
|
||||||
local v = e.data.params.value
|
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'
|
local id, tk = e.data.client_id, e.data.params.token or 'default'
|
||||||
T[id] = T[id] or {}
|
T[id] = T[id] or {}
|
||||||
if v.kind == 'end' or (v.kind == 'report' and v.percentage == 100) then
|
if v.kind == 'end' or (v.kind == 'report' and v.percentage == 100) then
|
||||||
T[id][tk] = nil
|
T[id][tk] = nil
|
||||||
SYNC(true) -- clear immediately, don't gate
|
sync(true)
|
||||||
else
|
else
|
||||||
local m = (v.title and v.title ~= '' and v.title) or v.message or 'Loading...'
|
local title = (v.title and v.title ~= '') and v.title or nil
|
||||||
if v.title and v.message and v.message ~= '' then
|
local msg = (v.message and v.message ~= '') and v.message or nil
|
||||||
m = v.title .. ' ' .. v.message
|
T[id][tk] = (title and msg and (title .. ' ' .. msg)) or title or msg or 'Loading...'
|
||||||
end
|
sync()
|
||||||
T[id][tk] = m
|
|
||||||
SYNC() -- gated: at most one redraw per IV ms
|
|
||||||
end
|
end
|
||||||
end)
|
end)
|
||||||
au('ColorScheme', R)
|
au({ 'ColorScheme', 'FocusGained' }, R)
|
||||||
au('FocusGained', R)
|
|
||||||
local function Status()
|
function _G.Status()
|
||||||
local v = vim.b
|
local v = vim.b
|
||||||
if not v.h or v.d then
|
if not v.h or v.d then
|
||||||
v.h, v.r = snap(C())
|
v.h, v.r = snap(C())
|
||||||
v.stl = v.h .. v.r
|
v.stl, v.d = v.h .. v.r, nil
|
||||||
v.d = nil
|
|
||||||
end
|
end
|
||||||
return JOB and (v.h .. LOAD .. JOB .. '%* ') or v.stl
|
return JOB and (v.h .. '%#DiagnosticInfo#' .. JOB .. '%* ') or v.stl
|
||||||
end
|
end
|
||||||
_G.Status = Status
|
|
||||||
vim.opt.statusline = '%!v:lua.Status()'
|
vim.opt.statusline = '%!v:lua.Status()'
|
||||||
|
|||||||
Reference in New Issue
Block a user