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

138 lines
3.6 KiB
Nix
Raw Permalink Normal View History

2023-09-23 17:36:25 +02:00
{
config,
pkgs,
2023-09-23 17:36:25 +02:00
lib,
...
}: let
inherit (builtins) attrNames;
inherit (lib.options) mkOption mkEnableOption literalExpression;
inherit (lib.modules) mkIf mkMerge;
inherit (lib.lists) isList;
inherit (lib.types) enum either package listOf str bool;
2024-10-16 04:42:01 +02:00
inherit (lib.nvim.languages) diagnosticsToLua mkLspConfig;
2024-03-24 01:14:39 +01:00
inherit (lib.nvim.types) diagnostics mkGrammarOption;
inherit (lib.nvim.lua) expToLua;
2023-09-23 17:36:25 +02:00
cfg = config.vim.languages.bash;
2024-10-16 04:42:01 +02:00
defaultServer = "bashls";
2023-09-23 17:36:25 +02:00
servers = {
2024-10-16 04:42:01 +02:00
bashls = mkLspConfig {
name = "bashls";
package = pkgs.bash-language-server;
2024-10-16 04:42:01 +02:00
args = ["start"];
2023-09-23 17:36:25 +02:00
};
};
defaultFormat = "shfmt";
formats = {
shfmt = {
package = pkgs.shfmt;
nullConfig = ''
table.insert(
ls_sources,
null_ls.builtins.formatting.shfmt.with({
command = "${pkgs.shfmt}/bin/shfmt",
})
)
'';
};
};
2024-03-24 01:14:39 +01:00
defaultDiagnosticsProvider = ["shellcheck"];
diagnosticsProviders = {
2023-09-23 17:36:25 +02:00
shellcheck = {
package = pkgs.shellcheck;
nullConfig = pkg: ''
table.insert(
ls_sources,
null_ls.builtins.diagnostics.shellcheck.with({
command = "${pkg}/bin/shellcheck",
diagnostics_format = "#{m} [#{c}]"
2023-09-23 17:36:25 +02:00
})
)
'';
};
};
in {
options.vim.languages.bash = {
enable = mkEnableOption "Bash language support";
treesitter = {
enable = mkEnableOption "Bash treesitter" // {default = config.vim.languages.enableTreesitter;};
2024-03-24 01:14:39 +01:00
package = mkGrammarOption pkgs "bash";
2023-09-23 17:36:25 +02:00
};
lsp = {
enable = mkEnableOption "Enable Bash LSP support" // {default = config.vim.languages.enableLSP;};
server = mkOption {
description = "Bash LSP server to use";
type = enum (attrNames servers);
2023-09-23 17:36:25 +02:00
default = defaultServer;
};
package = mkOption {
description = "bash-language-server package, or the command to run as a list of strings";
example = literalExpression ''[lib.getExe pkgs.nodePackages.bash-language-server "start"]'';
type = either package (listOf str);
default = pkgs.bash-language-server;
2023-09-23 17:36:25 +02:00
};
};
format = {
enable = mkOption {
description = "Enable Bash formatting";
type = bool;
2023-09-23 17:36:25 +02:00
default = config.vim.languages.enableFormat;
};
type = mkOption {
description = "Bash formatter to use";
type = enum (attrNames formats);
2023-09-23 17:36:25 +02:00
default = defaultFormat;
};
package = mkOption {
description = "Bash formatter package";
type = package;
2023-09-23 17:36:25 +02:00
default = formats.${cfg.format.type}.package;
};
};
extraDiagnostics = {
enable = mkEnableOption "extra Bash diagnostics" // {default = config.vim.languages.enableExtraDiagnostics;};
2024-03-24 01:14:39 +01:00
types = diagnostics {
2023-09-23 17:36:25 +02:00
langDesc = "Bash";
2024-03-24 01:14:39 +01:00
inherit diagnosticsProviders;
inherit defaultDiagnosticsProvider;
2023-09-23 17:36:25 +02:00
};
};
};
config = mkIf cfg.enable (mkMerge [
(mkIf cfg.treesitter.enable {
vim.treesitter.enable = true;
vim.treesitter.grammars = [cfg.treesitter.package];
})
(mkIf cfg.lsp.enable {
vim.lsp.lspconfig.enable = true;
vim.lsp.lspconfig.sources.bash-lsp = servers.${cfg.lsp.server}.lspConfig;
})
(mkIf cfg.format.enable {
vim.lsp.null-ls.enable = true;
vim.lsp.null-ls.sources.bash-format = formats.${cfg.format.type}.nullConfig;
})
(mkIf cfg.extraDiagnostics.enable {
vim.lsp.null-ls.enable = true;
vim.lsp.null-ls.sources = diagnosticsToLua {
lang = "bash";
config = cfg.extraDiagnostics.types;
inherit diagnosticsProviders;
};
})
]);
2023-09-23 17:36:25 +02:00
}