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