plugins/conform-nvim: migrate to mkNeovimPlugin

This commit is contained in:
Austin Horstman 2024-08-23 14:40:44 -05:00
parent a1c7932bdb
commit 89b876dc0d
No known key found for this signature in database
2 changed files with 344 additions and 224 deletions

View file

@ -1,24 +1,100 @@
{ {
lib, lib,
helpers,
config, config,
pkgs, pkgs,
... ...
}: }:
with lib;
let let
cfg = config.plugins.conform-nvim; inherit (lib) types;
inherit (lib.nixvim) defaultNullOpts mkRaw;
in
lib.nixvim.neovim-plugin.mkNeovimPlugin config {
name = "conform-nvim";
luaName = "conform";
originalName = "conform.nvim";
defaultPackage = pkgs.vimPlugins.conform-nvim;
maintainers = [ lib.maintainers.khaneliman ];
# TODO: added 2024-08-23 remove after 24.11
deprecateExtraOptions = true;
optionsRenamedToSettings = [
"formatters"
"formattersByFt"
"logLevel"
"notifyOnError"
];
imports =
map
(
optionName:
lib.mkRemovedOptionModule
[
"plugins"
"conform-nvim"
optionName
]
''
Please use `plugins.conform-nvim.settings.${lib.nixvim.toSnakeCase optionName}` instead.
Note that nested options will now be snake_case, as well, to match upstream plugin configuration.
''
)
[
"formatAfterSave"
"formatOnSave"
];
settingsOptions =
let
lsp_format =
defaultNullOpts.mkEnumFirstDefault
[
"never"
"fallback"
"prefer"
"first"
"last"
]
''
Option for choosing lsp formatting preference.
- `never`: never use the LSP for formatting.
- `fallback`: LSP formatting is used when no other formatters are available.
- `prefer`: Use only LSP formatting when available.
- `first`: LSP formatting is used when available and then other formatters.
- `last`: Other formatters are used then LSP formatting when available.
'';
timeout_ms = defaultNullOpts.mkUnsignedInt 1000 "Time in milliseconds to block for formatting.";
quiet = defaultNullOpts.mkBool false "Don't show any notifications for warnings or failures.";
stop_after_first = defaultNullOpts.mkBool false "Only run the first available formatter in the list.";
# NOTE: These are the available options for the `default_format_opts` opt.
# They are also included in the `format_opts` options available to `format_after_save` and `format_on_save`.
defaultFormatSubmodule =
with types;
(submodule {
freeformType = attrsOf anything;
options = {
inherit
lsp_format
timeout_ms
quiet
stop_after_first
;
};
});
in in
{ {
options.plugins.conform-nvim = helpers.neovim-plugin.extraOptionsOptions // { formatters_by_ft = defaultNullOpts.mkAttrsOf types.anything { } ''
enable = mkEnableOption "conform-nvim"; Creates a table mapping filetypes to formatters.
package = helpers.mkPluginPackageOption "conform-nvim" pkgs.vimPlugins.conform-nvim; You can run multiple formatters in a row by adding them in a list.
If you'd like to stop after the first successful formatter, set `stop_after_first`.
formattersByFt = helpers.defaultNullOpts.mkAttrsOf' {
type = types.anything;
# Unknown plugin default
description = ''
```nix ```nix
# Map of filetype to formatters # Map of filetype to formatters
formattersByFt = formattersByFt =
@ -26,8 +102,12 @@ in
lua = [ "stylua" ]; lua = [ "stylua" ];
# Conform will run multiple formatters sequentially # Conform will run multiple formatters sequentially
python = [ "isort" "black" ]; python = [ "isort" "black" ];
# Use a sub-list to run only the first available formatter # Use stop_after_first to run only the first available formatter
javascript = [ [ "prettierd" "prettier" ] ]; javascript = {
__unkeyed-1 = "prettierd";
__unkeyed-2 = "prettier";
stop_after_first = true;
};
# Use the "*" filetype to run formatters on all filetypes. # Use the "*" filetype to run formatters on all filetypes.
"*" = [ "codespell" ]; "*" = [ "codespell" ];
# Use the "_" filetype to run formatters on filetypes that don't # Use the "_" filetype to run formatters on filetypes that don't
@ -36,97 +116,126 @@ in
}; };
``` ```
''; '';
};
formatOnSave = helpers.defaultNullOpts.mkNullable' { format_on_save = defaultNullOpts.mkNullable' {
type = type = with types; either strLuaFn defaultFormatSubmodule;
with helpers.nixvimTypes; pluginDefault = { };
either strLuaFn (submodule {
options = {
lspFallback = mkOption {
type = types.bool;
default = true;
description = "See :help conform.format for details.";
};
timeoutMs = mkOption {
type = types.int;
default = 500;
description = "See :help conform.format for details.";
};
};
});
# Unknown plugin default
description = '' description = ''
If this is set, Conform will run the formatter on save. If this is set, Conform will run the formatter asynchronously on save.
It will pass the table to conform.format().
Conform will pass the table from `format_on_save` to `conform.format()`.
This can also be a function that returns the table. This can also be a function that returns the table.
See :help conform.format for details.
See `:help conform.format` for details.
''; '';
apply = v: if lib.isString v then mkRaw v else v;
}; };
formatAfterSave = helpers.defaultNullOpts.mkNullable' { default_format_opts = defaultNullOpts.mkNullable defaultFormatSubmodule { } ''
type = The default options to use when calling `conform.format()`.
with helpers.nixvimTypes; '';
either strLuaFn (submodule {
options = { format_after_save = defaultNullOpts.mkNullable' {
lspFallback = mkOption { type = with types; either strLuaFn defaultFormatSubmodule;
type = types.bool; pluginDefault = { };
default = true;
description = "See :help conform.format for details.";
};
};
});
# Unknown plugin default
description = '' description = ''
If this is set, Conform will run the formatter asynchronously after save. If this is set, Conform will run the formatter asynchronously after save.
It will pass the table to conform.format().
Conform will pass the table from `format_after_save` to `conform.format()`.
This can also be a function that returns the table. This can also be a function that returns the table.
See `:help conform.format` for details.
''; '';
apply = v: if lib.isString v then mkRaw v else v;
}; };
logLevel = helpers.defaultNullOpts.mkLogLevel "error" '' log_level = defaultNullOpts.mkLogLevel "error" ''
Set the log level. Use `:ConformInfo` to see the location of the log file. Set the log level. Use `:ConformInfo` to see the location of the log file.
''; '';
notifyOnError = helpers.defaultNullOpts.mkBool true "Conform will notify you when a formatter errors"; notify_on_error = defaultNullOpts.mkBool true "Conform will notify you when a formatter errors.";
formatters = helpers.defaultNullOpts.mkAttrsOf' { notify_no_formatters = defaultNullOpts.mkBool true "Conform will notify you when no formatters are available for the buffer.";
type = types.anything;
# Unknown plugin default formatters =
description = "Custom formatters and changes to built-in formatters"; defaultNullOpts.mkAttrsOf types.anything { }
}; "Custom formatters and changes to built-in formatters.";
}; };
config = settingsExample = lib.literalMD ''
let ```nix
setupOptions =
with cfg;
{ {
formatters_by_ft = formattersByFt; formattersByFt = {
format_on_save = bash = [
if builtins.isAttrs formatOnSave then "shellcheck"
{ "shellharden"
lsp_fallback = formatOnSave.lspFallback; "shfmt"
timeout_ms = formatOnSave.timeoutMs; ];
} cpp = [ "clang_format" ];
else javascript = {
helpers.mkRaw formatOnSave; __unkeyed-1 = "prettierd";
format_after_save = __unkeyed-2 = "prettier";
if builtins.isAttrs formatAfterSave then timeout_ms = 2000;
{ lsp_fallback = formatOnSave.lspFallback; } stop_after_first = true;
else };
helpers.mkRaw formatAfterSave; "_" = [
log_level = logLevel; "squeeze_blanks"
notify_on_error = notifyOnError; "trim_whitespace"
inherit formatters; "trim_newlines"
} ];
// cfg.extraOptions; };
in format_on_save = # Lua
mkIf cfg.enable { '''
extraPlugins = [ cfg.package ]; function(bufnr)
if vim.g.disable_autoformat or vim.b[bufnr].disable_autoformat then
return
end
extraConfigLua = '' if slow_format_filetypes[vim.bo[bufnr].filetype] then
require("conform").setup(${helpers.toLuaObject setupOptions}) return
end
local function on_format(err)
if err and err:match("timeout$") then
slow_format_filetypes[vim.bo[bufnr].filetype] = true
end
end
return { timeout_ms = 200, lsp_fallback = true }, on_format
end
''';
format_after_save = # Lua
'''
function(bufnr)
if vim.g.disable_autoformat or vim.b[bufnr].disable_autoformat then
return
end
if not slow_format_filetypes[vim.bo[bufnr].filetype] then
return
end
return { lsp_fallback = true }
end
''';
log_level = "warn";
notify_on_error = false;
notify_no_formatters = false;
formatters = {
shellcheck = {
command = lib.getExe pkgs.shellcheck;
};
shfmt = {
command = lib.getExe pkgs.shfmt;
};
shellharden = {
command = lib.getExe pkgs.shellharden;
};
squeeze_blanks = {
command = lib.getExe' pkgs.coreutils "cat";
};
};
}
```
''; '';
};
} }

