From a12ba5689a904eb6571344b3b501a134b0f0f7f1 Mon Sep 17 00:00:00 2001 From: NotAShelf Date: Sun, 7 Apr 2024 18:31:06 +0300 Subject: [PATCH] lib: move all binding related functions to lib/binds --- lib/binds.nix | 1 + lib/stdlib-extended.nix | 74 ++----------------- modules/core/build/config.nix | 13 ++-- modules/default.nix | 3 +- modules/neovim/mappings/default.nix | 56 +++++++------- modules/plugins/filetree/nvimtree/config.nix | 3 +- modules/plugins/git/config.nix | 4 +- .../plugins/notes/todo-comments/config.nix | 3 +- .../notes/todo-comments/todo-comments.nix | 3 +- modules/plugins/utility/telescope/config.nix | 3 +- 10 files changed, 51 insertions(+), 112 deletions(-) diff --git a/lib/binds.nix b/lib/binds.nix index 34d7a7c..57b700e 100644 --- a/lib/binds.nix +++ b/lib/binds.nix @@ -67,6 +67,7 @@ mkLuaBinding binding.value action binding.description; pushDownDefault = attr: mapAttrs (_name: value: mkDefault value) attr; + # pushDownDefault = attr: self.mapAttrs (name: value: self.mkDefault value) attr; }; in binds diff --git a/lib/stdlib-extended.nix b/lib/stdlib-extended.nix index ff7b7f6..159ef7e 100644 --- a/lib/stdlib-extended.nix +++ b/lib/stdlib-extended.nix @@ -1,75 +1,15 @@ -# From home-manager: https://github.com/nix-community/home-manager/blob/master/modules/lib/stdlib-extended.nix -# Just a convenience function that returns the given Nixpkgs standard -# library extended with the HM library. +# Cconvenience function that returns the given Nixpkgs standard library +# extended with our functions using `lib.extend`. nixpkgsLib: let mkNvimLib = import ./.; in - nixpkgsLib.extend (self: super: rec { + # WARNING: New functions should not be added here, but to files + # imported by `./default.nix` under their own categories. If your + # function does not fit to any of the existing categories, create + # a new file and import it in `./default.nix.` + nixpkgsLib.extend (self: super: { nvim = mkNvimLib {lib = self;}; - mkLuaBinding = key: action: desc: - self.mkIf (key != null) { - "${key}" = { - inherit action desc; - lua = true; - silent = true; - }; - }; - - mkExprBinding = key: action: desc: - self.mkIf (key != null) { - "${key}" = { - inherit action desc; - lua = true; - silent = true; - expr = true; - }; - }; - - mkBinding = key: action: desc: - self.mkIf (key != null) { - "${key}" = { - inherit action desc; - silent = true; - }; - }; - - mkMappingOption = description: default: - self.mkOption { - type = self.types.nullOr self.types.str; - inherit default description; - }; - - # Utility function that takes two attrsets: - # { someKey = "some_value" } and - # { someKey = { description = "Some Description"; }; } - # and merges them into - # { someKey = { value = "some_value"; description = "Some Description"; }; } - addDescriptionsToMappings = actualMappings: mappingDefinitions: - self.attrsets.mapAttrs (name: value: let - isNested = self.isAttrs value; - returnedValue = - if isNested - then addDescriptionsToMappings actualMappings."${name}" mappingDefinitions."${name}" - else { - value = value; - description = mappingDefinitions."${name}".description; - }; - in - returnedValue) - actualMappings; - - mkSetBinding = binding: action: - mkBinding binding.value action binding.description; - - mkSetExprBinding = binding: action: - mkExprBinding binding.value action binding.description; - - mkSetLuaBinding = binding: action: - mkLuaBinding binding.value action binding.description; - - pushDownDefault = attr: self.mapAttrs (name: value: self.mkDefault value) attr; - # For forward compatibility. literalExpression = super.literalExpression or super.literalExample; }) diff --git a/modules/core/build/config.nix b/modules/core/build/config.nix index 068319e..0a68a81 100644 --- a/modules/core/build/config.nix +++ b/modules/core/build/config.nix @@ -236,6 +236,11 @@ in { else abort ("Dependency cycle in ${name}: " + toJSON sortedDag); in result; + + mkSection = r: '' + -- SECTION: ${r.name} + ${r.data} + ''; in { vim = { startPlugins = map (x: x.package) (attrValues cfg.extraPlugins); @@ -243,10 +248,6 @@ in { globalsScript = entryAnywhere (concatStringsSep "\n" globalsScript); luaScript = let - mkSection = r: '' - -- SECTION: ${r.name} - ${r.data} - ''; mapResult = r: (wrapLuaConfig (concatStringsSep "\n" (map mkSection r))); luaConfig = resolveDag { name = "lua config script"; @@ -257,10 +258,6 @@ in { entryAfter ["globalsScript"] luaConfig; extraPluginConfigs = let - mkSection = r: '' - -- SECTION: ${r.name} - ${r.data} - ''; mapResult = r: (wrapLuaConfig (concatStringsSep "\n" (map mkSection r))); extraPluginsDag = mapAttrs (_: { after, diff --git a/modules/default.nix b/modules/default.nix index 8ecdaef..38afb88 100644 --- a/modules/default.nix +++ b/modules/default.nix @@ -9,6 +9,7 @@ inputs: { inherit (pkgs) wrapNeovimUnstable vimPlugins; inherit (pkgs.vimUtils) buildVimPlugin; inherit (pkgs.neovimUtils) makeNeovimConfig; + inherit (lib.attrsets) recursiveUpdate; extendedLib = import ../lib/stdlib-extended.nix lib; @@ -19,7 +20,7 @@ inputs: { module = extendedLib.evalModules { modules = [configuration] ++ nvimModules; - specialArgs = {modulesPath = toString ./.;} // extraSpecialArgs; + specialArgs = recursiveUpdate {modulesPath = toString ./.;} extraSpecialArgs; }; vimOptions = module.config.vim; diff --git a/modules/neovim/mappings/default.nix b/modules/neovim/mappings/default.nix index fd4db2d..77438a7 100644 --- a/modules/neovim/mappings/default.nix +++ b/modules/neovim/mappings/default.nix @@ -7,16 +7,41 @@ cfg = config.vim; in { - vim.maps.normal = - mkIf cfg.disableArrows { + vim.maps = { + normal = + mkIf cfg.disableArrows { + "" = { + action = ""; + + noremap = false; + }; + "" = { + action = ""; + + noremap = false; + }; + "" = { + action = ""; + noremap = false; + }; + "" = { + action = ""; + noremap = false; + }; + } + // mkIf cfg.mapLeaderSpace { + "" = { + action = ""; + }; + }; + + insert = mkIf cfg.disableArrows { "" = { action = ""; - noremap = false; }; "" = { action = ""; - noremap = false; }; "" = { @@ -27,29 +52,6 @@ in { action = ""; noremap = false; }; - } - // mkIf cfg.mapLeaderSpace { - "" = { - action = ""; - }; - }; - - vim.maps.insert = mkIf cfg.disableArrows { - "" = { - action = ""; - noremap = false; - }; - "" = { - action = ""; - noremap = false; - }; - "" = { - action = ""; - noremap = false; - }; - "" = { - action = ""; - noremap = false; }; }; } diff --git a/modules/plugins/filetree/nvimtree/config.nix b/modules/plugins/filetree/nvimtree/config.nix index c560650..08e3d24 100644 --- a/modules/plugins/filetree/nvimtree/config.nix +++ b/modules/plugins/filetree/nvimtree/config.nix @@ -9,8 +9,7 @@ inherit (lib.nvim.binds) mkBinding; inherit (lib.nvim.dag) entryAnywhere; inherit (lib.nvim.lua) toLuaObject; - # TODO: move this to its own module - inherit (lib) pushDownDefault; + inherit (lib.nvim.binds) pushDownDefault; cfg = config.vim.filetree.nvimTree; self = import ./nvimtree.nix {inherit pkgs lib;}; diff --git a/modules/plugins/git/config.nix b/modules/plugins/git/config.nix index 8e34963..8fc574a 100644 --- a/modules/plugins/git/config.nix +++ b/modules/plugins/git/config.nix @@ -5,10 +5,8 @@ }: let inherit (builtins) toJSON; inherit (lib.modules) mkIf mkMerge; - inherit (lib.nvim.binds) addDescriptionsToMappings mkSetExprBinding mkSetLuaBinding; + inherit (lib.nvim.binds) addDescriptionsToMappings mkSetExprBinding mkSetLuaBinding pushDownDefault; inherit (lib.nvim.dag) entryAnywhere; - # TODO: move this to its own module - inherit (lib) pushDownDefault; cfg = config.vim.git; diff --git a/modules/plugins/notes/todo-comments/config.nix b/modules/plugins/notes/todo-comments/config.nix index 1fa5dde..730a14d 100644 --- a/modules/plugins/notes/todo-comments/config.nix +++ b/modules/plugins/notes/todo-comments/config.nix @@ -4,7 +4,8 @@ lib, ... }: let - inherit (lib) mkMerge mkBinding mkIf; + inherit (lib) mkMerge mkIf; + inherit (lib.nvim.binds) mkBinding; inherit (lib.nvim.lua) toLuaObject; cfg = config.vim.notes.todo-comments; diff --git a/modules/plugins/notes/todo-comments/todo-comments.nix b/modules/plugins/notes/todo-comments/todo-comments.nix index f92af7a..7f7caed 100644 --- a/modules/plugins/notes/todo-comments/todo-comments.nix +++ b/modules/plugins/notes/todo-comments/todo-comments.nix @@ -3,7 +3,8 @@ lib, ... }: let - inherit (lib) mkEnableOption mkOption types mkMappingOption mkRenamedOptionModule; + inherit (lib) mkEnableOption mkOption types mkRenamedOptionModule; + inherit (lib.nvim.binds) mkMappingOption; in { imports = let renamedSetupOption = oldPath: newPath: diff --git a/modules/plugins/utility/telescope/config.nix b/modules/plugins/utility/telescope/config.nix index f178158..73bf759 100644 --- a/modules/plugins/utility/telescope/config.nix +++ b/modules/plugins/utility/telescope/config.nix @@ -7,8 +7,7 @@ inherit (lib.modules) mkIf mkMerge; inherit (lib.nvim.binds) addDescriptionsToMappings mkSetBinding; inherit (lib.nvim.dag) entryAnywhere; - # TODO: move this to its own module - inherit (lib) pushDownDefault; + inherit (lib.nvim.binds) pushDownDefault; inherit (lib.nvim.lua) toLuaObject; cfg = config.vim.telescope;