convert ocaml module

This commit is contained in:
sjcobb 2025-07-08 09:18:47 +01:00
commit a0f396aa28

View file

@ -8,27 +8,49 @@
inherit (lib.options) mkEnableOption mkOption; inherit (lib.options) mkEnableOption mkOption;
inherit (lib.modules) mkIf mkMerge; inherit (lib.modules) mkIf mkMerge;
inherit (lib.meta) getExe; inherit (lib.meta) getExe;
inherit (lib.lists) isList; inherit (lib.types) enum listOf package;
inherit (lib.types) either enum listOf package str;
inherit (lib.nvim.types) mkGrammarOption; inherit (lib.nvim.types) mkGrammarOption;
inherit (lib.nvim.lua) expToLua; inherit (lib.generators) mkLuaInline;
inherit (lib.nvim.attrsets) mapListToAttrs;
cfg = config.vim.languages.ocaml; cfg = config.vim.languages.ocaml;
defaultServer = "ocaml-lsp"; defaultServers = ["ocaml-lsp"];
servers = { servers = {
ocaml-lsp = { ocaml-lsp = {
package = pkgs.ocamlPackages.ocaml-lsp; enable = true;
lspConfig = '' cmd = [(getExe pkgs.ocamlPackages.ocaml-lsp)];
lspconfig.ocamllsp.setup { filetypes = ["ocaml" "menhir" "ocamlinterface" "ocamllex" "reason" "dune"];
capabilities = capabilities, root_dir =
on_attach = default_on_attach, mkLuaInline
cmd = ${ /*
if isList cfg.lsp.package lua
then expToLua cfg.lsp.package */
else ''{"${getExe cfg.lsp.package}"}'' ''
}; function(bufnr, on_dir)
local fname = vim.api.nvim_buf_get_name(bufnr)
on_dir(util.root_pattern('*.opam', 'esy.json', 'package.json', '.git', 'dune-project', 'dune-workspace')(fname))
end
'';
get_language_id =
mkLuaInline
/*
lua
*/
''
function(_, ftype)
local language_id_of = {
menhir = 'ocaml.menhir',
ocaml = 'ocaml',
ocamlinterface = 'ocaml.interface',
ocamllex = 'ocaml.ocamllex',
reason = 'reason',
dune = 'dune',
} }
return language_id_of[ftype]
end
''; '';
}; };
}; };
@ -49,16 +71,12 @@ in {
}; };
lsp = { lsp = {
enable = mkEnableOption "OCaml LSP support (ocaml-lsp)" // {default = config.vim.lsp.enable;}; enable = mkEnableOption "OCaml LSP support" // {default = config.vim.lsp.enable;};
server = mkOption {
description = "OCaml LSP server to user"; servers = mkOption {
type = enum (attrNames servers); description = "OCaml LSP server to use";
default = defaultServer; type = listOf (enum (attrNames servers));
}; default = defaultServers;
package = mkOption {
description = "OCaml language server package, or the command to run as a list of strings";
type = either package (listOf str);
default = servers.${cfg.lsp.server}.package;
}; };
}; };
@ -79,8 +97,12 @@ in {
config = mkIf cfg.enable (mkMerge [ config = mkIf cfg.enable (mkMerge [
(mkIf cfg.lsp.enable { (mkIf cfg.lsp.enable {
vim.lsp.lspconfig.enable = true; vim.lsp.servers =
vim.lsp.lspconfig.sources.ocaml-lsp = servers.${cfg.lsp.server}.lspConfig; mapListToAttrs (n: {
name = n;
value = servers.${n};
})
cfg.lsp.servers;
}) })
(mkIf cfg.treesitter.enable { (mkIf cfg.treesitter.enable {