progress.lua (2295B)
1 local M = {} 2 3 local client_notifs = {} 4 5 local function get_notif_data(client_id, token) 6 if not client_notifs[client_id] then client_notifs[client_id] = {} end 7 8 if not client_notifs[client_id][token] then client_notifs[client_id][token] = {} end 9 10 return client_notifs[client_id][token] 11 end 12 13 local spinner_frames = { '⣾', '⣽', '⣻', '⢿', '⡿', '⣟', '⣯', '⣷' } 14 15 local function update_spinner(client_id, token) 16 local notif_data = get_notif_data(client_id, token) 17 18 if notif_data.spinner then 19 local new_spinner = (notif_data.spinner + 1) % #spinner_frames 20 notif_data.spinner = new_spinner 21 22 notif_data.notification = vim.notify(nil, nil, { 23 hide_from_history = true, 24 icon = spinner_frames[new_spinner], 25 replace = notif_data.notification, 26 }) 27 28 vim.defer_fn(function() update_spinner(client_id, token) end, 100) 29 end 30 end 31 32 local function format_title(title, client_name) return client_name .. (#title > 0 and ': ' .. title or '') end 33 34 local function format_message(message, percentage) return (percentage and percentage .. '%\t' or '') .. (message or '') end 35 36 M.handler = function(_, result, ctx) 37 local client_id = ctx.client_id 38 39 local val = result.value 40 41 if not val.kind then return end 42 print(val.kind) 43 44 local notif_data = get_notif_data(client_id, result.token) 45 46 if val.kind == 'begin' then 47 local message = format_message(val.message, val.percentage) 48 49 notif_data.notification = vim.notify(message, 'info', { 50 title = format_title(val.title, vim.lsp.get_client_by_id(client_id).name), 51 icon = spinner_frames[1], 52 timeout = false, 53 hide_from_history = false, 54 }) 55 56 notif_data.spinner = 1 57 update_spinner(client_id, result.token) 58 elseif val.kind == 'report' and notif_data then 59 notif_data.notification = vim.notify(format_message(val.message, val.percentage), 'info', 60 { replace = notif_data.notification, hide_from_history = false }) 61 elseif val.kind == 'end' and notif_data then 62 notif_data.notification = vim.notify(val.message and format_message(val.message) or 'Complete', 'info', 63 { icon = '', replace = notif_data.notification, timeout = 3000 }) 64 65 notif_data.spinner = nil 66 end 67 end 68 69 return M