2023-04-17 20:27:27 +00:00
|
|
|
{
|
|
|
|
config,
|
2024-03-09 05:49:22 +00:00
|
|
|
pkgs,
|
2023-04-17 20:27:27 +00:00
|
|
|
lib,
|
|
|
|
...
|
2023-11-08 02:16:46 +00:00
|
|
|
}: let
|
2023-11-14 07:18:32 +00:00
|
|
|
inherit (builtins) attrNames;
|
2024-03-09 05:49:22 +00:00
|
|
|
inherit (lib.options) mkEnableOption mkOption;
|
|
|
|
inherit (lib.modules) mkIf mkMerge;
|
|
|
|
inherit (lib.lists) isList;
|
|
|
|
inherit (lib.types) enum either listOf package str;
|
|
|
|
inherit (lib.nvim.lua) expToLua;
|
2024-03-24 00:14:39 +00:00
|
|
|
inherit (lib.nvim.languages) diagnosticsToLua;
|
|
|
|
inherit (lib.nvim.types) diagnostics;
|
2023-11-08 02:16:46 +00:00
|
|
|
|
2023-04-17 20:27:27 +00:00
|
|
|
cfg = config.vim.languages.sql;
|
|
|
|
sqlfluffDefault = pkgs.sqlfluff;
|
|
|
|
|
|
|
|
defaultServer = "sqls";
|
|
|
|
servers = {
|
|
|
|
sqls = {
|
|
|
|
package = pkgs.sqls;
|
|
|
|
lspConfig = ''
|
|
|
|
lspconfig.sqls.setup {
|
|
|
|
on_attach = function(client)
|
|
|
|
client.server_capabilities.execute_command = true
|
|
|
|
on_attach_keymaps(client, bufnr)
|
|
|
|
require'sqls'.setup{}
|
|
|
|
end,
|
2023-09-18 23:26:29 +00:00
|
|
|
cmd = ${
|
|
|
|
if isList cfg.lsp.package
|
2024-03-09 05:49:22 +00:00
|
|
|
then expToLua cfg.lsp.package
|
2023-09-18 23:26:29 +00:00
|
|
|
else ''{ "${cfg.lsp.package}/bin/sqls", "-config", string.format("%s/config.yml", vim.fn.getcwd()) }''
|
|
|
|
}
|
2023-04-17 20:27:27 +00:00
|
|
|
}
|
|
|
|
'';
|
|
|
|
};
|
|
|
|
};
|
|
|
|
|
|
|
|
defaultFormat = "sqlfluff";
|
|
|
|
formats = {
|
|
|
|
sqlfluff = {
|
|
|
|
package = sqlfluffDefault;
|
|
|
|
nullConfig = ''
|
|
|
|
table.insert(
|
|
|
|
ls_sources,
|
|
|
|
null_ls.builtins.formatting.sqlfluff.with({
|
|
|
|
command = "${cfg.format.package}/bin/sqlfluff",
|
|
|
|
extra_args = {"--dialect", "${cfg.dialect}"}
|
|
|
|
})
|
|
|
|
)
|
|
|
|
'';
|
|
|
|
};
|
|
|
|
};
|
|
|
|
|
2024-03-24 00:14:39 +00:00
|
|
|
defaultDiagnosticsProvider = ["sqlfluff"];
|
|
|
|
diagnosticsProviders = {
|
2023-04-17 20:27:27 +00:00
|
|
|
sqlfluff = {
|
|
|
|
package = sqlfluffDefault;
|
|
|
|
nullConfig = pkg: ''
|
|
|
|
table.insert(
|
|
|
|
ls_sources,
|
|
|
|
null_ls.builtins.diagnostics.sqlfluff.with({
|
|
|
|
command = "${pkg}/bin/sqlfluff",
|
|
|
|
extra_args = {"--dialect", "${cfg.dialect}"}
|
|
|
|
})
|
|
|
|
)
|
|
|
|
'';
|
|
|
|
};
|
|
|
|
};
|
|
|
|
in {
|
|
|
|
options.vim.languages.sql = {
|
|
|
|
enable = mkEnableOption "SQL language support";
|
|
|
|
|
|
|
|
dialect = mkOption {
|
|
|
|
description = "SQL dialect for sqlfluff (if used)";
|
2024-03-09 05:49:22 +00:00
|
|
|
type = str;
|
2023-04-17 20:27:27 +00:00
|
|
|
default = "ansi";
|
|
|
|
};
|
|
|
|
|
|
|
|
treesitter = {
|
2023-10-21 17:15:36 +00:00
|
|
|
enable = mkEnableOption "SQL treesitter" // {default = config.vim.languages.enableTreesitter;};
|
2023-06-05 20:10:25 +00:00
|
|
|
|
2023-04-17 20:27:27 +00:00
|
|
|
package = mkOption {
|
|
|
|
description = "SQL treesitter grammar to use";
|
2024-03-09 05:49:22 +00:00
|
|
|
type = package;
|
2023-04-17 20:27:27 +00:00
|
|
|
default = pkgs.vimPlugins.nvim-treesitter.builtGrammars.sql;
|
|
|
|
};
|
|
|
|
};
|
|
|
|
|
|
|
|
lsp = {
|
2023-10-21 17:15:36 +00:00
|
|
|
enable = mkEnableOption "SQL LSP support" // {default = config.vim.languages.enableLSP;};
|
2023-06-05 20:10:25 +00:00
|
|
|
|
2023-04-17 20:27:27 +00:00
|
|
|
server = mkOption {
|
|
|
|
description = "SQL LSP server to use";
|
2024-03-09 05:49:22 +00:00
|
|
|
type = enum (attrNames servers);
|
2023-04-17 20:27:27 +00:00
|
|
|
default = defaultServer;
|
|
|
|
};
|
2023-06-05 20:10:25 +00:00
|
|
|
|
2023-04-17 20:27:27 +00:00
|
|
|
package = mkOption {
|
2023-09-22 22:28:39 +00:00
|
|
|
description = "SQL LSP server package, or the command to run as a list of strings";
|
|
|
|
example = ''[lib.getExe pkgs.jdt-language-server "-data" "~/.cache/jdtls/workspace"]'';
|
2024-03-09 05:49:22 +00:00
|
|
|
type = either package (listOf str);
|
2023-04-17 20:27:27 +00:00
|
|
|
default = servers.${cfg.lsp.server}.package;
|
|
|
|
};
|
|
|
|
};
|
|
|
|
|
|
|
|
format = {
|
2023-10-21 17:15:36 +00:00
|
|
|
enable = mkEnableOption "SQL formatting" // {default = config.vim.languages.enableFormat;};
|
2023-06-05 20:10:25 +00:00
|
|
|
|
2023-04-17 20:27:27 +00:00
|
|
|
type = mkOption {
|
|
|
|
description = "SQL formatter to use";
|
2024-03-09 05:49:22 +00:00
|
|
|
type = enum (attrNames formats);
|
2023-04-17 20:27:27 +00:00
|
|
|
default = defaultFormat;
|
|
|
|
};
|
2023-06-05 20:10:25 +00:00
|
|
|
|
2023-04-17 20:27:27 +00:00
|
|
|
package = mkOption {
|
|
|
|
description = "SQL formatter package";
|
2024-03-09 05:49:22 +00:00
|
|
|
type = package;
|
2023-04-17 20:27:27 +00:00
|
|
|
default = formats.${cfg.format.type}.package;
|
|
|
|
};
|
|
|
|
};
|
|
|
|
|
|
|
|
extraDiagnostics = {
|
2023-10-21 17:15:36 +00:00
|
|
|
enable = mkEnableOption "extra SQL diagnostics" // {default = config.vim.languages.enableExtraDiagnostics;};
|
2023-06-05 20:10:25 +00:00
|
|
|
|
2024-03-24 00:14:39 +00:00
|
|
|
types = diagnostics {
|
2023-04-17 20:27:27 +00:00
|
|
|
langDesc = "SQL";
|
2024-03-24 00:14:39 +00:00
|
|
|
inherit diagnosticsProviders;
|
|
|
|
inherit defaultDiagnosticsProvider;
|
2023-04-17 20:27:27 +00:00
|
|
|
};
|
|
|
|
};
|
|
|
|
};
|
|
|
|
|
|
|
|
config = mkIf cfg.enable (mkMerge [
|
|
|
|
(mkIf cfg.treesitter.enable {
|
|
|
|
vim.treesitter.enable = true;
|
|
|
|
vim.treesitter.grammars = [cfg.treesitter.package];
|
|
|
|
})
|
|
|
|
|
|
|
|
(mkIf cfg.lsp.enable {
|
2024-03-09 05:49:22 +00:00
|
|
|
vim = {
|
|
|
|
startPlugins = ["sqls-nvim"];
|
2023-04-17 20:27:27 +00:00
|
|
|
|
2024-03-09 05:49:22 +00:00
|
|
|
lsp.lspconfig = {
|
|
|
|
enable = true;
|
|
|
|
sources.sql-lsp = servers.${cfg.lsp.server}.lspConfig;
|
|
|
|
};
|
|
|
|
};
|
2023-04-17 20:27:27 +00:00
|
|
|
})
|
|
|
|
|
|
|
|
(mkIf cfg.format.enable {
|
|
|
|
vim.lsp.null-ls.enable = true;
|
|
|
|
vim.lsp.null-ls.sources."sql-format" = formats.${cfg.format.type}.nullConfig;
|
|
|
|
})
|
|
|
|
|
|
|
|
(mkIf cfg.extraDiagnostics.enable {
|
|
|
|
vim.lsp.null-ls.enable = true;
|
2024-03-24 00:14:39 +00:00
|
|
|
vim.lsp.null-ls.sources = diagnosticsToLua {
|
2023-04-17 20:27:27 +00:00
|
|
|
lang = "sql";
|
|
|
|
config = cfg.extraDiagnostics.types;
|
2024-03-24 00:14:39 +00:00
|
|
|
inherit diagnosticsProviders;
|
2023-04-17 20:27:27 +00:00
|
|
|
};
|
|
|
|
})
|
|
|
|
]);
|
|
|
|
}
|