plugins/lint: migrate to mkNeovimPlugin

This commit is contained in:
Heitor Augusto 2024-12-16 15:35:19 -03:00 committed by nix-infra-bot
parent 5b35c4eaed
commit 3fa487c81d

View file

@ -1,24 +1,18 @@
{ { lib, ... }:
lib,
helpers,
config,
pkgs,
...
}:
with lib;
let let
cfg = config.plugins.lint; inherit (lib) mkOption types;
inherit (lib.nixvim) defaultNullOpts toLuaObject;
linterOptions = with types; { linterOptions = {
cmd = { cmd = {
type = str; type = types.str;
description = "The command to call the linter"; description = "The command to call the linter";
example = "linter_cmd"; example = "linter_cmd";
mandatory = true; mandatory = true;
}; };
stdin = { stdin = {
type = bool; type = types.bool;
description = '' description = ''
Whether this parser supports content input via stdin. Whether this parser supports content input via stdin.
In that case the filename is automatically added to the arguments. In that case the filename is automatically added to the arguments.
@ -29,7 +23,7 @@ let
}; };
append_fname = { append_fname = {
type = bool; type = types.bool;
description = '' description = ''
Automatically append the file name to `args` if `stdin = false` Automatically append the file name to `args` if `stdin = false`
Whether this parser supports content input via stdin. Whether this parser supports content input via stdin.
@ -41,7 +35,7 @@ let
}; };
args = { args = {
type = listOf (either str rawLua); type = with types; listOf (either str rawLua);
description = '' description = ''
List of arguments. List of arguments.
Can contain functions with zero arguments that will be evaluated once the linter is used. Can contain functions with zero arguments that will be evaluated once the linter is used.
@ -51,7 +45,7 @@ let
}; };
stream = { stream = {
type = enum [ type = types.enum [
"stdout" "stdout"
"stderr" "stderr"
"both" "both"
@ -65,7 +59,7 @@ let
}; };
ignore_exitcode = { ignore_exitcode = {
type = bool; type = types.bool;
description = '' description = ''
Whether the linter exiting with a code !=0 should be considered normal. Whether the linter exiting with a code !=0 should be considered normal.
@ -75,7 +69,7 @@ let
}; };
env = { env = {
type = attrsOf str; type = with types; attrsOf str;
description = '' description = ''
Custom environment table to use with the external process. Custom environment table to use with the external process.
Note that this replaces the **entire** environment, it is not additive. Note that this replaces the **entire** environment, it is not additive.
@ -86,7 +80,7 @@ let
}; };
parser = { parser = {
type = lib.types.strLuaFn; type = types.strLuaFn;
description = "The code for your parser function."; description = "The code for your parser function.";
example = '' example = ''
require('lint.parser').from_pattern(pattern, groups, severity_map, defaults, opts) require('lint.parser').from_pattern(pattern, groups, severity_map, defaults, opts)
@ -98,9 +92,9 @@ let
mkLinterOpts = mkLinterOpts =
noDefaults: noDefaults:
types.submodule { types.submodule {
freeformType = types.attrs; freeformType = with types; attrsOf anything;
options = mapAttrs ( options = builtins.mapAttrs (
optionName: optionName:
( (
{ {
@ -132,67 +126,20 @@ let
) linterOptions; ) linterOptions;
}; };
in in
{ lib.nixvim.plugins.mkNeovimPlugin {
options.plugins.lint = lib.nixvim.plugins.neovim.extraOptionsOptions // { name = "lint";
enable = mkEnableOption "nvim-lint"; packPathName = "nvim-lint";
package = "nvim-lint";
callSetup = false;
hasSettings = false;
package = lib.mkPackageOption pkgs "nvim-lint" { maintainers = [ lib.maintainers.HeitorAugustoLN ];
default = [
"vimPlugins"
"nvim-lint"
];
};
lintersByFt = mkOption {
type = with types; attrsOf (listOf str);
default = { };
description = ''
Configure the linters you want to run per file type.
'';
example = {
text = [ "vale" ];
json = [ "jsonlint" ];
markdown = [ "vale" ];
rst = [ "vale" ];
ruby = [ "ruby" ];
janet = [ "janet" ];
inko = [ "inko" ];
clojure = [ "clj-kondo" ];
dockerfile = [ "hadolint" ];
terraform = [ "tflint" ];
};
};
linters = mkOption {
type = with types; attrsOf (mkLinterOpts false);
default = { };
description = ''
Customize the existing linters by overriding some of their properties.
'';
example = {
phpcs.args = [
"-q"
"--report=json"
"-"
];
};
};
customLinters = mkOption {
type = with types; attrsOf (either str (mkLinterOpts true));
default = { };
description = ''
Configure the linters you want to run per file type.
It can be both an attrs or a string containing the lua code that returns the appropriate
table.
'';
example = { };
};
extraOptions = {
autoCmd = autoCmd =
let let
defaultEvent = "BufWritePost"; defaultEvent = "BufWritePost";
defaultCallback = helpers.mkRaw '' defaultCallback = lib.nixvim.mkRaw ''
function() function()
require('lint').try_lint() require('lint').try_lint()
end end
@ -202,18 +149,18 @@ in
type = type =
with types; with types;
nullOr (submodule { nullOr (submodule {
options = helpers.autocmd.autoCmdOptions // { options = lib.nixvim.autocmd.autoCmdOptions // {
event = mkOption {
type = with types; nullOr (either str (listOf str));
default = defaultEvent;
description = "The event or events that should trigger linting.";
};
callback = mkOption { callback = mkOption {
type = with types; nullOr (either str rawLua); type = with types; nullOr (either str rawLua);
default = defaultCallback; default = defaultCallback;
description = "What action to perform for linting"; description = "What action to perform for linting";
}; };
event = mkOption {
type = with types; nullOr (either str (listOf str));
default = defaultEvent;
description = "The event or events that should trigger linting.";
};
}; };
}); });
description = '' description = ''
@ -225,43 +172,81 @@ in
callback = defaultCallback; callback = defaultCallback;
}; };
}; };
customLinters = defaultNullOpts.mkAttrsOf (with types; either str (mkLinterOpts true)) { } ''
Configure the linters you want to run per file type.
It can be both an attrs or a string containing the lua code that returns the appropriate
table.
'';
linters =
defaultNullOpts.mkAttrsOf (mkLinterOpts false)
{
phpcs.args = [
"-q"
"--report=json"
"-"
];
}
''
Configure the linters you want to run.
You can also add custom linters here.
'';
lintersByFt =
defaultNullOpts.mkAttrsOf (types.listOf types.str)
{
text = [ "vale" ];
json = [ "jsonlint" ];
markdown = [ "vale" ];
rst = [ "vale" ];
ruby = [ "ruby" ];
janet = [ "janet" ];
inko = [ "inko" ];
clojure = [ "clj-kondo" ];
dockerfile = [ "hadolint" ];
terraform = [ "tflint" ];
}
''
Configure the linters you want to run per file type.
'';
}; };
config = mkIf cfg.enable { extraConfig = cfg: {
extraPlugins = [ cfg.package ]; autoCmd = lib.optionals (cfg.autoCmd != null) [ cfg.autoCmd ];
plugins.lint.luaConfig.content =
extraConfigLua =
'' ''
__lint = require('lint') local __lint = require('lint')
__lint.linters_by_ft = ${lib.nixvim.toLuaObject cfg.lintersByFt}
'' ''
+ (optionalString (cfg.linters != { }) ( + (lib.optionalString (cfg.lintersByFt != null) ''
concatLines ( __lint.linters_by_ft = ${toLuaObject cfg.lintersByFt}
flatten ( '')
mapAttrsToList ( + (lib.optionalString (cfg.customLinters != null) (
lib.concatLines (
lib.mapAttrsToList (
customLinter: linterConfig:
let
linterConfig' =
if builtins.isString linterConfig then lib.nixvim.mkRaw linterConfig else linterConfig;
in
"__lint.linters.${customLinter} = ${toLuaObject linterConfig'}"
) cfg.customLinters
)
))
+ (lib.optionalString (cfg.linters != null) (
lib.concatLines (
lib.flatten (
lib.mapAttrsToList (
linter: linterConfig: linter: linterConfig:
mapAttrsToList ( lib.mapAttrsToList (
propName: propValue: propName: propValue:
optionalString ( lib.optionalString (
propValue != null propValue != null
) ''__lint.linters["${linter}"]["${propName}"] = ${lib.nixvim.toLuaObject propValue}'' ) "__lint.linters.${linter}.${propName} = ${toLuaObject propValue}"
) linterConfig ) linterConfig
) cfg.linters ) cfg.linters
) )
) )
))
+ (optionalString (cfg.customLinters != { }) (
concatLines (
mapAttrsToList (
customLinter: linterConfig:
let
linterConfig' = if isString linterConfig then helpers.mkRaw linterConfig else linterConfig;
in
''__lint.linters["${customLinter}"] = ${lib.nixvim.toLuaObject linterConfig'}''
) cfg.customLinters
)
)); ));
autoCmd = optional (cfg.autoCmd != null) cfg.autoCmd;
}; };
} }