From a2a49ca72227cac4c092db1bfd169556170c178a Mon Sep 17 00:00:00 2001 From: Dmitriy Pleshevskiy Date: Fri, 3 May 2024 17:50:57 +0300 Subject: [PATCH] modules: add augroups, lspconfig --- module-list.nix | 8 +- modules/build/neovim.nix | 2 + modules/plugins/language-server/lspconfig.nix | 51 +++++++ modules/vim/augroup.nix | 128 ++++++++++++++++++ 4 files changed, 186 insertions(+), 3 deletions(-) create mode 100644 modules/plugins/language-server/lspconfig.nix create mode 100644 modules/vim/augroup.nix diff --git a/module-list.nix b/module-list.nix index 99521b5..04bdf46 100644 --- a/module-list.nix +++ b/module-list.nix @@ -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 ################################################## diff --git a/modules/build/neovim.nix b/modules/build/neovim.nix index e67b17e..af58769 100644 --- a/modules/build/neovim.nix +++ b/modules/build/neovim.nix @@ -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 diff --git a/modules/plugins/language-server/lspconfig.nix b/modules/plugins/language-server/lspconfig.nix new file mode 100644 index 0000000..3577504 --- /dev/null +++ b/modules/plugins/language-server/lspconfig.nix @@ -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; + }; + }; +} diff --git a/modules/vim/augroup.nix b/modules/vim/augroup.nix new file mode 100644 index 0000000..8e749cb --- /dev/null +++ b/modules/vim/augroup.nix @@ -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 + ); + }; +}