Skip to content

Commit 0f29b97

Browse files
authored
Refactor: Redraw as-needed (#45)
* Implement as-needed redraw * Run lua-format * Fix missing case for setting timer_going * Make redraw timing more precise
1 parent 8c955db commit 0f29b97

File tree

8 files changed

+58
-90
lines changed

8 files changed

+58
-90
lines changed

lua/lsp-status.lua

Lines changed: 13 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ local default_config = {
77
indicator_info = '🛈',
88
indicator_hint = '',
99
indicator_ok = '',
10-
spinner_frames = { '', '', '', '', '', '', '', '' },
10+
spinner_frames = {'', '', '', '', '', '', '', ''},
1111
status_symbol = ' 🇻',
1212
select_symbol = nil,
1313
update_interval = 100
@@ -19,7 +19,7 @@ local messages = {}
1919
-- Diagnostics
2020
--- Get all diagnostics for the current buffer.
2121
--- Convenience function to retrieve all diagnostic counts for the current buffer.
22-
--@returns `{ 'Error': error_count, 'Warning': warning_count', 'Info': info_count, 'Hint': hint_count `}
22+
-- @returns `{ 'Error': error_count, 'Warning': warning_count', 'Info': info_count, 'Hint': hint_count `}
2323
local function diagnostics() -- luacheck: no unused
2424
error() -- Stub for docs
2525
end
@@ -32,16 +32,13 @@ local messaging = require('lsp-status/messaging')
3232
-- LSP extensions
3333
local pyls_ms = require('lsp-status/extensions/pyls_ms')
3434
local clangd = require('lsp-status/extensions/clangd')
35-
local extension_callbacks = {
36-
pyls_ms = pyls_ms,
37-
clangd = clangd
38-
}
35+
local extension_callbacks = {pyls_ms = pyls_ms, clangd = clangd}
3936

4037
-- Find current enclosing function
4138
local current_function = require('lsp-status/current_function')
4239

4340
-- Out-of-the-box statusline component
44-
local timer = require('lsp-status/timer')
41+
local redraw = require('lsp-status/redraw')
4542
local statusline = require('lsp-status/statusline')
4643

4744
--- Configure lsp-status.nvim.
@@ -59,7 +56,7 @@ local statusline = require('lsp-status/statusline')
5956
--- - `spinner_frames`: Animation frames for progress spinner in `status`. Default: { '⣾', '⣽', '⣻', '⢿', '⡿', '⣟', '⣯', '⣷' },
6057
--- - `status_symbol`: Symbol to start the statusline segment in `status`. Default: ' 🇻',
6158
---
62-
--@param config: (required, table) Table of values; keys are as listed above. Accept defaults by
59+
-- @param config: (required, table) Table of values; keys are as listed above. Accept defaults by
6360
--- omitting the relevant key.
6461
local function config(user_config)
6562
_config = vim.tbl_extend('keep', user_config, _config, default_config)
@@ -68,7 +65,7 @@ local function config(user_config)
6865
messaging._init(messages, _config)
6966
if _config.current_function then current_function._init(messages, _config) end
7067
statusline._init(messages, _config)
71-
timer._init(messages, _config)
68+
redraw._init(messages, _config)
7269
statusline = vim.tbl_extend('keep', statusline, statusline._get_component_functions())
7370
end
7471

@@ -77,25 +74,22 @@ end
7774
--- registers the server with `lsp-status` for progress message handling and current function
7875
--- updating
7976
---
80-
--@param client: (required, vim.lsp.client)
77+
-- @param client: (required, vim.lsp.client)
8178
local function on_attach(client)
8279
-- Register the client for messages
8380
messaging.register_client(client.id, client.name)
8481
vim.api.nvim_command('augroup lsp_aucmds')
8582
vim.api.nvim_command('au! * <buffer>')
86-
vim.api.nvim_command('au User LspDiagnosticsChanged let g:lsp_status_redraw = v:true')
83+
vim.api.nvim_command('au User LspDiagnosticsChanged lua require("lsp-status/redraw").redraw()')
8784

