Skip to content

Commit

Permalink
Merge pull request #292 from jacekpoz/main
Browse files Browse the repository at this point in the history
languages/ocaml: init
  • Loading branch information
NotAShelf authored May 16, 2024
2 parents 493854e + 7224c1e commit 74df3a5
Show file tree
Hide file tree
Showing 4 changed files with 111 additions and 0 deletions.
1 change: 1 addition & 0 deletions configuration.nix
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
4 changes: 4 additions & 0 deletions docs/release-notes/rl-0.7.md
Original file line number Diff line number Diff line change
Expand Up @@ -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.
1 change: 1 addition & 0 deletions modules/plugins/languages/default.nix
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ in {
./markdown.nix
./nim.nix
./nix.nix
./ocaml.nix
./php.nix
./python.nix
./rust.nix
Expand Down
105 changes: 105 additions & 0 deletions modules/plugins/languages/ocaml.nix
Original file line number Diff line number Diff line change
@@ -0,0 +1,105 @@
{
config,
pkgs,
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 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";

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;};
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";
type = either package (listOf str);
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 = formats.${cfg.format.type}.package;
};
};
};

config = mkIf cfg.enable (mkMerge [
(mkIf cfg.lsp.enable {
vim.lsp.lspconfig.enable = true;
vim.lsp.lspconfig.sources.ocaml-lsp = servers.${cfg.lsp.server}.lspConfig;
})

(mkIf cfg.treesitter.enable {
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 = formats.${cfg.format.type}.nullConfig;
vim.extraPackages = [formats.${cfg.format.type}.package];
})
]);
}

0 comments on commit 74df3a5

Please sign in to comment.