From 932959dcff27d26ad4f7f3db5de5e2b6935ed2a7 Mon Sep 17 00:00:00 2001 From: Matt Sturgeon Date: Sat, 28 Sep 2024 07:57:58 +0100 Subject: [PATCH 1/3] lib: standardize use of `lib` and `self` - Drop `helpers` in internal useage - Access nixvim's lib via `self` - Access the (un-extended) lib via `lib` - Build nixvim's types internally, before later merging into `lib.types` While I like the idea of having `lib.nixvim` access internally within our lib, keeping our lib (`self`) separate from the extended lib will be better in the long-term. --- lib/autocmd-helpers.nix | 25 ++++++++++++----------- lib/builders.nix | 6 +++--- lib/default.nix | 12 +++++++---- lib/deprecation.nix | 7 +++++-- lib/extend-lib.nix | 5 ++--- lib/keymap-helpers.nix | 27 ++++++++++++++----------- lib/modules.nix | 2 +- lib/neovim-plugin.nix | 13 +++++++----- lib/options.nix | 44 ++++++++++++++++++++--------------------- lib/types.nix | 9 ++++++--- lib/utils.nix | 1 - lib/vim-plugin.nix | 9 ++++++--- 12 files changed, 90 insertions(+), 70 deletions(-) diff --git a/lib/autocmd-helpers.nix b/lib/autocmd-helpers.nix index 4d6e7ae87..b99c71b02 100644 --- a/lib/autocmd-helpers.nix +++ b/lib/autocmd-helpers.nix @@ -1,4 +1,7 @@ -{ lib, helpers }: +{ + lib, + self, +}: let inherit (lib) types; in @@ -14,34 +17,34 @@ rec { }; autoCmdOptions = { - event = helpers.mkNullOrOption (with types; either str (listOf str)) '' + event = self.mkNullOrOption (with types; either str (listOf str)) '' The event or events to register this autocommand. ''; - group = helpers.mkNullOrOption (with types; either str int) '' + group = self.mkNullOrOption (with types; either str int) '' The autocommand group name or id to match against. ''; - pattern = helpers.mkNullOrOption (with types; either str (listOf str)) '' + pattern = self.mkNullOrOption (with types; either str (listOf str)) '' Pattern or patterns to match literally against. ''; - buffer = helpers.mkNullOrOption types.int '' + buffer = self.mkNullOrOption types.int '' Buffer number for buffer local autocommands |autocmd-buflocal|. Cannot be used with `pattern`. ''; # Introduced early October 2023. # TODO remove in early December 2023. - description = helpers.mkNullOrOption types.str '' + description = self.mkNullOrOption types.str '' DEPRECATED, please use `desc`. ''; - desc = helpers.mkNullOrOption types.str '' + desc = self.mkNullOrOption types.str '' A textual description of this autocommand. ''; - callback = helpers.mkNullOrOption (with types; either str rawLua) '' + callback = self.mkNullOrOption (with types; either str self.types.rawLua) '' A function or a string. - if a string, the name of a Vimscript function to call when this autocommand is triggered. - Otherwise, a Lua function which is called when this autocommand is triggered. @@ -65,13 +68,13 @@ rec { } ''; - command = helpers.defaultNullOpts.mkStr "" '' + command = self.defaultNullOpts.mkStr "" '' Vim command to execute on event. Cannot be used with `callback`. ''; - once = helpers.defaultNullOpts.mkBool false "Run the autocommand only once."; + once = self.defaultNullOpts.mkBool false "Run the autocommand only once."; - nested = helpers.defaultNullOpts.mkBool false "Run nested autocommands."; + nested = self.defaultNullOpts.mkBool false "Run nested autocommands."; }; autoCmdOption = types.submodule { options = autoCmdOptions; }; diff --git a/lib/builders.nix b/lib/builders.nix index 5eaceefbc..331871035 100644 --- a/lib/builders.nix +++ b/lib/builders.nix @@ -1,6 +1,6 @@ { lib }: -# NOTE: use local recursion instead of accessing `lib.nixvim.builders`. -# The latter isn't a fixed shape since it may get the deprecated functions meregd in, +# NOTE: use local recursion instead of accessing `self.builders`. +# The latter isn't a fixed shape since it may get the deprecated functions merged in, # which would lead to infinite recursion. lib.fix (builders: { # Curry a nixpkgs instance into the *With functions below, dropping the `With` suffix @@ -147,7 +147,7 @@ lib.fix (builders: { prev: { nativeBuildInputs = prev.nativeBuildInputs or [ ] ++ [ - (lib.nixvim.builders.byteCompileLuaHookWith pkgs) + (builders.byteCompileLuaHookWith pkgs) ]; } // lib.optionalAttrs (prev ? buildCommand) { diff --git a/lib/default.nix b/lib/default.nix index abda5dc70..3590d439f 100644 --- a/lib/default.nix +++ b/lib/default.nix @@ -10,9 +10,12 @@ lib.fix ( let # Used when importing parts of helpers call = lib.callPackageWith { - inherit call pkgs self; - helpers = self; # TODO: stop using `helpers` in the subsections - lib = self.extendedLib; + inherit + call + lib + pkgs + self + ; }; # Define this outside of the attrs to avoid infinite recursion, @@ -35,12 +38,13 @@ lib.fix ( { autocmd = call ./autocmd-helpers.nix { }; deprecation = call ./deprecation.nix { }; - extendedLib = call ./extend-lib.nix { inherit lib; }; + extendedLib = call ./extend-lib.nix { }; # TODO: provide an overlay instead keymaps = call ./keymap-helpers.nix { }; lua = call ./to-lua.nix { }; modules = call ./modules.nix { }; neovim-plugin = call ./neovim-plugin.nix { }; options = call ./options.nix { }; + types = call ./types.nix { }; # NOTE: you should usually prefer the extended `lib.types` utils = call ./utils.nix { inherit _nixvimTests; }; vim-plugin = call ./vim-plugin.nix { }; diff --git a/lib/deprecation.nix b/lib/deprecation.nix index 94070a274..7cb9f4be7 100644 --- a/lib/deprecation.nix +++ b/lib/deprecation.nix @@ -1,4 +1,7 @@ -{ lib }: +{ + lib, + self, +}: rec { # Get a (sub)option by walking the path, # checking for submodules along the way @@ -54,7 +57,7 @@ rec { let option = lib.toList option'; oldPath = oldPrefix ++ option; - newPath = newPrefix ++ map lib.nixvim.toSnakeCase option; + newPath = newPrefix ++ map self.toSnakeCase option; in lib.mkRenamedOptionModule oldPath newPath ); diff --git a/lib/extend-lib.nix b/lib/extend-lib.nix index c7fab0638..4ba9d4f49 100644 --- a/lib/extend-lib.nix +++ b/lib/extend-lib.nix @@ -1,8 +1,7 @@ # Extends nixpkg's lib with our functions, as expected by our modules { - call, lib, - self, + self ? import ./. { inherit lib; }, }: lib.extend ( final: prev: { @@ -13,6 +12,6 @@ lib.extend ( maintainers = prev.maintainers // import ./maintainers.nix; # Merge in our custom types - types = prev.types // call ./types.nix { }; + types = prev.types // self.types; } ) diff --git a/lib/keymap-helpers.nix b/lib/keymap-helpers.nix index 132ca6762..0db925ead 100644 --- a/lib/keymap-helpers.nix +++ b/lib/keymap-helpers.nix @@ -1,27 +1,30 @@ -{ lib, helpers }: +{ + lib, + self, +}: let inherit (lib) optionalAttrs isAttrs types; in rec { # These are the configuration options that change the behavior of each mapping. mapConfigOptions = { - silent = helpers.defaultNullOpts.mkBool false "Whether this mapping should be silent. Equivalent to adding `` to a map."; + silent = self.defaultNullOpts.mkBool false "Whether this mapping should be silent. Equivalent to adding `` to a map."; - nowait = helpers.defaultNullOpts.mkBool false "Whether to wait for extra input on ambiguous mappings. Equivalent to adding `` to a map."; + nowait = self.defaultNullOpts.mkBool false "Whether to wait for extra input on ambiguous mappings. Equivalent to adding `` to a map."; - script = helpers.defaultNullOpts.mkBool false "Equivalent to adding `