Skip to content

Commit 81e0f0a

Browse files
fix: multi filetype config chaining (#214)
* fix: multi filetype config chaining * chore(doc): auto generate docs * ci: add more tests for filetypes * chore: fix type warning --------- Co-authored-by: github-actions[bot] <github-actions[bot]@users.noreply.github.com>
1 parent 71f580c commit 81e0f0a

File tree

5 files changed

+40
-9
lines changed

5 files changed

+40
-9
lines changed

doc/guard.nvim.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
*guard.nvim.txt* For NVIM v0.8.0 Last change: 2025 June 08
1+
*guard.nvim.txt* For NVIM v0.8.0 Last change: 2025 June 10
22

33
==============================================================================
44
Table of Contents *guard.nvim-table-of-contents*

lua/guard/filetype.lua

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -70,7 +70,9 @@ local function box(ft)
7070
local events = require('guard.events')
7171
for _, it in ipairs(self:ft()) do
7272
if it ~= ft then
73-
M[it] = box(it)
73+
if not getmetatable(M[it]) then
74+
M[it] = box(it)
75+
end
7476
M[it].formatter = self.formatter
7577
end
7678

@@ -82,10 +84,6 @@ local function box(ft)
8284
events.fmt_attach_to_existing(it)
8385
end
8486
end
85-
86-
if ft:find(',') then
87-
M[ft] = nil
88-
end
8987
return self
9088
end
9189

@@ -101,7 +99,10 @@ local function box(ft)
10199
local evs = util.linter_events(config)
102100
for _, it in ipairs(self:ft()) do
103101
if it ~= ft then
104-
M[it] = box(it)
102+
if not getmetatable(M[it]) then
103+
M[it] = box(it)
104+
vim.print('init ' .. it)
105+
end
105106
M[it].linter = self.linter
106107
end
107108

lua/guard/health.lua

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,12 @@ end
1818

1919
local function executable_check()
2020
local checked = {}
21-
for _, item in pairs(filetype) do
21+
for ft, item in pairs(filetype) do
22+
if ft:find(',') then
23+
goto continue
24+
end
25+
26+
-- run executable or custom healt check
2227
for _, conf in ipairs(item.formatter or {}) do
2328
if conf.health then
2429
conf.health()
@@ -33,6 +38,8 @@ local function executable_check()
3338
check_cmd(conf, checked)
3439
end
3540
end
41+
42+
::continue::
3643
end
3744
if pcall(require, 'mason') then
3845
health.warn(

lua/guard/util.lua

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -140,7 +140,7 @@ end
140140
function M.buf_get_info(buf)
141141
local fname = vim.fn.fnameescape(resolve_symlink(api.nvim_buf_get_name(buf)))
142142
---@diagnostic disable-next-line: undefined-field
143-
return fname, M.get_lsp_root() or vim.uv.cwd()
143+
return fname, assert(M.get_lsp_root() or vim.uv.cwd())
144144
end
145145

146146
---@param c (FmtConfig|LintConfig)?

spec/filetype_spec.lua

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -100,6 +100,29 @@ describe('filetype module', function()
100100
}, ft.javascriptreact)
101101
end)
102102

103+
it(
104+
'can register a formatter for multiple filetypes simultaneously while chaining configs',
105+
function()
106+
ft('javascript,javascriptreact')
107+
:fmt({
108+
cmd = 'cat',
109+
args = { '-v', '-E' },
110+
})
111+
:append({ cmd = 'tac' })
112+
:lint({ cmd = 'cat' })
113+
:append({ cmd = 'tac' })
114+
115+
same({
116+
formatter = { { cmd = 'cat', args = { '-v', '-E' } }, { cmd = 'tac' } },
117+
linter = { { cmd = 'cat' }, { cmd = 'tac' } },
118+
}, ft.javascript)
119+
same({
120+
formatter = { { cmd = 'cat', args = { '-v', '-E' } }, { cmd = 'tac' } },
121+
linter = { { cmd = 'cat' }, { cmd = 'tac' } },
122+
}, ft.javascriptreact)
123+
end
124+
)
125+
103126
it('can add extra command arguments', function()
104127
ft('c')
105128
:fmt({

0 commit comments

Comments
 (0)