nix-community.nixvim/plugins/lsp/conform-nvim.nix

131 lines
4 KiB
Nix
Raw Normal View History

2023-11-03 20:18:24 +05:30
{
lib,
helpers,
config,
pkgs,
2023-11-03 20:18:24 +05:30
...
}:
2024-05-05 19:39:35 +02:00
with lib;
let
2023-11-03 20:18:24 +05:30
cfg = config.plugins.conform-nvim;
2024-05-05 19:39:35 +02:00
in
{
options.plugins.conform-nvim = helpers.neovim-plugin.extraOptionsOptions // {
enable = mkEnableOption "conform-nvim";
2023-11-03 20:18:24 +05:30
package = helpers.mkPluginPackageOption "conform-nvim" pkgs.vimPlugins.conform-nvim;
2023-11-03 20:18:24 +05:30
2024-05-05 19:39:35 +02:00
formattersByFt =
helpers.defaultNullOpts.mkNullable (types.attrsOf types.anything) "see documentation"
2023-11-03 20:18:24 +05:30
''
```nix
# Map of filetype to formatters
formattersByFt =
{
lua = [ "stylua" ];
# Conform will run multiple formatters sequentially
python = [ "isort" "black" ];
# Use a sub-list to run only the first available formatter
javascript = [ [ "prettierd" "prettier" ] ];
# Use the "*" filetype to run formatters on all filetypes.
"*" = [ "codespell" ];
# Use the "_" filetype to run formatters on filetypes that don't
# have other formatters configured.
"_" = [ "trim_whitespace" ];
};
```
'';
2024-05-05 19:39:35 +02:00
formatOnSave =
helpers.defaultNullOpts.mkNullable
(
with helpers.nixvimTypes;
2024-05-05 19:39:35 +02:00
either strLuaFn (submodule {
options = {
lspFallback = mkOption {
type = types.bool;
default = true;
description = "See :help conform.format for details.";
};
timeoutMs = mkOption {
type = types.int;
default = 500;
description = "See :help conform.format for details.";
};
};
})
)
2023-11-03 20:18:24 +05:30
"see documentation"
''
If this is set, Conform will run the formatter on save.
It will pass the table to conform.format().
This can also be a function that returns the table.
See :help conform.format for details.
'';
2024-05-05 19:39:35 +02:00
formatAfterSave =
helpers.defaultNullOpts.mkNullable
(
with helpers.nixvimTypes;
2024-05-05 19:39:35 +02:00
either strLuaFn (submodule {
options = {
lspFallback = mkOption {
type = types.bool;
default = true;
description = "See :help conform.format for details.";
};
};
})
)
2023-11-03 20:18:24 +05:30
"see documentation"
''
If this is set, Conform will run the formatter asynchronously after save.
It will pass the table to conform.format().
This can also be a function that returns the table.
'';
2024-05-05 19:39:35 +02:00
logLevel = helpers.defaultNullOpts.mkLogLevel "error" ''
Set the log level. Use `:ConformInfo` to see the location of the log file.
'';
2023-11-03 20:18:24 +05:30
2024-05-05 19:39:35 +02:00
notifyOnError = helpers.defaultNullOpts.mkBool true "Conform will notify you when a formatter errors";
2023-11-03 20:18:24 +05:30
2024-05-05 19:39:35 +02:00
formatters =
helpers.defaultNullOpts.mkNullable (types.attrsOf types.anything) "see documentation"
2023-11-03 20:18:24 +05:30
"Custom formatters and changes to built-in formatters";
2024-05-05 19:39:35 +02:00
};
2023-11-03 20:18:24 +05:30
2024-05-05 19:39:35 +02:00
config =
let
setupOptions =
with cfg;
{
formatters_by_ft = formattersByFt;
format_on_save =
if builtins.isAttrs formatOnSave then
{
lsp_fallback = formatOnSave.lspFallback;
timeout_ms = formatOnSave.timeoutMs;
}
else
helpers.mkRaw formatOnSave;
format_after_save =
if builtins.isAttrs formatAfterSave then
{ lsp_fallback = formatOnSave.lspFallback; }
else
helpers.mkRaw formatAfterSave;
log_level = logLevel;
notify_on_error = notifyOnError;
inherit formatters;
}
// cfg.extraOptions;
in
2023-11-03 20:18:24 +05:30
mkIf cfg.enable {
2024-05-05 19:39:35 +02:00
extraPlugins = [ cfg.package ];
2023-11-03 20:18:24 +05:30
extraConfigLua = ''
require("conform").setup(${helpers.toLuaObject setupOptions})
'';
};
}