language/python: allow multiple formatters

This commit is contained in:
Ching Pei Yang 2025-08-23 13:50:35 +02:00
commit 2e7079e429
No known key found for this signature in database
GPG key ID: B3841364253DC4C8

View file

@ -4,8 +4,9 @@
lib,
...
}: let
inherit (builtins) attrNames;
inherit (builtins) attrNames warn;
inherit (lib.options) mkEnableOption mkOption literalExpression;
inherit (lib.lists) flatten;
inherit (lib.meta) getExe;
inherit (lib.modules) mkIf mkMerge;
inherit (lib.types) enum package bool;
@ -123,31 +124,19 @@
defaultFormat = "black";
formats = {
black = {
package = pkgs.black;
command = getExe pkgs.black;
};
isort = {
package = pkgs.isort;
command = getExe pkgs.isort;
};
black-and-isort = {
package = pkgs.writeShellApplication {
name = "black";
runtimeInputs = [pkgs.black pkgs.isort];
text = ''
black --quiet - "$@" | isort --profile black -
'';
};
};
# dummy option for backwards compat
black-and-isort = {};
ruff = {
package = pkgs.writeShellApplication {
name = "ruff";
runtimeInputs = [pkgs.ruff];
text = ''
ruff format -
'';
};
command = getExe pkgs.ruff;
args = ["format" "-"];
};
};
@ -240,9 +229,9 @@ in {
enable = mkEnableOption "Python formatting" // {default = config.vim.languages.enableFormat;};
type = mkOption {
type = enum (attrNames formats);
type = singleOrListOf (enum (attrNames formats));
default = defaultFormat;
description = "Python formatter to use";
description = "Python formatters to use";
};
package = mkOption {
@ -316,21 +305,25 @@ in {
})
(mkIf cfg.format.enable {
vim.formatter.conform-nvim = {
vim.formatter.conform-nvim = let
names = flatten (map (type:
if type == "black-and-isort"
then
warn ''
vim.languages.python.format.type: "black-and-isort" is deprecated,
use `["black" "isort"]` instead.
'' ["black" "isort"]
else type)
cfg.format.type);
in {
enable = true;
# HACK: I'm planning to remove these soon so I just took the easiest way out
setupOpts.formatters_by_ft.python =
if cfg.format.type == "black-and-isort"
then ["black"]
else [cfg.format.type];
setupOpts.formatters_by_ft.python = names;
setupOpts.formatters =
if (cfg.format.type == "black-and-isort")
then {
black.command = "${cfg.format.package}/bin/black";
}
else {
${cfg.format.type}.command = getExe cfg.format.package;
};
mapListToAttrs (name: {
inherit name;
value = formats.${name};
})
names;
};
})