feat: update filetype pattern check

This commit is contained in:
Matthew Leong 2023-10-18 19:41:16 -07:00
parent af2cef94d3
commit c978fd185e

View file

@ -5,8 +5,10 @@ local group = vim.api.nvim_create_augroup(augroup_name, { clear = true })
local user_config = require('cosmic.core.user') local user_config = require('cosmic.core.user')
function M.on_attach(client, bufnr) function M.on_attach(client, bufnr)
local function buf_set_option(...) local function buf_set_option(name, value)
vim.api.nvim_buf_set_option(bufnr, ...) vim.api.nvim_set_option_value(name, value, {
buf = bufnr,
})
end end
-- Enable completion triggered by <c-x><c-o> -- Enable completion triggered by <c-x><c-o>
@ -24,13 +26,15 @@ function M.on_attach(client, bufnr)
if user_config.lsp.format_on_save then if user_config.lsp.format_on_save then
-- check user config to see if we can format on save -- check user config to see if we can format on save
-- collect filetype(s) from user config -- collect filetype(s) from user config
local filetype_pattern = '' local filetype_patterns = {}
local filetype_allowed = false
if vim.tbl_islist(user_config.lsp.format_on_save) then if vim.tbl_islist(user_config.lsp.format_on_save) then
for _, ft in pairs(user_config.lsp.format_on_save) do for _, ft in pairs(user_config.lsp.format_on_save) do
filetype_pattern = filetype_pattern .. '*' .. ft table.insert(filetype_patterns, '*' .. ft)
end end
else -- any filetype if none set else -- any filetype if none set
filetype_pattern = '*' table.insert(filetype_patterns, '*')
filetype_allowed = true
end end
vim.api.nvim_clear_autocmds({ vim.api.nvim_clear_autocmds({
@ -38,9 +42,16 @@ function M.on_attach(client, bufnr)
buffer = bufnr, buffer = bufnr,
}) })
-- autocommand for format on save with specified filetype(s) -- autocommand for format on save with specified filetype(s)
vim.api.nvim_create_autocmd(string.format('BufWritePre %s', filetype_pattern), { vim.api.nvim_create_autocmd('BufWritePre', {
callback = function() callback = function(ev)
for pattern, _ in pairs(filetype_patterns) do
if string.match(ev.file, pattern) then
filetype_allowed = true
end
end
if filetype_allowed then
require('cosmic.utils.lsp').format(bufnr) require('cosmic.utils.lsp').format(bufnr)
end
end, end,
buffer = bufnr, buffer = bufnr,
group = group, group = group,