nixeovim/modules/plugins/nvim-cmp.nix
Dmitriy Pleshevskiy f3926f6365
modules/plugin: remove isDependency
do not import to local variable by default. Instead you should
set varName option.
2024-05-10 02:17:00 +03:00

104 lines
3.2 KiB
Nix

{ config, lib, pkgs, ... }:
let
inherit (lib.nix2lua) pipe1 var call call0;
cfg = config.plugins.nvim-cmp;
inherit (config.plugins) snippet language-server;
in
{
options.plugins.nvim-cmp = with lib; {
enable = mkEnableOption "nvim-cmp";
package = mkPackageOption pkgs.vimPlugins "nvim-cmp" { };
settings = mkOption {
type = types.attrs;
default = { };
};
};
config = lib.mkIf cfg.enable (lib.mkMerge [
{
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";
}
];
}
# Backend dependencies
(lib.mkIf snippet.luasnip.enable {
plugin.cmp_luasnip = { };
})
(lib.mkIf snippet.snippy.enable {
plugin.cmp-snippy = { };
})
(lib.mkIf snippet.vsnip.enable {
plugin.cmp-vsnip = { };
})
(lib.mkIf snippet.ultisnips.enable {
plugin.cmp-nvim-ultisnips = { };
})
# Lsp configuration
(lib.mkIf language-server.lspconfig.enable {
plugin.cmp-nvim-lsp = {
name = "cmp_nvim_lsp";
};
plugins.language-server.lspconfig.defaultServerSettings.capabilities =
pipe1 config.plugin.cmp-nvim-lsp.var (call0 "default_capabilities");
})
# Base configuration
{
vim.opt.completeopt = [ "menu" "menuone" "noselect" ];
fn.nvim-cmp-snippet-expand = {
args = [ "args" ];
content = { args }:
let body = pipe1 args (var "body"); in {
extra =
if snippet.luasnip.enable then
pipe1 config.plugin.luasnip.var (call "lsp_expand" body)
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)
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 {
name = "cmp";
varName = name;
package = cfg.package;
setupSettings = lib.mkMerge [
{
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; }];
}
cfg.settings
{ snippet.expand = config.fn.nvim-cmp-snippet-expand.lambda; }
];
};
}
]);
}