Skip to content

Commit a9e5ccb

Browse files
committed
feat(provider): add basic runtime_condition checking support to null-ls provider
1 parent e5c5d45 commit a9e5ccb

File tree

2 files changed

+21
-6
lines changed

2 files changed

+21
-6
lines changed

lua/astroui/status/provider.lua

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -524,8 +524,12 @@ function M.lsp_client_names(opts)
524524
for _, client in pairs((vim.lsp.get_clients or vim.lsp.get_active_clients) { bufnr = bufnr }) do
525525
if client.name == "null-ls" and opts.integrations.null_ls then
526526
local null_ls_sources = {}
527+
local ft = vim.bo.filetype
528+
local params =
529+
{ client_id = client.id, bufname = vim.api.nvim_buf_get_name(bufnr), bufnr = bufnr, filetype = ft, ft = ft }
527530
for _, type in ipairs { "FORMATTING", "DIAGNOSTICS" } do
528-
for _, source in ipairs(status_utils.null_ls_sources(vim.bo.filetype, type)) do
531+
params.method = type
532+
for _, source in ipairs(status_utils.null_ls_sources(ft, type, params)) do
529533
null_ls_sources[source] = true
530534
end
531535
end

lua/astroui/status/utils.lua

Lines changed: 16 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -152,18 +152,28 @@ function M.decode_pos(c) return bit.rshift(c, 16), bit.band(bit.rshift(c, 6), 10
152152

153153
--- Get a list of registered null-ls providers for a given filetype
154154
---@param filetype string the filetype to search null-ls for
155+
---@param params table? optional parameters to use for checking `runtime_condition`
155156
---@return table # a table of null-ls sources
156-
function M.null_ls_providers(filetype)
157+
function M.null_ls_providers(filetype, params)
157158
local registered = {}
158159
-- try to load null-ls
159160
local sources_avail, sources = pcall(require, "null-ls.sources")
160161
if sources_avail then
161162
-- get the available sources of a given filetype
162163
for _, source in ipairs(sources.get_available(filetype)) do
163164
-- get each source name
165+
local runtime_condition = params and vim.tbl_get(source, "generator", "opts", "runtime_condition")
164166
for method in pairs(source.methods) do
165-
registered[method] = registered[method] or {}
166-
table.insert(registered[method], source.name)
167+
local source_activated = true
168+
if runtime_condition then -- try to calculate runtime_condition with supported parameters
169+
params.source_id = vim.tbl_get(source, "generator", "source_id")
170+
local condition_calculated, condition = pcall(runtime_condition, params)
171+
if condition_calculated then source_activated = condition == true end
172+
end
173+
if source_activated then
174+
registered[method] = registered[method] or {}
175+
table.insert(registered[method], source.name)
176+
end
167177
end
168178
end
169179
end
@@ -174,10 +184,11 @@ end
174184
--- Get the null-ls sources for a given null-ls method
175185
---@param filetype string the filetype to search null-ls for
176186
---@param method string the null-ls method (check null-ls documentation for available methods)
187+
---@param params table? optional parameters to use for checking `runtime_condition`
177188
---@return string[] # the available sources for the given filetype and method
178-
function M.null_ls_sources(filetype, method)
189+
function M.null_ls_sources(filetype, method, params)
179190
local methods_avail, methods = pcall(require, "null-ls.methods")
180-
return methods_avail and M.null_ls_providers(filetype)[methods.internal[method]] or {}
191+
return methods_avail and M.null_ls_providers(filetype, params)[methods.internal[method]] or {}
181192
end
182193

183194
--- A helper function for decoding statuscolumn click events with mouse click pressed, modifier keys, as well as which signcolumn sign was clicked if any

0 commit comments

Comments
 (0)