Skip to content

Commit 925acda

Browse files
authored
functions for statusline components (#39)
* functions for statusline components * some accidental Js * add bufh param * removing an extra space after icon * [WIP] statusline component factory function * call factory function in init * add component functions to statusline after config init * adding spaces after the icons that need it * remove icon param * replace hard coded space in favor of indicator_separator
1 parent 187c492 commit 925acda

File tree

2 files changed

+45
-4
lines changed

2 files changed

+45
-4
lines changed

lua/lsp-status.lua

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,7 @@ local function config(user_config)
6666
messaging._init(messages, _config)
6767
if _config.current_function then current_function._init(messages, _config) end
6868
statusline._init(messages, _config)
69+
statusline = vim.tbl_extend('keep', statusline, statusline._get_component_functions())
6970
end
7071

7172
--- Register a new server for messages.
@@ -176,6 +177,10 @@ local M = {
176177
config = config,
177178
on_attach = on_attach,
178179
status = statusline.status,
180+
status_errors = statusline.errors,
181+
status_warnings = statusline.warnings,
182+
status_info = statusline.info,
183+
status_hints = statusline.hints,
179184
capabilities = messaging.capabilities
180185
}
181186

lua/lsp-status/statusline.lua

Lines changed: 40 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,41 @@
11
local config = {}
22

3-
local function init(_, _config)
4-
config = vim.tbl_extend('force', config, _config)
5-
end
3+
local _errors
4+
local _warnings
5+
local _hints
6+
local _info
7+
local icons
68

79
local diagnostics = require('lsp-status/diagnostics')
810
local messages = require('lsp-status/messaging').messages
911
local aliases = {
1012
pyls_ms = 'MPLS',
1113
}
1214

15+
local function make_statusline_component(diagnostics_key)
16+
return function(bufh)
17+
bufh = bufh or vim.api.nvim_get_current_buf()
18+
local icon = icons[diagnostics_key] .. config.indicator_separator
19+
return (icon or '') .. diagnostics(bufh)[diagnostics_key]
20+
end
21+
end
22+
23+
local function init(_, _config)
24+
config = vim.tbl_extend('force', config, _config)
25+
26+
icons = {
27+
errors = config.indicator_errors,
28+
warnings = config.indicator_warnings,
29+
hints = config.indicator_hint,
30+
info = config.indicator_info,
31+
}
32+
33+
_errors = make_statusline_component('errors')
34+
_warnings = make_statusline_component('warnings')
35+
_hints = make_statusline_component('hints')
36+
_info = make_statusline_component('info')
37+
end
38+
1339
local function statusline_lsp(bufnr)
1440
bufnr = bufnr or 0
1541
if #vim.lsp.buf_get_clients(bufnr) == 0 then
@@ -97,9 +123,19 @@ local function statusline_lsp(bufnr)
97123
return symbol .. config.indicator_ok .. ' '
98124
end
99125

126+
local function get_component_functions()
127+
return {
128+
errors = _errors,
129+
warnings = _warnings,
130+
hints = _hints,
131+
info = _info,
132+
}
133+
end
134+
100135
local M = {
101136
_init = init,
102-
status = statusline_lsp
137+
status = statusline_lsp,
138+
_get_component_functions = get_component_functions
103139
}
104140

105141
return M

0 commit comments

Comments
 (0)