|
| 1 | +--- Timer instance |
| 2 | +local timer = nil |
| 3 | +--- This flag is used to redraw one more time after the las LSP update. |
| 4 | +-- NOTE: This is achieved by setting the redraw_flag to true if |
| 5 | +-- vim.b.lsp_status_redraw is true and there has been an update. Next time if |
| 6 | +-- vim.b.lsp_status_redraw is false (no update) it will still redraw the |
| 7 | +-- statusline and this flag will be set to false. |
| 8 | +local redraw_flag = false |
| 9 | + |
| 10 | +--- Timer callback function |
| 11 | +-- NOTE: This function uses get_lsp_statusline so that needs nvim api functions |
| 12 | +-- so it should call in the vim.schedule_wrap() |
| 13 | +local function timer_callback() |
| 14 | + -- Check if need to redraw |
| 15 | + local update_flag = vim.b.lsp_status_redraw |
| 16 | + if update_flag or redraw_flag then |
| 17 | + vim.b.lsp_status_redraw = false |
| 18 | + -- Schedule the command when it's safe to call it |
| 19 | + local new_state = require('lsp-status/statusline').get_lsp_statusline() |
| 20 | + if new_state ~= vim.b.lsp_status_statusline then |
| 21 | + vim.b.lsp_status_statusline = new_state |
| 22 | + vim.api.nvim_command('redrawstatus!') |
| 23 | + -- If this was an lsp update (update_flag is true) redraw also next time |
| 24 | + -- by setting redraw_flag |
| 25 | + redraw_flag = update_flag |
| 26 | + end |
| 27 | + end |
| 28 | +end |
| 29 | + |
| 30 | +--- Function to register a timer to update statusline |
| 31 | +-- This function is called on attach of a LSP Server and will pull updates for |
| 32 | +-- status line on a specific every interval of time. It will also schedule the |
| 33 | +-- redraw of the status line on the main loop. This will reduce the lag for |
| 34 | +-- servers that constantly update the messages like `rust-analyzer`. It will |
| 35 | +-- set use the variable timer to schedule the updates. |
| 36 | +-- TODO: This could error if the lsp is disconnected, the timer should be |
| 37 | +-- stopped |
| 38 | +local function register_timer() |
| 39 | + -- Guard the for an already defined timer |
| 40 | + if timer ~= nil then |
| 41 | + return |
| 42 | + end |
| 43 | + timer = vim.loop.new_timer() |
| 44 | + -- Execute the timer every 100 milliseconds |
| 45 | + -- NOTE: This could be 30 to get a 30 updates per seconds, but set to 100 to |
| 46 | + -- copy coc.nvim status interval |
| 47 | + timer:start(0, 100, vim.schedule_wrap(timer_callback)) |
| 48 | +end |
| 49 | + |
| 50 | +local M = { |
| 51 | + timer = timer, |
| 52 | + register_timer = register_timer, |
| 53 | +} |
| 54 | + |
| 55 | +return M |
0 commit comments