plugins/none-ls: add enableLspFormat option

This commit is contained in:
Gaetan Lepage 2023-11-28 09:47:29 +01:00 committed by Gaétan Lepage
parent 3ff4976eba
commit 27b0c6bf20
2 changed files with 82 additions and 36 deletions

View file

@ -28,6 +28,15 @@ in {
description = "Plugin to use for none-ls"; description = "Plugin to use for none-ls";
}; };
enableLspFormat = mkOption {
type = types.bool;
default = config.plugins.lsp-format.enable;
description = ''
Automatically enable the `lsp-format` plugin and configure `none-ls` accordingly.
'';
example = true;
};
border = helpers.defaultNullOpts.mkBorder "null" "`:NullLsInfo` UI window." '' border = helpers.defaultNullOpts.mkBorder "null" "`:NullLsInfo` UI window." ''
Uses `NullLsInfoBorder` highlight group (see [Highlight Groups](#highlight-groups)). Uses `NullLsInfoBorder` highlight group (see [Highlight Groups](#highlight-groups)).
''; '';
@ -206,42 +215,67 @@ in {
"The list of sources to enable, should be strings of lua code. Don't use this directly"; "The list of sources to enable, should be strings of lua code. Don't use this directly";
}; };
config = let config = mkIf cfg.enable {
options = warnings =
optional
(cfg.enableLspFormat && (cfg.onAttach != null))
''
You have enabled the lsp-format integration with none-ls.
However, you have provided a custom value to `plugins.none-ls.onAttach`.
-> The `enableLspFormat` option will thus not be able to add the `require('lsp-format').on_attach` snippet to `none-ls`.
'';
assertions = [
{
assertion = cfg.enableLspFormat -> config.plugins.lsp-format.enable;
message = ''
Nixvim: You have enabled the `lsp-format` integration with none-ls.
However, you have not enabled the `lsp-format` plugin itself (`plugins.lsp-format.enable = true`).
'';
}
];
extraPlugins = [cfg.package];
extraConfigLua = let
onAttach' =
if (cfg.onAttach == null) && cfg.enableLspFormat
then ''
require('lsp-format').on_attach
''
else cfg.onAttach;
setupOptions = with cfg;
{ {
inherit inherit
(cfg)
border border
cmd cmd
debounce debounce
debug debug
; ;
default_timeout = cfg.defaultTimeout; default_timeout = defaultTimeout;
diagnostic_config = cfg.diagnosticConfig; diagnostic_config = diagnosticConfig;
diagnostics_format = cfg.diagnosticsFormat; diagnostics_format = diagnosticsFormat;
fallback_severity = fallback_severity =
if isString cfg.fallbackSeverity if isString fallbackSeverity
then helpers.mkRaw "vim.diagnostic.severity.${strings.toUpper cfg.fallbackSeverity}" then helpers.mkRaw "vim.diagnostic.severity.${strings.toUpper fallbackSeverity}"
else cfg.fallbackSeverity; else fallbackSeverity;
log_level = cfg.logLevel; log_level = logLevel;
notify_format = cfg.notifyFormat; notify_format = notifyFormat;
on_attach = helpers.mkRaw cfg.onAttach; on_attach = helpers.mkRaw onAttach';
on_init = helpers.mkRaw cfg.onInit; on_init = helpers.mkRaw onInit;
on_exit = helpers.mkRaw cfg.onExit; on_exit = helpers.mkRaw onExit;
root_dir = helpers.mkRaw cfg.rootDir; root_dir = helpers.mkRaw rootDir;
should_attach = helpers.mkRaw cfg.shouldAttach; should_attach = helpers.mkRaw shouldAttach;
temp_dir = cfg.tempDir; temp_dir = tempDir;
update_in_insert = cfg.updateInInsert; update_in_insert = updateInInsert;
sources = cfg.sourcesItems; sources = sourcesItems;
} }
// cfg.extraOptions; // cfg.extraOptions;
in in ''
mkIf cfg.enable { require("null-ls").setup(${helpers.toLuaObject setupOptions})
extraPlugins = [cfg.package];
extraConfigLua = ''
require("null-ls").setup(${helpers.toLuaObject options})
''; '';
}; };
} }

View file

@ -14,9 +14,21 @@
# }; # };
# }; # };
with-lsp-format = {
plugins = {
lsp-format.enable = true;
none-ls = {
enable = true;
enableLspFormat = true;
};
};
};
default = { default = {
plugins.none-ls = { plugins.none-ls = {
enable = true; enable = true;
enableLspFormat = false;
border = null; border = null;
cmd = ["nvim"]; cmd = ["nvim"];
debounce = 250; debounce = 250;