Skip to content

Commit 2e395e2

Browse files
committed
perf(components.lsp_servers): be async
1 parent 4defc24 commit 2e395e2

2 files changed

Lines changed: 58 additions & 46 deletions

File tree

doc/bareline.txt

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -213,8 +213,8 @@ See: |bareline-control-stl-redraws|.
213213
Control statusline redraws ~
214214

215215
Bareline does not use a timer to redraw the statusline, instead it uses:
216-
1. |autocmd|s. See `redraw_on_autocmd` in |bareline-BareComponentCommonOpts|.
217-
2. |uv| file watchers. See |bareline.redraw_on_fs_event()|.
216+
1. |autocmd|s. See |bareline-BareComponentCommonOpts|, `register_redraw_on_autocmd`.
217+
2. |uv| file watchers. See |bareline.register_redraw_on_fs_event()|.
218218

219219
These are the base autocmds used to redraw the stl:
220220
|BufEnter|
@@ -229,8 +229,8 @@ These are the base autocmds used to redraw the stl:
229229
With the default config, these are the fs paths watched to redraw the stl:
230230
• Git repository directories to fulfill |bareline.components.git_head|.
231231

232-
*bareline.redraw_on_fs_event()*
233-
`bareline.redraw_on_fs_event`({fs_path}, {var_name})
232+
*bareline.register_redraw_on_fs_event()*
233+
`bareline.register_redraw_on_fs_event`({fs_path}, {var_name})
234234
Conditionally create a |uv_fs_event_t| to monitor `fs_path` for changes. When
235235
a change is detected, redraw the statusline of the current window. If a luv fs
236236
event handle already exists for the `fs_path`, then do nothing.
@@ -262,8 +262,8 @@ Class ~
262262
Fields ~
263263
{callback} `(fun(opts:BareComponentCommonOpts, callback:fun(var_value:any))?)`
264264
TODO: Write docs.
265-
{redraw_on_autocmd} `(table?)` Expects a table with the keys `event` and
266-
`opts`. These values are passed as-is to |vim.api.nvim_create_autocmd()|.
265+
{register_redraw_on_autocmd} `(table?)` Expects a table with the keys `event`
266+
and `opts`. These values are passed as-is to |vim.api.nvim_create_autocmd()|.
267267
{cache_on_vim_modes} `((string[]|fun():string[])|nil)` Use cache in these Vim
268268
modes. Each Vim mode is expected as the first char returned by |mode()|.
269269
{mask} `(string?)` Single character used to mask the value.

lua/bareline.lua

Lines changed: 52 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -335,8 +335,8 @@ end
335335
--- Control statusline redraws ~
336336
---
337337
--- Bareline does not use a timer to redraw the statusline, instead it uses:
338-
--- 1. |autocmd|s. See `redraw_on_autocmd` in |bareline-BareComponentCommonOpts|.
339-
--- 2. |uv| file watchers. See |bareline.redraw_on_fs_event()|.
338+
--- 1. |autocmd|s. See |bareline-BareComponentCommonOpts|, `register_redraw_on_autocmd`.
339+
--- 2. |uv| file watchers. See |bareline.register_redraw_on_fs_event()|.
340340
---
341341
--- These are the base autocmds used to redraw the stl:
342342
--- * |BufEnter|
@@ -356,7 +356,7 @@ end
356356
--- event handle already exists for the `fs_path`, then do nothing.
357357
---@param fs_path string Full or relative path to a dir or file.
358358
---@param var_name string
359-
function bareline.redraw_on_fs_event(fs_path, var_name)
359+
function bareline.register_redraw_on_fs_event(fs_path, var_name)
360360
local fs_path_absolute = vim.uv.fs_realpath(fs_path)
361361
if
362362
fs_path_absolute ~= nil
@@ -406,8 +406,8 @@ bareline.BareComponent["__index"] = bareline.BareComponent
406406
---@class BareComponentCommonOpts
407407
---@field callback fun(opts:BareComponentCommonOpts, callback:fun(var_value:any))?
408408
--- TODO: Write docs.
409-
---@field redraw_on_autocmd table? Expects a table with the keys `event` and
410-
--- `opts`. These values are passed as-is to |vim.api.nvim_create_autocmd()|.
409+
---@field register_redraw_on_autocmd table? Expects a table with the keys `event`
410+
--- and `opts`. These values are passed as-is to |vim.api.nvim_create_autocmd()|.
411411
---@field cache_on_vim_modes (string[]|fun():string[])|nil Use cache in these Vim
412412
--- modes. Each Vim mode is expected as the first char returned by |mode()|.
413413
---@field mask string? Single character used to mask the value.
@@ -488,7 +488,7 @@ bareline.components.vim_mode = bareline.BareComponent:new(function()
488488
}
489489
return mode_labels[vim_mode]:upper()
490490
end, {
491-
redraw_on_autocmd = {
491+
register_redraw_on_autocmd = {
492492
event = "ModeChanged",
493493
},
494494
})
@@ -527,7 +527,7 @@ bareline.components.end_of_line = bareline.BareComponent:new(function()
527527
return "noeol"
528528
end, {})
529529

