nixeovim/modules/plugins/nvim-cmp.nix

105 lines
3.1 KiB
Nix
Raw Normal View History

2024-05-09 02:28:24 +03:00
{ config, lib, pkgs, ... }:
let
inherit (lib.nix2lua) pipe1 var call call0;
cfg = config.plugins.nvim-cmp;
2024-05-09 18:41:09 +03:00
inherit (config.plugins) snippet language-server;
2024-05-09 02:28:24 +03:00
in
{
options.plugins.nvim-cmp = with lib; {
enable = mkEnableOption "nvim-cmp";
package = mkPackageOption pkgs.vimPlugins "nvim-cmp" { };
settings = mkOption {
type = types.attrs;
default = { };
};
};
2024-05-09 18:41:09 +03:00
2024-05-09 02:28:24 +03:00
config = lib.mkIf cfg.enable (lib.mkMerge [
{
2024-05-09 18:41:09 +03:00
assertions = [
{
assertion = snippet.luasnip.enable || snippet.snippy.enable || snippet.vsnip.enable || snippet.ultisnips.enable;
message = "One of snippet backend (luasnip, snippy, vsnip, ultisnip) is required to use nvim-cmp";
}
];
}
2024-05-09 02:28:24 +03:00
2024-05-09 18:41:09 +03:00
# Backend dependencies
(lib.mkIf snippet.luasnip.enable {
plugin.cmp_luasnip = { };
2024-05-09 18:41:09 +03:00
})
(lib.mkIf snippet.snippy.enable {
plugin.cmp-snippy = { };
2024-05-09 18:41:09 +03:00
})
(lib.mkIf snippet.vsnip.enable {
plugin.cmp-vsnip = { };
2024-05-09 18:41:09 +03:00
})
(lib.mkIf snippet.ultisnips.enable {
plugin.cmp-nvim-ultisnips = { };
2024-05-09 18:41:09 +03:00
})
# Lsp configuration
(lib.mkIf language-server.lspconfig.enable {
plugin.cmp-nvim-lsp = {
name = "cmp_nvim_lsp";
2024-05-09 02:28:24 +03:00
};
2024-05-09 18:41:09 +03:00
plugins.language-server.lspconfig.defaultServerSettings.capabilities =
pipe1 config.plugin.cmp-nvim-lsp.var (call0 "default_capabilities");
2024-05-09 18:41:09 +03:00
})
# Base configuration
{
2024-05-09 02:28:24 +03:00
vim.opt.completeopt = [ "menu" "menuone" "noselect" ];
2024-05-09 18:41:09 +03:00
fn.nvim-cmp-snippet-expand = {
args = [ "args" ];
content = { args }:
2024-05-10 15:56:17 +03:00
let body = pipe1 args "body"; in {
2024-05-09 18:41:09 +03:00
extra =
if snippet.luasnip.enable then
pipe1 config.plugin.luasnip.var (call "lsp_expand" body)
2024-05-09 18:41:09 +03:00
else if snippet.vsnip.enable then
call "vim.fn[\"${config.plugin.vim-vsnip.name}#anonymous\"]" body
else if snippet.snippy.enable then
pipe1 config.plugin.nvim-snippy.var (call "expand_snipped" body)
2024-05-09 18:41:09 +03:00
else if snippet.ultisnips.enable then
call "vim.fn[\"${config.plugin.ultisnips.name}#Anon\"]" body
else
call "vim.snippet.expand" body; # For native neovim snippets (Neovim v0.10+);
};
};
plugin.nvim-cmp = rec {
2024-05-09 02:28:24 +03:00
name = "cmp";
2024-05-09 18:41:09 +03:00
varName = name;
2024-05-09 02:28:24 +03:00
package = cfg.package;
setupSettings = lib.mkMerge [
2024-05-09 18:41:09 +03:00
{
mapping = call0 "${varName}.mapping.preset.insert";
sources =
lib.optional language-server.lspconfig.enable { name = "nvim_lsp"; group_index = 1; }
++ lib.optional snippet.luasnip.enable { name = "luasnip"; group_index = 5; }
++ lib.optional snippet.snippy.enable { name = "snippy"; group_index = 5; }
++ lib.optional snippet.vsnip.enable { name = "vsnip"; group_index = 5; }
++ lib.optional snippet.ultisnips.enable { name = "ultisnips"; group_index = 5; }
++ [{ name = "buffer"; group_index = 10; max_item_count = 10; }];
}
2024-05-09 02:28:24 +03:00
cfg.settings
{ snippet.expand = config.fn.nvim-cmp-snippet-expand.lambda; }
];
};
}
]);
}