Skip to content

Commit

Permalink
feat: use health check for status command
Browse files Browse the repository at this point in the history
  • Loading branch information
Saghen committed Jan 11, 2025
1 parent 86c9676 commit fe9b851
Show file tree
Hide file tree
Showing 2 changed files with 49 additions and 35 deletions.
34 changes: 1 addition & 33 deletions lua/blink/cmp/commands.lua
Original file line number Diff line number Diff line change
@@ -1,41 +1,9 @@
local commands = {}

function commands.status()
local sources = require('blink.cmp.sources.lib')
local all_providers = sources.get_all_providers()
local enabled_provider_ids = sources.get_enabled_provider_ids('default')

--- @type string[]
local not_enabled_provider_ids = {}
for provider_id, _ in pairs(all_providers) do
if not vim.list_contains(enabled_provider_ids, provider_id) then
table.insert(not_enabled_provider_ids, provider_id)
end
end

if #enabled_provider_ids > 0 then
vim.api.nvim_echo({ { '\n', 'Normal' } }, false, {})
vim.api.nvim_echo({ { '# enabled sources providers\n', 'Special' } }, false, {})

for _, provider_id in ipairs(enabled_provider_ids) do
vim.api.nvim_echo({ { ('- %s\n'):format(provider_id), 'Normal' } }, false, {})
end
end

if #not_enabled_provider_ids > 0 then
vim.api.nvim_echo({ { '\n', 'Normal' } }, false, {})
vim.api.nvim_echo({ { '# not enabled sources providers\n', 'Comment' } }, false, {})

for _, provider_id in pairs(not_enabled_provider_ids) do
vim.api.nvim_echo({ { ('- %s\n'):format(provider_id), 'Normal' } }, false, {})
end
end
end

function commands.setup()
vim.api.nvim_create_user_command('BlinkCmp', function(cmd)
if cmd.fargs[1] == 'status' then
commands.status()
vim.cmd('checkhealth blink.cmp')
else
vim.notify("[blink.cmp] invalid command '" .. cmd.args .. "'", vim.log.levels.ERROR)
end
Expand Down
50 changes: 48 additions & 2 deletions lua/blink/cmp/health.lua
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
local health = {}

function health.check()
vim.health.start('blink.cmp healthcheck')
function health.report_system()
vim.health.start('System')

local required_executables = { 'curl', 'git' }
for _, executable in ipairs(required_executables) do
Expand Down Expand Up @@ -32,4 +32,50 @@ function health.check()
end
end

function health.report_sources()
vim.health.start('Sources')

local sources = require('blink.cmp.sources.lib')

local all_providers = sources.get_all_providers()
local default_providers = sources.get_enabled_provider_ids('default')
local cmdline_providers = sources.get_enabled_provider_ids('cmdline')

local bufnr = vim.api.nvim_create_buf(false, true)
vim.bo[bufnr].filetype = 'checkhealth'

vim.health.warn('Some providers may show up as "disabled" but are enabled dynamically (i.e. cmdline)')

--- @type string[]
local disabled_providers = {}
for provider_id, _ in pairs(all_providers) do
if
not vim.list_contains(default_providers, provider_id) and not vim.list_contains(cmdline_providers, provider_id)
then
table.insert(disabled_providers, provider_id)
end
end

health.report_sources_list('Default sources', default_providers)
health.report_sources_list('Cmdline sources', cmdline_providers)
health.report_sources_list('Disabled sources', disabled_providers)
end

--- @param header string
--- @param provider_ids string[]
function health.report_sources_list(header, provider_ids)
if #provider_ids == 0 then return end

vim.health.start(header)
local all_providers = require('blink.cmp.sources.lib').get_all_providers()
for _, provider_id in ipairs(provider_ids) do
vim.health.info(('%s (%s)'):format(provider_id, all_providers[provider_id].config.module))
end
end

function health.check()
health.report_system()
health.report_sources()
end

return health

0 comments on commit fe9b851

Please sign in to comment.