530-
local component_git_head_value = "bareline_git_head"
530+
local component_git_head_var_name = "bareline_git_head"
531531

532532
--- Git HEAD.
533533
---
@@ -569,7 +569,7 @@ local component_git_head_value = "bareline_git_head"
569569
--- Mockup: `(main)`
570570
---@type BareComponent
571571
bareline.components.git_head =
572-
bareline.BareComponent:new(component_git_head_value, {
572+
bareline.BareComponent:new(component_git_head_var_name, {
573573
callback = function(opts, callback)
574574
local filepath = vim.api.nvim_buf_get_name(0)
575575
if filepath == "" then
@@ -593,7 +593,10 @@ bareline.components.git_head =
593593
function(is_filepath_tracked)
594594
if is_filepath_tracked then
595595
-- Found standard repo or work tree from `git worktree add`.
596-
bareline.redraw_on_fs_event(gitdir, component_git_head_value)
596+
bareline.register_redraw_on_fs_event(
597+
gitdir,
598+
component_git_head_var_name
599+
)
597600
h.providers.git_head.get_pretty_head(
598601
gitdir,
599602
function(pretty_head)
@@ -606,9 +609,9 @@ bareline.components.git_head =
606609
function(should_show_untracked)
607610
if should_show_untracked then
608611
-- Found standard repo or work tree from `git worktree add`.
609-
bareline.redraw_on_fs_event(
612+
bareline.register_redraw_on_fs_event(
610613
gitdir,
611-
component_git_head_value
614+
component_git_head_var_name
612615
)
613616
h.providers.git_head.get_pretty_head(
614617
gitdir,
@@ -638,9 +641,9 @@ bareline.components.git_head =
638641
function(is_filepath_tracked)
639642
if is_filepath_tracked then
640643
-- Found work tree from `worktrees` custom component opt.
641-
bareline.redraw_on_fs_event(
644+
bareline.register_redraw_on_fs_event(
642645
worktree.gitdir,
643-
component_git_head_value
646+
component_git_head_var_name
644647
)
645648
h.providers.git_head.get_pretty_head(
646649
worktree.gitdir,
@@ -654,9 +657,9 @@ bareline.components.git_head =
654657
function(should_show_untracked)
655658
if should_show_untracked then
656659
-- Found work tree from `worktrees` custom component opt.
657-
bareline.redraw_on_fs_event(
660+
bareline.register_redraw_on_fs_event(
658661
worktree.gitdir,
659-
component_git_head_value
662+
component_git_head_var_name
660663
)
661664
h.providers.git_head.get_pretty_head(
662665
worktree.gitdir,
@@ -678,8 +681,8 @@ bareline.components.git_head =
678681
end)
679682
)
680683
end,
681-
redraw_on_autocmd = {
682-
var_name = component_git_head_value,
684+
register_redraw_on_autocmd = {
685+
var_name = component_git_head_var_name,
683686
event = {
684687
"VimResume",
685688
"FocusGained",
@@ -690,21 +693,30 @@ bareline.components.git_head =
690693
},
691694
})
692695

696+
local component_lsp_servers_var_name = "bareline_lsp_servers"
697+
693698
--- LSP servers.
694699
--- The LSP servers attached to the current buffer.
695700
--- Mockup: `[lua_ls]`
696701
---@type BareComponent
697-
bareline.components.lsp_servers = bareline.BareComponent:new(function()
698-
local lsp_servers = h.providers.lsp_servers.get_names()
699-
if lsp_servers == nil or vim.tbl_isempty(lsp_servers) then
700-
return nil
701-
end
702-
return "[" .. vim.fn.join(lsp_servers, ",") .. "]"
703-
end, {
704-
redraw_on_autocmd = {
705-
event = { "LspAttach", "LspDetach" },
706-
},
707-
})
702+
bareline.components.lsp_servers =
703+
bareline.BareComponent:new(component_lsp_servers_var_name, {
704+
callback = function(_, callback)
705+
h.providers.lsp_servers.get_names(function(lsp_servers)
706+
if lsp_servers == nil or vim.tbl_isempty(lsp_servers) then
707+
callback(nil)
708+
else
709+
callback("[" .. vim.fn.join(lsp_servers, ",") .. "]")
710+
end
711+
end)
712+
end,
713+
{
714+
register_redraw_on_autocmd = {
715+
var_name = component_lsp_servers_var_name,
716+
event = { "LspAttach", "LspDetach" },
717+
},
718+
},
719+
})
708720

709721
--- Stable `%f`.
710722
--- If the file is in the cwd (|:pwd|) at any depth level, the filepath relative
@@ -755,7 +767,7 @@ bareline.components.diagnostics = bareline.BareComponent:new(function()
755767
end
756768
return string.sub(output, 1, #output - 1)
757769
end, {
758-
redraw_on_autocmd = {
770+
register_redraw_on_autocmd = {
759771
event = "DiagnosticChanged",
760772
},
761773
cache_on_vim_modes = function()
@@ -971,14 +983,14 @@ end
971983

972984
h.providers.lsp_servers = {}
973985

974-
--- LSP attached servers.
975-
--- Returns the names of the LSP servers attached to the current buffer.
976-
--- Example output: `{ "lua_ls" }`
977-
---@return table
978-
function h.providers.lsp_servers.get_names()
979-
return vim.tbl_map(function(client)
980-
return client.name
981-
end, vim.lsp.get_clients({ bufnr = 0 }))
986+
---@param callback fun(lsp_servers:string[]) Example `lsp_servers`: `{"lua_ls"}`.
987+
function h.providers.lsp_servers.get_names(callback)
988+
vim.defer_fn(function()
989+
local lsp_servers = vim.tbl_map(function(client)
990+
return client.name
991+
end, vim.lsp.get_clients({ bufnr = 0 }))
992+
callback(lsp_servers)
993+
end, 0)
982994
end
983995

984996
h.providers.mhr = {}
@@ -1190,12 +1202,12 @@ function h.create_bare_component_autocmds(statusline)
11901202
if
11911203
type(component) ~= "table"
11921204
or type(component.opts) ~= "table"
1193-
or type(component.opts.redraw_on_autocmd) ~= "table"
1194-
or component.opts.redraw_on_autocmd.event == nil
1205+
or type(component.opts.register_redraw_on_autocmd) ~= "table"
1206+
or component.opts.register_redraw_on_autocmd.event == nil
11951207
then
11961208
return
11971209
end
1198-
local autocmd = component.opts.redraw_on_autocmd
1210+
local autocmd = component.opts.register_redraw_on_autocmd
11991211
autocmd.opts = autocmd.opts or {}
12001212
autocmd.opts.group = h.draw_methods_augroup
12011213
autocmd.opts.callback = function()

0 commit comments

Comments
 (0)