-
Notifications
You must be signed in to change notification settings - Fork 195
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
feat: support tables as keys in setup({ formatters_by_ft }) #627
Conversation
Add support for the following syntax: ```lua require("conform").setup({ formatters_by_ft = { -- Setup function now supports tables as keys for opts.formatters_by_ft [{ "sh", "bash", "zsh" }] = { "beautysh" }, }, }) ``` Function `setup()` unmarshals `opts.formatters_by_ft table` and assigns it to `M.formatters_by_ft`. Values are copied using `vim.deepcopy` to allow further overriding. This functionality works exclusively within the setup function.
…ng tables as keys
I appreciate the work that went into this, but I'm not interested in adding this syntax to |
I am using this, would this cause any issues down the line .. asking because what's in the commit utils looks different function flatten_formatters(formatter_table)
local flattened = {}
for key, value in pairs(formatter_table) do
if type(key) == "table" then
-- Handle multiple (table)
for _, filetype in ipairs(key) do
flattened[filetype] = value
end
else
-- Handle single (string)
flattened[key] = value
end
end
return flattened
end |
Don't worry, there should be no problem except in very rare cases where you mutate table.insert(require('conform').formatters_by_ft[<ft>], <additional-formatter>) If you want to prevent this, simply make a deep copy of the value: for _, filetype in ipairs(key) do
flattened[filetype] = vim.deepcopy(value)
end |
Thanks for the detailed explanation and clarity on table references, really appreciate it! I am a js dev so still drawing parallels between it and lua 😅 |
There is only one caveat: lua-language-server currently does not support type annotations for table keys.
See LuaLS/lua-language-server#2610 for more details.