From db810bbf9f2d38ec92dc415aab7ac09fdca3913e Mon Sep 17 00:00:00 2001 From: jacekpoz Date: Wed, 15 May 2024 19:37:27 +0200 Subject: [PATCH 1/5] languages/ocaml: init --- docs/release-notes/rl-0.7.md | 4 ++ modules/plugins/languages/default.nix | 1 + modules/plugins/languages/ocaml.nix | 56 +++++++++++++++++++++++++++ 3 files changed, 61 insertions(+) create mode 100644 modules/plugins/languages/ocaml.nix diff --git a/docs/release-notes/rl-0.7.md b/docs/release-notes/rl-0.7.md index 8f3ffd2..0a304dd 100644 --- a/docs/release-notes/rl-0.7.md +++ b/docs/release-notes/rl-0.7.md @@ -38,3 +38,7 @@ Release notes for release 0.7 - Cleaned up Lualine module to reduce theme dependency on Catppuccin, and fixed blending issues in component separators. + +[jacekpoz](https://github.com/jacekpoz): + +- Added [ocaml-lsp](https://github.com/ocaml/ocaml-lsp) support. diff --git a/modules/plugins/languages/default.nix b/modules/plugins/languages/default.nix index 279d46b..e86a521 100644 --- a/modules/plugins/languages/default.nix +++ b/modules/plugins/languages/default.nix @@ -14,6 +14,7 @@ in { ./markdown.nix ./nim.nix ./nix.nix + ./ocaml.nix ./php.nix ./python.nix ./rust.nix diff --git a/modules/plugins/languages/ocaml.nix b/modules/plugins/languages/ocaml.nix new file mode 100644 index 0000000..c6ff5a5 --- /dev/null +++ b/modules/plugins/languages/ocaml.nix @@ -0,0 +1,56 @@ +{ + config, + pkgs, + lib, + ... +}: let + 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.nvim.types) mkGrammarOption; + inherit (lib.nvim.lua) expToLua; + + cfg = config.vim.languages.ocaml; +in { + options.vim.languages.ocaml = { + enable = mkEnableOption "OCaml language support"; + + treesitter = { + enable = mkEnableOption "OCaml treesitter" // {default = config.vim.languages.enableTreesitter;}; + package = mkGrammarOption pkgs "ocaml"; + }; + + lsp = { + enable = mkEnableOption "OCaml LSP support (ocaml-lsp)" // {default = config.vim.languages.enableLSP;}; + package = mkOption { + 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; + }; + }; + }; + + 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}"}'' + }; + } + ''; + }) + + (mkIf cfg.treesitter.enable { + vim.treesitter.enable = true; + vim.treesitter.grammars = [cfg.treesitter.package]; + }) + ]); +} From cc0aba4e218a17fd81b10d957fcd4b5caaf5572e Mon Sep 17 00:00:00 2001 From: jacekpoz Date: Wed, 15 May 2024 20:55:04 +0200 Subject: [PATCH 2/5] languages/ocaml: add formatting support --- modules/plugins/languages/ocaml.nix | 21 +++++++++++++++++++++ 1 file changed, 21 insertions(+) diff --git a/modules/plugins/languages/ocaml.nix b/modules/plugins/languages/ocaml.nix index c6ff5a5..1817cc4 100644 --- a/modules/plugins/languages/ocaml.nix +++ b/modules/plugins/languages/ocaml.nix @@ -30,6 +30,15 @@ in { default = pkgs.ocamlPackages.ocaml-lsp; }; }; + + format = { + enable = mkEnableOption "OCaml formatting support (ocamlformat)" // {default = config.vim.languages.enableFormat;}; + package = mkOption { + description = "OCaml formatter package"; + type = package; + default = pkgs.ocamlPackages.ocamlformat; + }; + }; }; config = mkIf cfg.enable (mkMerge [ @@ -52,5 +61,17 @@ in { vim.treesitter.enable = true; vim.treesitter.grammars = [cfg.treesitter.package]; }) + + (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", + }) + ) + ''; + }) ]); } From 208d1e67414dc189168697a639f118d69b98efd1 Mon Sep 17 00:00:00 2001 From: jacekpoz Date: Thu, 16 May 2024 14:25:07 +0200 Subject: [PATCH 3/5] 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; }) ]); } From f343ab8b3ab42eb7cad63574ac2179d36e3d2578 Mon Sep 17 00:00:00 2001 From: jacekpoz Date: Thu, 16 May 2024 14:25:23 +0200 Subject: [PATCH 4/5] languages/ocaml: add formatter to nvf's env --- modules/plugins/languages/ocaml.nix | 1 + 1 file changed, 1 insertion(+) diff --git a/modules/plugins/languages/ocaml.nix b/modules/plugins/languages/ocaml.nix index 4ab901b..e7064eb 100644 --- a/modules/plugins/languages/ocaml.nix +++ b/modules/plugins/languages/ocaml.nix @@ -99,6 +99,7 @@ in { (mkIf cfg.format.enable { vim.lsp.null-ls.enable = true; vim.lsp.null-ls.sources.ocamlformat = formats.${cfg.format.type}.nullConfig; + vim.extraPackages = [ formats.${cfg.format.type}.package ]; }) ]); } From 7224c1e6afd23535f8607d292c6d9c70699f752e Mon Sep 17 00:00:00 2001 From: jacekpoz Date: Thu, 16 May 2024 14:36:01 +0200 Subject: [PATCH 5/5] languages/ocaml: add ocaml to maximal config --- configuration.nix | 1 + modules/plugins/languages/ocaml.nix | 10 +++++----- 2 files changed, 6 insertions(+), 5 deletions(-) diff --git a/configuration.nix b/configuration.nix index e2c02f7..1d81459 100644 --- a/configuration.nix +++ b/configuration.nix @@ -73,6 +73,7 @@ inputs: let go.enable = isMaximal; elixir.enable = isMaximal; zig.enable = isMaximal; + ocaml.enable = isMaximal; python.enable = isMaximal; dart.enable = isMaximal; bash.enable = isMaximal; diff --git a/modules/plugins/languages/ocaml.nix b/modules/plugins/languages/ocaml.nix index e7064eb..ddfa85e 100644 --- a/modules/plugins/languages/ocaml.nix +++ b/modules/plugins/languages/ocaml.nix @@ -24,10 +24,10 @@ capabilities = capabilities, on_attach = default_on_attach, cmd = ${ - if isList cfg.lsp.package - then expToLua cfg.lsp.package - else ''{"${getExe cfg.lsp.package}"}'' - }; + if isList cfg.lsp.package + then expToLua cfg.lsp.package + else ''{"${getExe cfg.lsp.package}"}'' + }; } ''; }; @@ -99,7 +99,7 @@ in { (mkIf cfg.format.enable { vim.lsp.null-ls.enable = true; vim.lsp.null-ls.sources.ocamlformat = formats.${cfg.format.type}.nullConfig; - vim.extraPackages = [ formats.${cfg.format.type}.package ]; + vim.extraPackages = [formats.${cfg.format.type}.package]; }) ]); }