feat(editorconfig): allow editorconfig to be toggled dynamically

Rather than only check `editorconfig_enable` when the plugin is loaded,
check it each time the autocommand fires, so that users may enable or
disable it dynamically.

Also check for a buffer local version of the variable, so that
editorconfig can be enabled or disabled per-buffer.
This commit is contained in:
Gregory Anders 2023-01-04 14:36:18 -07:00
parent 42afa0369a
commit 34d1eaa792
2 changed files with 23 additions and 4 deletions

View File

@ -1,11 +1,14 @@
if vim.g.editorconfig_enable == false or vim.g.editorconfig_enable == 0 then
return
end
local group = vim.api.nvim_create_augroup('editorconfig', {})
vim.api.nvim_create_autocmd({ 'BufNewFile', 'BufRead', 'BufFilePost' }, {
group = group,
callback = function(args)
-- Buffer-local enable has higher priority
local enable =
vim.F.if_nil(vim.b.editorconfig_enable, vim.F.if_nil(vim.g.editorconfig_enable, true))
if not enable then
return
end
require('editorconfig').config(args.buf)
end,
})

View File

@ -4,6 +4,8 @@ local command = helpers.command
local eq = helpers.eq
local pathsep = helpers.get_pathsep()
local curbufmeths = helpers.curbufmeths
local funcs = helpers.funcs
local meths = helpers.meths
local testdir = 'Xtest-editorconfig'
@ -191,4 +193,18 @@ But not this one
it('sets textwidth', function()
test_case('max_line_length.txt', { textwidth = 42 })
end)
it('can be disabled globally', function()
meths.set_var('editorconfig_enable', false)
meths.set_option_value('shiftwidth', 42, {})
test_case('3_space.txt', { shiftwidth = 42 })
end)
it('can be disabled per-buffer', function()
meths.set_option_value('shiftwidth', 42, {})
local bufnr = funcs.bufadd(testdir .. pathsep .. '3_space.txt')
meths.buf_set_var(bufnr, 'editorconfig_enable', false)
test_case('3_space.txt', { shiftwidth = 42 })
test_case('4_space.py', { shiftwidth = 4 })
end)
end)