8885
-- If the client is a documentSymbolProvider, set up an autocommand
8986
-- to update the containing symbol
9087
if _config.current_function and client.resolved_capabilities.document_symbol then
9188
vim.api.nvim_command(
92-
'au CursorHold <buffer> lua require("lsp-status").update_current_function()'
93-
)
89+
'au CursorHold <buffer> lua require("lsp-status").update_current_function()')
9490
end
9591

9692
vim.api.nvim_command('augroup END')
97-
98-
timer.register_timer()
9993
end
10094

10195
config(_config)
@@ -130,7 +124,7 @@ end
130124
--- Normal messages are tables of the form
131125
--- `{ name = Server name, content = Message contents }`
132126
---
133-
--@returns list of messages
127+
-- @returns list of messages
134128
local function messages() -- luacheck: no unused
135129
error()
136130
end
@@ -144,8 +138,8 @@ end
144138
--- Register a new server to receive messages.
145139
--- Generally, you don't need to call this manually - `on_attach` sets it up for you
146140
---
147-
--@param id: (required, number) Client ID
148-
--@param name: (required, string) Client name
141+
-- @param id: (required, number) Client ID
142+
-- @param name: (required, string) Client name
149143
local function register_client(id, name) -- luacheck: no unused
150144
error()
151145
end
@@ -156,7 +150,7 @@ end
156150
--- Usable out of the box, but intended more as an example/template for modification to customize
157151
--- to your own needs
158152
---
159-
--@returns: statusline component string
153+
-- @returns: statusline component string
160154
local function status() -- luacheck: no unused
161155
error()
162156
end

lua/lsp-status/current_function.lua

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
-- Utilities
22
local lsp_util = require('vim.lsp.util')
33
local util = require('lsp-status/util')
4+
local redraw = require('lsp-status/redraw')
45

56
local _config = {}
67

@@ -21,7 +22,7 @@ local function current_function_callback(_, _, result, _, _)
2122
end)
2223

2324
if not function_symbols or #function_symbols == 0 then
24-
vim.g.lsp_status_redraw = true
25+
redraw.redraw()
2526
return
2627
end
2728

@@ -38,7 +39,7 @@ local function current_function_callback(_, _, result, _, _)
3839
end
3940

4041
vim.b.lsp_current_function = fn_name
41-
vim.g.lsp_status_redraw = true
42+
redraw.redraw()
4243
return
4344
end
4445
end

lua/lsp-status/extensions/clangd.lua

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
local util = require('lsp-status/util')
2+
local redraw = require('lsp-status/redraw')
23

34
local messages = {}
45

@@ -12,7 +13,7 @@ local handlers = {
1213
['textDocument/clangd.fileStatus'] = function(_, _, statusMessage, client_id)
1314
ensure_init(client_id)
1415
messages[client_id].status = {uri = statusMessage.uri, content = statusMessage.state}
15-
vim.g.lsp_status_redraw = true
16+
redraw.redraw()
1617
end
1718
}
1819

lua/lsp-status/extensions/pyls_ms.lua

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
local util = require('lsp-status/util')
2-
local statusline = require('lsp-status/statusline')
2+
local redraw = require('lsp-status/redraw')
33

