Skip to content

Commit 23cb030

Browse files
committed
language/python: allow multiple formatters
1 parent d36996c commit 23cb030

File tree

1 file changed

+23
-25
lines changed

1 file changed

+23
-25
lines changed

modules/plugins/languages/python.nix

Lines changed: 23 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,9 @@
44
lib,
55
...
66
}: let
7-
inherit (builtins) attrNames;
7+
inherit (builtins) attrNames warn;
88
inherit (lib.options) mkEnableOption mkOption literalExpression;
9+
inherit (lib.lists) flatten;
910
inherit (lib.meta) getExe;
1011
inherit (lib.modules) mkIf mkMerge;
1112
inherit (lib.types) enum package bool;
@@ -130,15 +131,8 @@
130131
package = pkgs.isort;
131132
};
132133

133-
black-and-isort = {
134-
package = pkgs.writeShellApplication {
135-
name = "black";
136-
runtimeInputs = [pkgs.black pkgs.isort];
137-
text = ''
138-
black --quiet - "$@" | isort --profile black -
139-
'';
140-
};
141-
};
134+
# dummy option for backwards compat
135+
black-and-isort = {};
142136

143137
ruff = {
144138
package = pkgs.writeShellApplication {
@@ -240,9 +234,9 @@ in {
240234
enable = mkEnableOption "Python formatting" // {default = config.vim.languages.enableFormat;};
241235

242236
type = mkOption {
243-
type = enum (attrNames formats);
237+
type = singleOrListOf (enum (attrNames formats));
244238
default = defaultFormat;
245-
description = "Python formatter to use";
239+
description = "Python formatters to use";
246240
};
247241

248242
package = mkOption {
@@ -316,21 +310,25 @@ in {
316310
})
317311

318312
(mkIf cfg.format.enable {
319-
vim.formatter.conform-nvim = {
313+
vim.formatter.conform-nvim = let
314+
names = flatten (map (type:
315+
if type == "black-and-isort"
316+
then
317+
warn ''
318+
vim.languages.python.format.type: "black-and-isort" is deprecated,
319+
use `["black" "isort"]` instead.
320+
'' ["black" "isort"]
321+
else type)
322+
cfg.format.type);
323+
in {
320324
enable = true;
321-
# HACK: I'm planning to remove these soon so I just took the easiest way out
322-
setupOpts.formatters_by_ft.python =
323-
if cfg.format.type == "black-and-isort"
324-
then ["black"]
325-
else [cfg.format.type];
325+
setupOpts.formatters_by_ft.python = names;
326326
setupOpts.formatters =
327-
if (cfg.format.type == "black-and-isort")
328-
then {
329-
black.command = "${cfg.format.package}/bin/black";
330-
}
331-
else {
332-
${cfg.format.type}.command = getExe cfg.format.package;
333-
};
327+
mapListToAttrs (name: {
328+
inherit name;
329+
value = {command = getExe formats.${name}.package;};
330+
})
331+
names;
334332
};
335333
})
336334

0 commit comments

Comments
 (0)