Skip to content

Commit 6d36c7d

Browse files
committed
feat: unify guard info with healthcheck
1 parent 46649a9 commit 6d36c7d

File tree

3 files changed

+63
-74
lines changed

3 files changed

+63
-74
lines changed

lua/guard/api.lua

+3-59
Original file line numberDiff line numberDiff line change
@@ -33,8 +33,8 @@ end
3333
---@param bufnr number?
3434
function M.disable_fmt(bufnr)
3535
local buf = bufnr or api.nvim_get_current_buf()
36-
vim.iter(events.get_format_autocmds(buf)):each(function(x)
37-
api.nvim_del_autocmd(x)
36+
vim.iter(events.get_format_autocmds(buf)):each(function(it)
37+
api.nvim_del_autocmd(it.id)
3838
end)
3939
events.user_fmt_autocmds[vim.bo[buf].ft] = {}
4040
end
@@ -64,63 +64,7 @@ end
6464

6565
---Show guard info for current buffer
6666
function M.info()
67-
local util = require('guard.util')
68-
local buf = api.nvim_get_current_buf()
69-
local ft = require('guard.filetype')[vim.bo[buf].ft] or {}
70-
local formatters = ft.formatter or {}
71-
local linters = ft.linter or {}
72-
local fmtau = events.get_format_autocmds(buf)
73-
local lintau = events.get_lint_autocmds(buf)
74-
75-
util.open_info_win()
76-
local lines = {
77-
'# Guard info (press Esc or q to close)',
78-
'## Settings:',
79-
('- `fmt_on_save`: %s'):format(util.getopt('fmt_on_save')),
80-
('- `lsp_as_default_formatter`: %s'):format(util.getopt('lsp_as_default_formatter')),
81-
('- `save_on_fmt`: %s'):format(util.getopt('save_on_fmt')),
82-
'',
83-
('## Current buffer has filetype %s:'):format(vim.bo[buf].ft),
84-
('- %s formatter autocmds attached'):format(#fmtau),
85-
('- %s linter autocmds attached'):format(#lintau),
86-
'- formatters:',
87-
'',
88-
}
89-
90-
if #formatters > 0 then
91-
vim.list_extend(lines, { '', '```lua' })
92-
vim.list_extend(
93-
lines,
94-
vim
95-
.iter(formatters)
96-
:map(function(formatter)
97-
return vim.split(vim.inspect(formatter), '\n', { trimempty = true })
98-
end)
99-
:flatten()
100-
:totable()
101-
)
102-
vim.list_extend(lines, { '```', '' })
103-
end
104-
105-
vim.list_extend(lines, { '- linters:' })
106-
107-
if #linters > 0 then
108-
vim.list_extend(lines, { '', '```lua' })
109-
vim.list_extend(
110-
lines,
111-
vim
112-
.iter(linters)
113-
:map(function(linter)
114-
return vim.split(vim.inspect(linter), '\n', { trimempty = true })
115-
end)
116-
:flatten()
117-
:totable()
118-
)
119-
vim.list_extend(lines, { '```' })
120-
end
121-
122-
api.nvim_buf_set_lines(0, 0, -1, true, lines)
123-
api.nvim_set_option_value('modifiable', false, { buf = 0 })
67+
vim.cmd('checkhealth guard')
12468
end
12569

12670
return M

lua/guard/events.lua

+9-7
Original file line numberDiff line numberDiff line change
@@ -51,17 +51,19 @@ local function maybe_fill_auoption(opt, cb)
5151
end
5252

5353
---@param bufnr number
54-
---@return number[]
54+
---@return vim.api.keyset.get_autocmds.ret[]
5555
function M.get_format_autocmds(bufnr)
5656
if not api.nvim_buf_is_valid(bufnr) then
5757
return {}
5858
end
59-
return M.user_fmt_autocmds[vim.bo[bufnr].ft]
60-
or iter(api.nvim_get_autocmds({ group = M.group, event = 'BufWritePre', buffer = bufnr })):map(
61-
function(it)
62-
return it.id
63-
end
64-
)
59+
local caus = M.user_fmt_autocmds[vim.bo[bufnr].ft]
60+
return caus
61+
and iter(api.nvim_get_autocmds({ group = M.group }))
62+
:filter(function(it)
63+
return vim.tbl_contains(caus, it.id)
64+
end)
65+
:totable()
66+
or api.nvim_get_autocmds({ group = M.group, event = 'BufWritePre', buffer = bufnr })
6567
end
6668

6769
---@param bufnr number

lua/guard/health.lua

+51-8
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
local fn, health = vim.fn, vim.health
2+
local ok, error, info = health.ok, health.error, health.info
23
local filetype = require('guard.filetype')
3-
local ok, error = health.ok, health.error
4+
local util = require('guard.util')
5+
local events = require('guard.events')
46
local M = {}
57

68
local function executable_check()
@@ -27,19 +29,60 @@ local function executable_check()
2729
table.insert(checked, conf.cmd)
2830
end
2931
end
30-
if pcall(require, 'mason') then
31-
health.warn(
32-
'It seems that mason.nvim is installed,'
33-
.. 'in which case checkhealth may be inaccurate.'
34-
.. ' Please add your mason bin path to PATH to avoid potential issues.'
35-
)
36-
end
3732
end
33+
if pcall(require, 'mason') then
34+
health.warn(
35+
'It seems that mason.nvim is installed,'
36+
.. 'in which case checkhealth may be inaccurate.'
37+
.. ' Please add your mason bin path to PATH to avoid potential issues.'
38+
)
39+
end
40+
end
41+
42+
local function dump_settings()
43+
ok(('fmt_on_save: %s'):format(util.getopt('fmt_on_save')))
44+
ok(('lsp_as_default_formatter: %s'):format(util.getopt('lsp_as_default_formatter')))
45+
ok(('save_on_fmt: %s'):format(util.getopt('save_on_fmt')))
3846
end
3947

48+
local function dump_tools(buf)
49+
local fmtau = events.get_format_autocmds(buf)
50+
local lintau = events.get_lint_autocmds(buf)
51+
local ft = vim.bo[buf].ft
52+
53+
ok(('Current buffer has filetype %s:'):format(ft))
54+
info(('%s formatter autocmds attached'):format(#fmtau))
55+
info(('%s linter autocmds attached'):format(#lintau))
56+
57+
local conf = filetype[ft] or {}
58+
local formatters = conf.formatter or {}
59+
local linters = conf.linter or {}
60+
61+
if #formatters > 0 then
62+
info('formatters:')
63+
vim.iter(formatters):map(vim.inspect):each(info)
64+
end
65+
66+
if #linters > 0 then
67+
info('formatters:')
68+
vim.iter(linters):map(vim.inspect):each(info)
69+
end
70+
end
71+
72+
-- TODO: add custom autocmds info
4073
M.check = function()
4174
health.start('Executable check')
4275
executable_check()
76+
77+
health.start('Settings')
78+
dump_settings()
79+
80+
local orig_bufnr = vim.fn.bufnr('#')
81+
if not vim.api.nvim_buf_is_valid(orig_bufnr) then
82+
return
83+
end
84+
health.start(('Tools registered for current buffer (bufnr %s)'):format(orig_bufnr))
85+
dump_tools(orig_bufnr)
4386
end
4487

4588
return M

0 commit comments

Comments
 (0)