44
local messages = {}
55
---@private
@@ -12,7 +12,7 @@ local handlers = {
1212
['python/setStatusBarMessage'] = function(_, _, message, client_id)
1313
ensure_init(client_id)
1414
messages[client_id].static_message = {content = message[1]}
15-
vim.g.lsp_status_redraw = true
15+
redraw.redraw()
1616
end,
1717
['python/beginProgress'] = function(_, _, _, client_id)
1818
ensure_init(client_id)
@@ -23,11 +23,11 @@ local handlers = {
2323
['python/reportProgress'] = function(_, _, message, client_id)
2424
messages[client_id].progress[1].spinner = messages[client_id].progress[1].spinner + 1
2525
messages[client_id].progress[1].title = message[1]
26-
vim.g.lsp_status_redraw = true
26+
redraw.redraw()
2727
end,
2828
['python/endProgress'] = function(_, _, _, client_id)
2929
messages[client_id].progress[1] = nil
30-
vim.g.lsp_status_redraw = true
30+
redraw.redraw()
3131
end
3232
}
3333

lua/lsp-status/messaging.lua

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
local util = require('lsp-status/util')
2+
local redraw = require('lsp-status/redraw')
23

34
local clients = {}
45
local messages = {}
@@ -30,8 +31,8 @@ local function progress_callback(_, _, msg, client_id)
3031
if messages[client_id].progress[msg.token] == nil then
3132
vim.api.nvim_command('echohl WarningMsg')
3233
vim.api.nvim_command(
33-
'echom "[lsp-status] Received `end` message with no corresponding `begin` from '
34-
.. clients[client_id] .. '!"')
34+
'echom "[lsp-status] Received `end` message with no corresponding `begin` from ' ..
35+
clients[client_id] .. '!"')
3536
vim.api.nvim_command('echohl None')
3637
else
3738
messages[client_id].progress[msg.token].message = val.message
@@ -43,7 +44,7 @@ local function progress_callback(_, _, msg, client_id)
4344
table.insert(messages[client_id], {content = val, show_once = true, shown = 0})
4445
end
4546

46-
vim.g.lsp_status_redraw = true
47+
redraw.redraw()
4748
end
4849

4950
-- Process messages
@@ -93,9 +94,7 @@ local function get_messages()
9394
return new_messages
9495
end
9596

96-
local function register_progress()
97-
vim.lsp.handlers['$/progress'] = progress_callback
98-
end
97+
local function register_progress() vim.lsp.handlers['$/progress'] = progress_callback end
9998

10099
-- Client registration for messages
101100
local function register_client(id, name)

lua/lsp-status/redraw.lua

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
local config = {}
2+
local function init(_, _config) config = vim.tbl_extend('force', config, _config) end
3+
4+
local last_redraw = nil
5+
local timer_going = false
6+
7+
local function redraw_callback(now)
8+
last_redraw = now or vim.loop.now()
9+
vim.api.nvim_command('redrawstatus!')
10+
timer_going = false
11+
end
12+
13+
local wrapped_redraw_callback = vim.schedule_wrap(redraw_callback)
14+
local timer = vim.loop.new_timer()
15+
local function redraw()
16+
local now = vim.loop.now()
17+
if last_redraw == nil or now - last_redraw >= config.update_interval then
18+
vim.loop.timer_stop(timer)
19+
timer_going = false
20+
redraw_callback(now)
21+
elseif not timer_going then
22+
timer_going = true
23+
vim.loop.timer_start(timer, config.update_interval + last_redraw - now, 0,
24+
wrapped_redraw_callback)
25+
end
26+
end
27+
28+
local M = {redraw = redraw, _init = init}
29+
30+
return M

lua/lsp-status/statusline.lua

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -118,13 +118,9 @@ local function get_component_functions()
118118
return {errors = _errors, warnings = _warnings, hints = _hints, info = _info}
119119
end
120120

121-
-- Status line component for nvim-lsp
122-
local function lsp_status() return vim.g.lsp_status_statusline or '' end
123-
124121
local M = {
125122
_init = init,
126-
status = lsp_status,
127-
get_lsp_statusline = get_lsp_statusline,
123+
status = get_lsp_statusline,
128124
_get_component_functions = get_component_functions
129125
}
130126

lua/lsp-status/timer.lua

Lines changed: 0 additions & 53 deletions
This file was deleted.

0 commit comments

Comments
 (0)