nvf/modules/plugins/languages/go.nix
NotAShelf 5bc627f4e4
Some checks failed
Check for typos in the source tree / check-typos (push) Has been cancelled
languages: convert modules to new LSP option set format
2025-03-19 10:20:16 +03:00

190 lines
4.9 KiB
Nix

{
config,
pkgs,
lib,
...
}: let
inherit (builtins) attrNames;
inherit (lib.options) mkEnableOption mkOption;
inherit (lib.modules) mkIf mkMerge;
inherit (lib.types) bool enum either listOf package str;
inherit (lib.lists) isList;
inherit (lib.meta) getExe;
inherit (lib.generators) mkLuaInline;
inherit (lib.nvim.types) mkGrammarOption;
inherit (lib.nvim.languages) lspOptions;
inherit (lib.nvim.lua) expToLua toLuaObject;
inherit (lib.nvim.dag) entryAfter;
cfg = config.vim.languages.go;
defaultServer = "gopls";
servers = {
gopls = {
package = pkgs.gopls;
options = {
capabilities = mkLuaInline "capabilities";
on_attach = mkLuaInline "default_on_attach";
filetypes = ["go" "gomod" "gowork" "gotmpl"];
cmd =
if isList cfg.lsp.package
then expToLua cfg.lsp.package
else ["${getExe cfg.lsp.package}" "serve"];
single_file_support = true;
};
};
};
defaultFormat = "gofmt";
formats = {
gofmt = {
package = pkgs.go;
nullConfig = ''
table.insert(
ls_sources,
null_ls.builtins.formatting.gofmt.with({
command = "${cfg.format.package}/bin/gofmt",
})
)
'';
};
gofumpt = {
package = pkgs.gofumpt;
nullConfig = ''
table.insert(
ls_sources,
null_ls.builtins.formatting.gofumpt.with({
command = "${cfg.format.package}/bin/gofumpt",
})
)
'';
};
golines = {
package = pkgs.golines;
nullConfig = ''
table.insert(
ls_sources,
null_ls.builtins.formatting.golines.with({
command = "${cfg.format.package}/bin/golines",
})
)
'';
};
};
defaultDebugger = "delve";
debuggers = {
delve = {
package = pkgs.delve;
};
};
in {
options.vim.languages.go = {
enable = mkEnableOption "Go language support";
treesitter = {
enable = mkEnableOption "Go treesitter" // {default = config.vim.languages.enableTreesitter;};
package = mkGrammarOption pkgs "go";
};
lsp = {
enable = mkEnableOption "Go LSP support" // {default = config.vim.languages.enableLSP;};
server = mkOption {
description = "Go LSP server to use";
type = enum (attrNames servers);
default = defaultServer;
};
package = mkOption {
description = "Go LSP server package, or the command to run as a list of strings";
example = ''[lib.getExe pkgs.jdt-language-server " - data " " ~/.cache/jdtls/workspace "]'';
type = either package (listOf str);
default = servers.${cfg.lsp.server}.package;
};
options = mkOption {
type = lspOptions;
default = servers.${cfg.lsp.server}.options;
description = ''
LSP options for Go language support.
This option is freeform, you may add options that are not set by default
and they will be merged into the final table passed to lspconfig.
'';
};
};
format = {
enable = mkEnableOption "Go formatting" // {default = config.vim.languages.enableFormat;};
type = mkOption {
description = "Go formatter to use";
type = enum (attrNames formats);
default = defaultFormat;
};
package = mkOption {
description = "Go formatter package";
type = package;
default = formats.${cfg.format.type}.package;
};
};
dap = {
enable = mkOption {
description = "Enable Go Debug Adapter via nvim-dap-go plugin";
type = bool;
default = config.vim.languages.enableDAP;
};
debugger = mkOption {
description = "Go debugger to use";
type = enum (attrNames debuggers);
default = defaultDebugger;
};
package = mkOption {
description = "Go debugger package.";
type = package;
default = debuggers.${cfg.dap.debugger}.package;
};
};
};
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;
sources.go-lsp = ''
lspconfig.${toLuaObject cfg.lsp.server}.setup(${toLuaObject cfg.lsp.options})
'';
};
})
(mkIf cfg.format.enable {
vim.lsp.null-ls.enable = true;
vim.lsp.null-ls.sources.go-format = formats.${cfg.format.type}.nullConfig;
})
(mkIf cfg.dap.enable {
vim = {
startPlugins = ["nvim-dap-go"];
pluginRC.nvim-dap-go = entryAfter ["nvim-dap"] ''
require('dap-go').setup {
delve = {
path = '${getExe cfg.dap.package}',
}
}
'';
debugger.nvim-dap.enable = true;
};
})
]);
}