neovim-flake/modules/plugins/languages/bash.nix

132 lines
3.3 KiB
Nix
Raw Normal View History

2023-09-23 18:36:25 +03:00
{
config,
pkgs,
2023-09-23 18:36:25 +03:00
lib,
...
}: let
inherit (builtins) attrNames;
inherit (lib.options) mkOption mkEnableOption literalExpression;
inherit (lib.meta) getExe;
inherit (lib.modules) mkIf mkMerge;
inherit (lib.lists) isList;
inherit (lib.types) enum either package listOf str bool;
2025-06-02 23:56:40 +01:00
inherit (lib.generators) mkLuaInline;
2024-03-23 20:14:39 -04:00
inherit (lib.nvim.types) diagnostics mkGrammarOption;
inherit (lib.nvim.lua) expToLua;
2025-06-05 09:42:05 +01:00
inherit (lib.nvim.attrsets) mapListToAttrs;
2023-09-23 18:36:25 +03:00
cfg = config.vim.languages.bash;
2025-06-02 23:56:40 +01:00
defaultServers = ["bash-ls"];
2023-09-23 18:36:25 +03:00
servers = {
bash-ls = {
2025-06-02 23:56:40 +01:00
enable = true;
cmd = [(getExe pkgs.bash-language-server) "start"];
filetypes = ["bash" "sh"];
root_markers = [".git"];
settings = {
basheIde = {
globPattern = mkLuaInline "vim.env.GLOB_PATTERN or '*@(.sh|.inc|.bash|.command)'";
2023-09-23 18:36:25 +03:00
};
2025-06-02 23:56:40 +01:00
};
2023-09-23 18:36:25 +03:00
};
};
defaultFormat = "shfmt";
formats = {
shfmt = {
package = pkgs.shfmt;
};
};
2024-03-23 20:14:39 -04:00
defaultDiagnosticsProvider = ["shellcheck"];
diagnosticsProviders = {
2023-09-23 18:36:25 +03:00
shellcheck = {
package = pkgs.shellcheck;
};
};
in {
options.vim.languages.bash = {
enable = mkEnableOption "Bash language support";
treesitter = {
enable = mkEnableOption "Bash treesitter" // {default = config.vim.languages.enableTreesitter;};
2024-03-23 20:14:39 -04:00
package = mkGrammarOption pkgs "bash";
2023-09-23 18:36:25 +03:00
};
lsp = {
2025-06-02 23:56:40 +01:00
enable = mkEnableOption "Bash LSP support" // {default = config.vim.lsp.enable;};
servers = mkOption {
2023-09-23 18:36:25 +03:00
description = "Bash LSP server to use";
2025-06-02 23:56:40 +01:00
type = listOf (enum (attrNames servers));
default = defaultServers;
2023-09-23 18:36:25 +03:00
};
};
format = {
enable = mkOption {
description = "Enable Bash formatting";
type = bool;
2023-09-23 18:36:25 +03:00
default = config.vim.languages.enableFormat;
};
type = mkOption {
description = "Bash formatter to use";
type = enum (attrNames formats);
2023-09-23 18:36:25 +03:00
default = defaultFormat;
};
package = mkOption {
description = "Bash formatter package";
type = package;
2023-09-23 18:36:25 +03:00
default = formats.${cfg.format.type}.package;
};
};
extraDiagnostics = {
enable = mkEnableOption "extra Bash diagnostics" // {default = config.vim.languages.enableExtraDiagnostics;};
2024-03-23 20:14:39 -04:00
types = diagnostics {
2023-09-23 18:36:25 +03:00
langDesc = "Bash";
2024-03-23 20:14:39 -04:00
inherit diagnosticsProviders;
inherit defaultDiagnosticsProvider;
2023-09-23 18:36:25 +03:00
};
};
};
config = mkIf cfg.enable (mkMerge [
(mkIf cfg.treesitter.enable {
vim.treesitter.enable = true;
vim.treesitter.grammars = [cfg.treesitter.package];
})
(mkIf cfg.lsp.enable {
2025-06-02 23:56:40 +01:00
vim.lsp.servers =
mapListToAttrs (n: {
name = n;
value = servers.${n};
})
cfg.lsp.servers;
})
(mkIf cfg.format.enable {
vim.formatter.conform-nvim = {
enable = true;
setupOpts.formatters_by_ft.sh = [cfg.format.type];
setupOpts.formatters.${cfg.format.type} = {
command = getExe cfg.format.package;
};
};
})
(mkIf cfg.extraDiagnostics.enable {
vim.diagnostics.nvim-lint = {
enable = true;
linters_by_ft.sh = cfg.extraDiagnostics.types;
linters = mkMerge (map (name: {
${name}.cmd = getExe diagnosticsProviders.${name}.package;
})
cfg.extraDiagnostics.types);
};
})
]);
2023-09-23 18:36:25 +03:00
}