View file

@ -1,3 +1,4 @@
{ lib, pkgs, ... }:
{ {
empty = { empty = {
plugins.conform-nvim.enable = true; plugins.conform-nvim.enable = true;
@ -7,31 +8,68 @@
plugins.conform-nvim = { plugins.conform-nvim = {
enable = true; enable = true;
formattersByFt = { settings = {
formatters_by_ft = { };
format_on_save = {
lsp_format = "never";
timeout_ms = 1000;
quiet = false;
stop_after_first = false;
};
default_format_opts = {
lsp_format = "never";
timeout_ms = 1000;
quiet = false;
stop_after_first = false;
};
format_after_save = {
lsp_format = "never";
timeout_ms = 1000;
quiet = false;
stop_after_first = false;
};
log_level = "error";
notify_on_error = true;
notify_no_formatters = true;
formatters = { };
};
};
};
example = {
plugins.conform-nvim = {
enable = true;
settings = {
formatters_by_ft = {
lua = [ "stylua" ]; lua = [ "stylua" ];
python = [ python = [
"isort" "isort"
"black" "black"
]; ];
javascript = [ javascript = {
[ __unkeyed-1 = "prettierd";
"prettierd" __unkeyed-2 = "prettier";
"prettier" timeout_ms = 2000;
] stop_after_first = true;
]; };
"*" = [ "codespell" ]; "*" = [ "codespell" ];
"_" = [ "trimWhitespace" ]; "_" = [ "trimWhitespace" ];
}; };
formatOnSave = { format_on_save = {
lspFallback = true; lsp_format = "fallback";
timeoutMs = 500; timeout_ms = 500;
}; };
formatAfterSave = { format_after_save = {
lspFallback = true; lsp_format = "fallback";
}; };
logLevel = "error"; log_level = "error";
notifyOnError = true; notify_on_error = false;
notify_no_formatters = false;
formatters = { formatters = {
nixfmt = {
command = lib.getExe pkgs.nixfmt-rfc-style;
};
myFormatter = { myFormatter = {
command = "myCmd"; command = "myCmd";
args = [ args = [
@ -73,28 +111,14 @@
}; };
}; };
}; };
};
custom_format_on_save_function = { custom_format_on_save_function = {
plugins.conform-nvim = { plugins.conform-nvim = {
enable = true; enable = true;
formattersByFt = { settings = {
lua = [ "stylua" ]; format_on_save = ''
python = [
"isort"
"black"
];
javascript = [
[
"prettierd"
"prettier"
]
];
"*" = [ "codespell" ];
"_" = [ "trimWhitespace" ];
};
formatOnSave = ''
function(bufnr) function(bufnr)
local ignore_filetypes = { "helm" } local ignore_filetypes = { "helm" }
if vim.tbl_contains(ignore_filetypes, vim.bo[bufnr].filetype) then if vim.tbl_contains(ignore_filetypes, vim.bo[bufnr].filetype) then
@ -116,28 +140,14 @@
''; '';
}; };
}; };
};
custom_format_after_save_function = { custom_format_after_save_function = {
plugins.conform-nvim = { plugins.conform-nvim = {
enable = true; enable = true;
formattersByFt = { settings = {
lua = [ "stylua" ]; format_after_save = ''
python = [
"isort"
"black"
];
javascript = [
[
"prettierd"
"prettier"
]
];
"*" = [ "codespell" ];
"_" = [ "trimWhitespace" ];
};
formatAfterSave = ''
function(bufnr) function(bufnr)
if vim.g.disable_autoformat or vim.b[bufnr].disable_autoformat then if vim.g.disable_autoformat or vim.b[bufnr].disable_autoformat then
return return
@ -150,4 +160,5 @@
''; '';
}; };
}; };
};
} }