Skip to content

Commit

Permalink
feat: support register config from setup
Browse files Browse the repository at this point in the history
Co-authored-by: mrjones2014 <[email protected]>
close #11
  • Loading branch information
glepnir committed Jul 18, 2023
1 parent 03b0582 commit 65110bc
Show file tree
Hide file tree
Showing 4 changed files with 104 additions and 2 deletions.
30 changes: 30 additions & 0 deletions lua/guard/filetype.lua
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,36 @@ local function box()
return self
end

function tbl:key_alias(key)
local _t = {
['lint'] = function()
self.linter = {}
return self.linter
end,
['fmt'] = function()
self.format = {}
return self.format
end,
}
return _t[key]()
end

function tbl:register(key, cfg)
vim.validate({
key = {
key,
function(val)
local available = { 'lint', 'fmt' }
return vim.tbl_contains(available, val)
end,
},
})
local target = self:key_alias(key)
for _, item in ipairs(cfg) do
target[#target + 1] = vim.deepcopy(item)
end
end

return setmetatable({}, tbl)
end

Expand Down
18 changes: 17 additions & 1 deletion lua/guard/init.lua
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
local api = vim.api
local group = api.nvim_create_augroup('Guard', { clear = true })
local fts_config = require('guard.filetype')
local util = require('guard.util')

local function register_event(fts)
api.nvim_create_autocmd('FileType', {
Expand All @@ -22,11 +24,25 @@ local function register_event(fts)
end, {})
end

local function parse_setup_cfg(fts_with_cfg)
for ft, cfg in pairs(fts_with_cfg or {}) do
if not vim.tbl_isempty(cfg) then
local handler = fts_config(ft)
local keys = vim.tbl_keys(cfg)
vim.tbl_map(function(key)
handler:register(key, util.as_table(cfg[key]))
end, keys)
end
end
end

local function setup(opt)
opt = opt or {
fmt_on_save = true,
}
local fts_config = require('guard.filetype')

parse_setup_cfg(opt.ft)

if opt.fmt_on_save then
register_event(vim.tbl_keys(fts_config))
end
Expand Down
8 changes: 7 additions & 1 deletion lua/guard/util.lua
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
---@diagnostic disable-next-line: deprecated
local get_clients = vim.version().minor >= 10 and vim.lsp.get_clients or vim.lsp.get_active_clients
local api = vim.api
local util = {}

Expand All @@ -12,7 +14,7 @@ end

function util.get_lsp_root()
local curbuf = api.nvim_get_current_buf()
local clients = vim.lsp.get_active_clients({ bufnr = curbuf })
local clients = get_clients({ bufnr = curbuf })
if #clients == 0 then
return
end
Expand All @@ -23,4 +25,8 @@ function util.get_lsp_root()
end
end

function util.as_table(t)
return vim.tbl_islist(t) and t or { t }
end

return util
50 changes: 50 additions & 0 deletions test/filetype_spec.lua
Original file line number Diff line number Diff line change
Expand Up @@ -87,4 +87,54 @@ describe('filetype module', function()
},
}, ft.python)
end)

it('can setup filetypes via setup()', function()
require('guard').setup({
ft = {
c = {
fmt = {
cmd = 'clang-format',
lines = { 'test', 'lines' },
},
},
python = {
fmt = {
{ cmd = 'tool-1' },
{ cmd = 'tool-2' },
},
lint = {
cmd = 'lint_tool_1',
},
},
rust = {
lint = {
{
cmd = 'clippy',
args = { 'check' },
stdin = true,
},
},
},
},
})
same({
format = {
{ cmd = 'clang-format', lines = { 'test', 'lines' } },
},
}, ft.c)
same({
format = {
{ cmd = 'tool-1' },
{ cmd = 'tool-2' },
},
linter = {
{ cmd = 'lint_tool_1' },
},
}, ft.python)
same({
linter = {
{ cmd = 'clippy', args = { 'check' }, stdin = true },
},
}, ft.rust)
end)
end)

0 comments on commit 65110bc

Please sign in to comment.