nix-community.nixvim/plugins/lsp/lsp-format.nix

119 lines
3.3 KiB
Nix
Raw Normal View History

2023-05-12 11:01:10 +02:00
{
lib,
helpers,
2023-05-12 11:01:10 +02:00
config,
pkgs,
2023-05-12 11:01:10 +02:00
...
}:
2024-05-05 19:39:35 +02:00
with lib;
let
2023-05-12 11:01:10 +02:00
cfg = config.plugins.lsp-format;
2024-05-05 19:39:35 +02:00
in
{
options.plugins.lsp-format = helpers.neovim-plugin.extraOptionsOptions // {
enable = mkEnableOption "lsp-format.nvim";
2023-05-12 11:01:10 +02:00
package = helpers.mkPluginPackageOption "lsp-format.nvim" pkgs.vimPlugins.lsp-format-nvim;
2023-05-12 11:01:10 +02:00
2024-05-05 19:39:35 +02:00
setup = mkOption {
type =
with types;
attrsOf (submodule {
# Allow the user to provide other options
freeformType = types.attrs;
2023-05-12 11:01:10 +02:00
2024-05-05 19:39:35 +02:00
options = {
exclude = helpers.mkNullOrOption (listOf str) "List of client names to exclude from formatting.";
2023-05-12 11:01:10 +02:00
2024-05-05 19:39:35 +02:00
order = helpers.mkNullOrOption (listOf str) ''
List of client names. Formatting is requested from clients in the following
order: first all clients that are not in the `order` table, then the remaining
clients in the order as they occur in the `order` table.
(same logic as |vim.lsp.buf.formatting_seq_sync()|).
'';
2023-05-12 11:01:10 +02:00
2024-05-05 19:39:35 +02:00
sync = helpers.defaultNullOpts.mkBool false ''
Whether to turn on synchronous formatting.
The editor will block until formatting is done.
'';
2023-05-12 11:01:10 +02:00
2024-05-05 19:39:35 +02:00
force = helpers.defaultNullOpts.mkBool false ''
If true, the format result will always be written to the buffer, even if the
buffer changed.
'';
2023-05-12 11:01:10 +02:00
};
2024-05-05 19:39:35 +02:00
});
description = "The setup option maps |filetypes| to format options.";
example = {
2024-05-19 18:33:14 +02:00
go = {
2024-05-05 19:39:35 +02:00
exclude = [ "gopls" ];
order = [
"gopls"
"efm"
];
sync = true;
force = true;
2023-05-12 11:01:10 +02:00
};
};
2024-05-05 19:39:35 +02:00
default = { };
};
2023-05-12 11:01:10 +02:00
2024-05-05 19:39:35 +02:00
lspServersToEnable = mkOption {
type =
with types;
either (enum [
"none"
"all"
]) (listOf str);
default = "all";
description = ''
Choose the LSP servers for which lsp-format should be enabled.
2023-05-12 11:01:10 +02:00
2024-05-05 19:39:35 +02:00
Possible values:
- "all" (default): Enable formatting for all language servers
- "none": Do not enable formatting on any language server.
You might choose this if for some reason you want to manually call
`require("lsp-format").on_attach(client)` in the `onAttach` function of your language
servers.
- list of LS names: Manually choose the servers by name
'';
example = [
"efm"
"gopls"
];
2023-05-12 11:01:10 +02:00
};
2024-05-05 19:39:35 +02:00
};
2023-05-12 11:01:10 +02:00
2024-05-05 19:39:35 +02:00
config =
let
setupOptions = cfg.setup // cfg.extraOptions;
in
2023-05-12 11:01:10 +02:00
mkIf cfg.enable {
warnings = mkIf (!config.plugins.lsp.enable) [
"You have enabled `plugins.lsp-format` but have `plugins.lsp` disabled."
];
2024-05-05 19:39:35 +02:00
extraPlugins = [ cfg.package ];
2023-05-12 11:01:10 +02:00
plugins.lsp = {
onAttach = mkIf (cfg.lspServersToEnable == "all") ''
require("lsp-format").on_attach(client)
'';
servers =
2024-05-05 19:39:35 +02:00
if (isList cfg.lspServersToEnable) then
genAttrs cfg.lspServersToEnable (serverName: {
onAttach.function = ''
require("lsp-format").on_attach(client)
'';
})
else
{ };
2023-05-12 11:01:10 +02:00
};
extraConfigLua = ''
require("lsp-format").setup(${helpers.toLuaObject setupOptions})
'';
};
}