modules: add nvim-cmp

This commit is contained in:
Dmitriy Pleshevskiy 2024-05-09 02:28:24 +03:00
parent 538f114300
commit 4b85dbd927
Signed by: pleshevskiy
GPG key ID: 17041163DA10A9A2
12 changed files with 115 additions and 28 deletions

View file

@ -22,6 +22,7 @@
./modules/plugins/style/nvim-treesitter.nix ./modules/plugins/style/nvim-treesitter.nix
./modules/plugins/theme/catppuccin.nix ./modules/plugins/theme/catppuccin.nix
./modules/plugins/gitsigns.nix ./modules/plugins/gitsigns.nix
./modules/plugins/nvim-cmp.nix
################################################## ##################################################
(pkgs.path + "/nixos/modules/misc/assertions.nix") (pkgs.path + "/nixos/modules/misc/assertions.nix")

View file

@ -34,22 +34,20 @@ in
}; };
config.build.neovim = { config.build.neovim = {
luaConfig = with lib.nix2lua; toLua (spaceBetween (lib.flatten [ luaConfig = with lib; with nix2lua; toLua (spaceBetween (flatten [
# Global Opts # Global Opts
(lib.flip lib.mapAttrsToList config.vim.g (k: set "vim.g.${k}")) (flip mapAttrsToList config.vim.g (k: set "vim.g.${k}"))
# Opts # Opts
(lib.flip lib.mapAttrsToList config.vim.opt (k: set "vim.opt.${k}")) (flip mapAttrsToList config.vim.opt (k: set "vim.opt.${k}"))
# Plugins # Plugins
(map (v: v.genConfig) (filter (v: !v.isDependency) (attrValues config.plugin))) (map (v: v.genConfig) (filter (v: !v.isDependency) (attrValues config.plugin)))
# Cmd # Cmd
(lib.optional (config.vim.cmd != "") (call "vim.cmd" config.vim.cmd)) (optional (config.vim.cmd != "") (call "vim.cmd" config.vim.cmd))
# Autocommands # Autocommands
(lib.flip lib.mapAttrsToList config.vim.augroup (k: v: v.genConfig)) (flip mapAttrsToList config.vim.augroup (k: v: v.genConfig))
# Keymaps # Keymaps
(lib.flip map config.vim.keymap.set ({ mode, lhs, rhs, ... } @ vars: (flip map config.vim.keymap.set ({ mode, lhs, rhs, ... } @ vars:
let let m = if builtins.isList mode then head mode else mode; in
m = if builtins.isList mode then lib.head mode else mode;
in
if config.vim.keymap._validate."${m}"."${lhs}" == rhs then vars.genConfig if config.vim.keymap._validate."${m}"."${lhs}" == rhs then vars.genConfig
else abort "This case should never happen." else abort "This case should never happen."
)) ))

View file

@ -1,6 +1,8 @@
{ lib, ... }: { lib, ... }:
let let
inherit (lib.nix2lua) lambda raw;
fnOpts = { name, config, ... }: { fnOpts = { name, config, ... }: {
options = with lib; { options = with lib; {
args = mkOption { args = mkOption {
@ -32,7 +34,7 @@ let
}; };
config = { config = {
lambda = with lib.nix2lua; lambda =
let let
fnArgs = builtins.listToAttrs (map (v: { name = v; value = raw v; }) config.args); fnArgs = builtins.listToAttrs (map (v: { name = v; value = raw v; }) config.args);
innerCfg = config.content fnArgs; innerCfg = config.content fnArgs;

View file

@ -61,11 +61,11 @@ let
name = lib.mkDefault name; name = lib.mkDefault name;
varName = lib.mkDefault (builtins.replaceStrings [ "-" "/" "." ] [ "_" "_" "_" ] config.name); varName = lib.mkDefault (builtins.replaceStrings [ "-" "/" "." ] [ "_" "_" "_" ] config.name);
genConfig = with lib.nix2lua; lib.mkIf (!config.isDependency) (lib.flatten [ genConfig = with lib; with nix2lua; mkIf (!config.isDependency) (flatten [
(local (set config.varName (require config.name))) (local (set config.varName (require config.name)))
(lib.mapAttrsToList (k: v: local (set k (require v))) config.extraImports) (mapAttrsToList (k: v: local (set k (require v))) config.extraImports)
config.beforeSetup config.beforeSetup
(lib.optional (config.setupSettings != null) (optional (config.setupSettings != null)
(pipe1 (var config.varName) (call config.setupFnName config.setupSettings)) (pipe1 (var config.varName) (call config.setupFnName config.setupSettings))
) )
config.afterSetup config.afterSetup
@ -78,9 +78,8 @@ let
in in
{ {
options.plugin = with lib; with types; mkOption { options.plugin = with lib; mkOption {
type = attrsOf (submodule pluginOpts); type = with types; attrsOf (submodule pluginOpts);
default = { }; default = { };
}; };
} }

View file

@ -1,6 +1,7 @@
{ config, lib, pkgs, ... }: { config, lib, pkgs, ... }:
let let
inherit (lib.nix2lua) pipe pipe1 var set call;
cfg = config.plugins.language-server.lspconfig; cfg = config.plugins.language-server.lspconfig;
in in
{ {
@ -12,6 +13,16 @@ in
serverSettings = mkOption { serverSettings = mkOption {
type = with types; attrsOf attrs; type = with types; attrsOf attrs;
default = { }; default = { };
description = ''
Server-specific settings.
`:help lspconfig-setup`
'';
};
defaultServerSettings = mkOption {
type = with types; nullOr attrs;
default = null;
}; };
keymap.set = mkOption { keymap.set = mkOption {
@ -23,7 +34,7 @@ in
config = lib.mkIf cfg.enable { config = lib.mkIf cfg.enable {
fn.lspconfig-on-attach = { fn.lspconfig-on-attach = {
args = [ "event" ]; args = [ "event" ];
content = with lib.nix2lua; { event }: { content = { event }: {
vim.keymap.set = map vim.keymap.set = map
(attrs: attrs // { buffer = pipe1 event (var "buf"); }) (attrs: attrs // { buffer = pipe1 event (var "buf"); })
cfg.keymap.set; cfg.keymap.set;
@ -35,13 +46,24 @@ in
callback = config.fn.lspconfig-on-attach.lambda; callback = config.fn.lspconfig-on-attach.lambda;
}; };
plugin.nvim-lspconfig = { plugin.nvim-lspconfig = rec {
name = "lspconfig"; name = "lspconfig";
varName = name;
package = cfg.package; package = cfg.package;
afterSetup = with lib.nix2lua; lib.mapAttrsToList beforeSetup =
let varDefaultConfig = var "${varName}.util.default_config"; in
lib.optional (cfg.defaultServerSettings != null) (set
varDefaultConfig
(call "vim.tbl_extend" [
"force"
varDefaultConfig
cfg.defaultServerSettings
])
);
afterSetup = lib.mapAttrsToList
(ls: lsSettings: (ls: lsSettings:
pipe [ pipe [
(var "lspconfig") (var varName)
(var ls) (var ls)
(call "setup" lsSettings) (call "setup" lsSettings)
] ]

View file

@ -1,6 +1,7 @@
{ config, lib, pkgs, ... }: { config, lib, pkgs, ... }:
let let
inherit (lib.nix2lua) var;
cfg = config.plugins.navigation.hop-nvim; cfg = config.plugins.navigation.hop-nvim;
in in
{ {
@ -48,7 +49,7 @@ in
setupSettings = cfg.settings; setupSettings = cfg.settings;
}; };
vim.keymap.set = with lib.nix2lua; cfg.keymap.set { vim.keymap.set = cfg.keymap.set {
after = var "hop_hint.HintDirection.AFTER_CURSOR"; after = var "hop_hint.HintDirection.AFTER_CURSOR";
before = var "hop_hint.HintDirection.BEFORE_CURSOR"; before = var "hop_hint.HintDirection.BEFORE_CURSOR";
}; };

View file

@ -1,7 +1,9 @@
{ config, lib, pkgs, ... }: { config, lib, pkgs, ... }:
let cfg = config.plugins.navigation.nvim-tree; in let
inherit (lib.nix2lua) require pipe var call;
cfg = config.plugins.navigation.nvim-tree;
in
{ {
options.plugins.navigation.nvim-tree = with lib; { options.plugins.navigation.nvim-tree = with lib; {
enable = mkEnableOption "nvim-tree"; enable = mkEnableOption "nvim-tree";
@ -40,7 +42,7 @@ let cfg = config.plugins.navigation.nvim-tree; in
fn.nvim-tree-lua-on-attach = lib.mkIf (cfg.keymap.set != [ ]) { fn.nvim-tree-lua-on-attach = lib.mkIf (cfg.keymap.set != [ ]) {
args = [ "bufnr" ]; args = [ "bufnr" ];
content = { bufnr }: { content = { bufnr }: {
extra = with lib.nix2lua; lib.mkIf cfg.keymap.withDefault pipe [ extra = lib.mkIf cfg.keymap.withDefault pipe [
(require "nvim-tree.api") (require "nvim-tree.api")
(var "config") (var "config")
(var "mappings") (var "mappings")

View file

@ -2,6 +2,7 @@
let let
inherit (builtins) match isFunction attrValues; inherit (builtins) match isFunction attrValues;
inherit (lib.nix2lua) pipe1 var call;
cfg = config.plugins.navigation.telescope; cfg = config.plugins.navigation.telescope;
@ -38,7 +39,7 @@ let
config = { config = {
name = lib.mkDefault name; name = lib.mkDefault name;
loadExtension = with lib.nix2lua; lib.mkDefault (pipe1 loadExtension = lib.mkDefault (pipe1
(var config.plugin.telescope-nvim.varName) (var config.plugin.telescope-nvim.varName)
(call "load_extension" sub.config.name) (call "load_extension" sub.config.name)
); );

View file

@ -0,0 +1,57 @@
{ config, lib, pkgs, ... }:
let
inherit (lib.nix2lua) pipe1 var call call0;
cfg = config.plugins.nvim-cmp;
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 [
{
plugin.luasnip = { };
plugin.cmp_luasnip = lib.mkDefault { isDependency = true; };
fn.nvim-cmp-snippet-expand = {
args = [ "args" ];
content = args: {
extra = pipe1
(var config.plugin.luasnip.varName)
(call "lsp_expand" args);
};
};
vim.opt.completeopt = [ "menu" "menuone" "noselect" ];
plugin.nvim-cmp = {
name = "cmp";
package = cfg.package;
setupSettings = lib.mkMerge [
cfg.settings
{ snippet.expand = config.fn.nvim-cmp-snippet-expand.lambda; }
];
};
}
(lib.mkIf config.plugins.language-server.lspconfig.enable {
plugin.cmp-nvim-lsp = {
name = "cmp_nvim_lsp";
};
plugins.language-server.lspconfig.defaultServerSettings.capabilities =
pipe1
(var config.plugin.cmp-nvim-lsp.varName)
(call0 "default_capabilities");
})
]);
}

View file

@ -83,12 +83,12 @@ in
name = "nvim-treesitter.configs"; name = "nvim-treesitter.configs";
package = finalNvimTreeSitter; package = finalNvimTreeSitter;
beforeSetup = with lib.nix2lua; lib.optionals (cfg.extraGrammars != { }) (lib.flatten [ beforeSetup = with lib; with nix2lua; optionals (cfg.extraGrammars != { }) (flatten [
(local (set "parser_config") (local (set "parser_config")
(pipe1 (require "nvim-treesitter.parsers") (call0 "get_parser_configs")) (pipe1 (require "nvim-treesitter.parsers") (call0 "get_parser_configs"))
) )
(lib.mapAttrsToList (k: v: set "parser_config.${v.language}" { }) cfg.extraGrammars) (mapAttrsToList (k: v: set "parser_config.${v.language}" { }) cfg.extraGrammars)
]); ]);
setupSettings = lib.mkMerge [ setupSettings = lib.mkMerge [

View file

@ -66,5 +66,7 @@ let inherit (lib.nix2lua) nf; in
}; };
gitsigns.enable = lib.mkDefault true; gitsigns.enable = lib.mkDefault true;
nvim-cmp.enable = lib.mkDefault true;
}; };
} }

View file

@ -2,6 +2,8 @@
let let
inherit (lib.nix2lua) call;
augroupsCfg = config.vim.augroup; augroupsCfg = config.vim.augroup;
autocmd = { name, config, ... }: { autocmd = { name, config, ... }: {
@ -86,7 +88,7 @@ let
config = { config = {
group = lib.mkDefault name; group = lib.mkDefault name;
genConfig = with lib.nix2lua; call "vim.api.nvim_create_autocmd" [ genConfig = call "vim.api.nvim_create_autocmd" [
config.event config.event
{ {
inherit (config) pattern buffer desc callback command once nested; inherit (config) pattern buffer desc callback command once nested;