modules: add augroups, lspconfig

This commit is contained in:
Dmitriy Pleshevskiy 2024-05-03 17:50:57 +03:00
parent 8628fb2ed8
commit a2a49ca722
Signed by: pleshevskiy
GPG key ID: 17041163DA10A9A2
4 changed files with 186 additions and 3 deletions

View file

@ -5,18 +5,20 @@
./modules/vim/opts.nix
./modules/vim/keymap.nix
./modules/vim/augroup.nix
./modules/filetype.nix
./modules/input.nix
./modules/plugin.nix
./modules/fn.nix
./modules/plugins/theme/catppuccin.nix
./modules/plugins/style/neoformat.nix
./modules/plugins/style/nvim-treesitter.nix
./modules/plugins/language-server/lspconfig.nix
./modules/plugins/navigation/hop-nvim.nix
./modules/plugins/navigation/telescope.nix
./modules/plugins/navigation/nvim-tree.nix
./modules/plugins/style/neoformat.nix
./modules/plugins/style/nvim-treesitter.nix
./modules/plugins/theme/catppuccin.nix
./modules/plugins/gitsigns.nix
##################################################

View file

@ -40,6 +40,8 @@ in
(map (v: v.genConfig) (filter (v: !v.isDependency) (attrValues config.plugin)))
# Cmd
(lib.optional (config.vim.cmd != "") (call "vim.cmd" config.vim.cmd))
# Autocommands
(lib.flip lib.mapAttrsToList config.vim.augroup (k: v: v.genConfig))
# Keymaps
(lib.flip map config.vim.keymap.set ({ mode, lhs, rhs, ... } @ vars:
let

View file

@ -0,0 +1,51 @@
{ config, lib, pkgs, ... }:
let
cfg = config.plugins.language-server.lspconfig;
in
{
options.plugins.language-server.lspconfig = with lib; {
enable = mkEnableOption "lspconfig";
package = mkPackageOption pkgs.vimPlugins "nvim-lspconfig" { };
serverSettings = mkOption {
type = with types; attrsOf attrs;
default = { };
};
keymap.set = mkOption {
type = with types; listOf keymap;
default = [ ];
};
};
config = lib.mkIf cfg.enable {
fn.lspconfig-on-attach = {
args = [ "event" ];
content = with lib.nix2lua; { event }: {
vim.keymap.set = map
(attrs: attrs // { buffer = pipe1 event (var "buf"); })
cfg.keymap.set;
};
};
vim.augroup.user-lsp-config = {
event = "LspAttach";
callback = config.fn.lspconfig-on-attach.lambda;
};
plugin.nvim-lspconfig = {
name = "lspconfig";
afterSetup = with lib.nix2lua; lib.mapAttrsToList
(ls: lsSettings:
pipe [
(var "lspconfig")
(var ls)
(call "setup" lsSettings)
]
)
cfg.serverSettings;
};
};
}

128
modules/vim/augroup.nix Normal file
View file

@ -0,0 +1,128 @@
{ config, lib, ... }:
let
augroupsCfg = config.vim.augroup;
autocmd = { name, config, ... }: {
options = with lib; {
group = mkOption {
type = types.str;
description = ''
autocommand group name.
'';
};
event = mkOption {
type = with types; either str (listOf str);
description = ''
Event(s) that will trigger the handler (`callback` or `command`).
'';
};
pattern = mkOption {
type = with types; nullOr (either str (listOf str));
default = null;
description = ''
pattern(s) to match literally.
`:help autocmd-pattern
'';
};
buffer = mkOption {
type = with types; nullOr attrs;
default = null;
description = ''
buffer number for buffer-local autocommands.
Cannot be used with `pattern`.
`:help autocmd-buflocal`
'';
};
desc = mkOption {
type = with types; nullOr str;
default = null;
description = ''
description (for documentation and troubleshooting).
'';
};
callback = mkOption {
type = with types; nullOr attrs;
default = null;
description = ''
Lua function (or Vimscript function name) called when the event(s) is triggered.
'';
};
command = mkOption {
type = with types; nullOr str;
default = null;
description = ''
Vim command to execute on event. Cannot be used with `callback`.
'';
};
once = mkOption {
type = types.bool;
default = false;
description = ''
Run the autocommand only once.
`:help autocmd-once`
'';
};
nested = mkOption {
type = types.bool;
default = false;
description = ''
Run nested autocommands.
`:help autocmd-nested`
'';
};
genConfig = mkOption {
type = types.attrs;
internal = true;
};
};
config = {
group = lib.mkDefault name;
genConfig = with lib.nix2lua; call "vim.api.nvim_create_autocmd" [
config.event
{
inherit (config) pattern buffer desc callback command once nested;
group = call "vim.api.nvim_create_augroup" config.group;
}
];
};
};
in
{
options.vim.augroup = with lib; mkOption {
type = with types; attrsOf (submodule autocmd);
default = { };
};
config = lib.mkIf (augroupsCfg != { }) {
assertions = lib.flatten (lib.mapAttrsToList
(k: { pattern, buffer, command, callback, ... }: [
{
assertion = (buffer != null -> pattern == null)
|| (pattern != null -> buffer == null);
message = "vim.augroup.${k}: `buffer` cannot be used with `pattern`";
}
{
assertion = callback != null || command != null;
message = "vim.augroup.${k}: either `command` or `callback` is required";
}
{
assertion = (callback != null -> command == null)
|| (command != null -> callback == null);
message = "vim.augroup.${k}: `command` cannot be used with `callback`";
}
])
augroupsCfg
);
};
}