mirror of
https://github.com/NotAShelf/nvf.git
synced 2025-09-05 18:01:32 +00:00
language/python: allow multiple formatters
This commit is contained in:
parent
d36996c8ba
commit
2e7079e429
1 changed files with 27 additions and 34 deletions
|
@ -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;
|
||||
};
|
||||
})
|
||||
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue