From 208d1e67414dc189168697a639f118d69b98efd1 Mon Sep 17 00:00:00 2001 From: jacekpoz Date: Thu, 16 May 2024 14:25:07 +0200 Subject: [PATCH] languages/ocaml: follow other languages' format --- modules/plugins/languages/ocaml.nix | 73 ++++++++++++++++++++--------- 1 file changed, 50 insertions(+), 23 deletions(-) diff --git a/modules/plugins/languages/ocaml.nix b/modules/plugins/languages/ocaml.nix index 1817cc4..4ab901b 100644 --- a/modules/plugins/languages/ocaml.nix +++ b/modules/plugins/languages/ocaml.nix @@ -4,15 +4,49 @@ lib, ... }: let + inherit (builtins) attrNames; inherit (lib.options) mkEnableOption mkOption; inherit (lib.modules) mkIf mkMerge; inherit (lib.meta) getExe; inherit (lib.lists) isList; - inherit (lib.types) either listOf package str; + inherit (lib.types) either enum listOf package str; inherit (lib.nvim.types) mkGrammarOption; inherit (lib.nvim.lua) expToLua; cfg = config.vim.languages.ocaml; + + defaultServer = "ocaml-lsp"; + servers = { + ocaml-lsp = { + package = pkgs.ocamlPackages.ocaml-lsp; + lspConfig = '' + lspconfig.ocamllsp.setup { + capabilities = capabilities, + on_attach = default_on_attach, + cmd = ${ + if isList cfg.lsp.package + then expToLua cfg.lsp.package + else ''{"${getExe cfg.lsp.package}"}'' + }; + } + ''; + }; + }; + + defaultFormat = "ocamlformat"; + formats = { + ocamlformat = { + package = pkgs.ocamlPackages.ocamlformat; + nullConfig = '' + table.insert( + ls_sources, + null_ls.builtins.formatting.ocamlformat.with({ + command = "${cfg.format.package}/bin/ocamlformat", + }) + ) + ''; + }; + }; in { options.vim.languages.ocaml = { enable = mkEnableOption "OCaml language support"; @@ -24,19 +58,29 @@ in { lsp = { enable = mkEnableOption "OCaml LSP support (ocaml-lsp)" // {default = config.vim.languages.enableLSP;}; + server = mkOption { + description = "OCaml LSP server to user"; + type = enum (attrNames servers); + default = defaultServer; + }; package = mkOption { - description = "ocaml language server package, or the command to run as a list of strings"; + description = "OCaml language server package, or the command to run as a list of strings"; type = either package (listOf str); - default = pkgs.ocamlPackages.ocaml-lsp; + default = servers.${cfg.lsp.server}.package; }; }; format = { enable = mkEnableOption "OCaml formatting support (ocamlformat)" // {default = config.vim.languages.enableFormat;}; + type = mkOption { + description = "OCaml formatter to use"; + type = enum (attrNames formats); + default = defaultFormat; + }; package = mkOption { description = "OCaml formatter package"; type = package; - default = pkgs.ocamlPackages.ocamlformat; + default = formats.${cfg.format.type}.package; }; }; }; @@ -44,17 +88,7 @@ in { config = mkIf cfg.enable (mkMerge [ (mkIf cfg.lsp.enable { vim.lsp.lspconfig.enable = true; - vim.lsp.lspconfig.sources.ocaml-lsp = '' - lspconfig.ocamllsp.setup { - capabilities = capabilities, - on_attach = default_on_attach, - cmd = ${ - if isList cfg.lsp.package - then expToLua cfg.lsp.package - else ''{"${getExe cfg.lsp.package}"}'' - }; - } - ''; + vim.lsp.lspconfig.sources.ocaml-lsp = servers.${cfg.lsp.server}.lspConfig; }) (mkIf cfg.treesitter.enable { @@ -64,14 +98,7 @@ in { (mkIf cfg.format.enable { vim.lsp.null-ls.enable = true; - vim.lsp.null-ls.sources.ocamlformat = '' - table.insert( - ls_sources, - null_ls.builtins.formatting.ocamlformat.with({ - command = "${cfg.format.package}/bin/ocamlformat", - }) - ) - ''; + vim.lsp.null-ls.sources.ocamlformat = formats.${cfg.format.type}.nullConfig; }) ]); }