plugins/conform: add str type for option formatOnSave

This commit is contained in:
Alison Jenkins 2023-12-11 20:36:58 +00:00 committed by Gaétan Lepage
parent 7fbb9240de
commit 14f753cfb6
2 changed files with 64 additions and 18 deletions

View file

@ -38,7 +38,13 @@ in {
'';
formatOnSave =
helpers.defaultNullOpts.mkNullable (types.submodule {
helpers.defaultNullOpts.mkNullable
(
with types;
either
str
(
submodule {
options = {
lspFallback = mkOption {
type = types.bool;
@ -51,7 +57,9 @@ in {
description = "See :help conform.format for details.";
};
};
})
}
)
)
"see documentation"
''
If this is set, Conform will run the formatter on save.
@ -96,10 +104,13 @@ in {
setupOptions = with cfg;
{
formatters_by_ft = formattersByFt;
format_on_save = helpers.ifNonNull' formatOnSave {
format_on_save =
if builtins.isAttrs formatOnSave
then {
lsp_fallback = formatOnSave.lspFallback;
timeout_ms = formatOnSave.timeoutMs;
};
}
else helpers.mkRaw formatOnSave;
format_after_save = helpers.ifNonNull' formatOnSave {
lsp_fallback = formatOnSave.lspFallback;
};

View file

@ -59,4 +59,39 @@
};
};
};
custom_format_on_save_function = {
plugins.conform-nvim = {
enable = true;
formattersByFt = {
lua = ["stylua"];
python = ["isort" "black"];
javascript = [["prettierd" "prettier"]];
"*" = ["codespell"];
"_" = ["trimWhitespace"];
};
formatOnSave = ''
function(bufnr)
local ignore_filetypes = { "helm" }
if vim.tbl_contains(ignore_filetypes, vim.bo[bufnr].filetype) then
return
end
-- Disable with a global or buffer-local variable
if vim.g.disable_autoformat or vim.b[bufnr].disable_autoformat then
return
end
-- Disable autoformat for files in a certain path
local bufname = vim.api.nvim_buf_get_name(bufnr)
if bufname:match("/node_modules/") then
return
end
return { timeout_ms = 500, lsp_fallback = true }
end
'';
};
};
}