Skip to content

Commit 932959d

Browse files
committed
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.
1 parent b5c19b6 commit 932959d

12 files changed

+90
-70
lines changed

lib/autocmd-helpers.nix

Lines changed: 14 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,7 @@
1-
{ lib, helpers }:
1+
{
2+
lib,
3+
self,
4+
}:
25
let
36
inherit (lib) types;
47
in
@@ -14,34 +17,34 @@ rec {
1417
};
1518

1619
autoCmdOptions = {
17-
event = helpers.mkNullOrOption (with types; either str (listOf str)) ''
20+
event = self.mkNullOrOption (with types; either str (listOf str)) ''
1821
The event or events to register this autocommand.
1922
'';
2023

21-
group = helpers.mkNullOrOption (with types; either str int) ''
24+
group = self.mkNullOrOption (with types; either str int) ''
2225
The autocommand group name or id to match against.
2326
'';
2427

25-
pattern = helpers.mkNullOrOption (with types; either str (listOf str)) ''
28+
pattern = self.mkNullOrOption (with types; either str (listOf str)) ''
2629
Pattern or patterns to match literally against.
2730
'';
2831

29-
buffer = helpers.mkNullOrOption types.int ''
32+
buffer = self.mkNullOrOption types.int ''
3033
Buffer number for buffer local autocommands |autocmd-buflocal|.
3134
Cannot be used with `pattern`.
3235
'';
3336

3437
# Introduced early October 2023.
3538
# TODO remove in early December 2023.
36-
description = helpers.mkNullOrOption types.str ''
39+
description = self.mkNullOrOption types.str ''
3740
DEPRECATED, please use `desc`.
3841
'';
3942

40-
desc = helpers.mkNullOrOption types.str ''
43+
desc = self.mkNullOrOption types.str ''
4144
A textual description of this autocommand.
4245
'';
4346

44-
callback = helpers.mkNullOrOption (with types; either str rawLua) ''
47+
callback = self.mkNullOrOption (with types; either str self.types.rawLua) ''
4548
A function or a string.
4649
- if a string, the name of a Vimscript function to call when this autocommand is triggered.
4750
- Otherwise, a Lua function which is called when this autocommand is triggered.
@@ -65,13 +68,13 @@ rec {
6568
}
6669
'';
6770

68-
command = helpers.defaultNullOpts.mkStr "" ''
71+
command = self.defaultNullOpts.mkStr "" ''
6972
Vim command to execute on event. Cannot be used with `callback`.
7073
'';
7174

72-
once = helpers.defaultNullOpts.mkBool false "Run the autocommand only once.";
75+
once = self.defaultNullOpts.mkBool false "Run the autocommand only once.";
7376

74-
nested = helpers.defaultNullOpts.mkBool false "Run nested autocommands.";
77+
nested = self.defaultNullOpts.mkBool false "Run nested autocommands.";
7578
};
7679

7780
autoCmdOption = types.submodule { options = autoCmdOptions; };

lib/builders.nix

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{ lib }:
2-
# NOTE: use local recursion instead of accessing `lib.nixvim.builders`.
3-
# The latter isn't a fixed shape since it may get the deprecated functions meregd in,
2+
# NOTE: use local recursion instead of accessing `self.builders`.
3+
# The latter isn't a fixed shape since it may get the deprecated functions merged in,
44
# which would lead to infinite recursion.
55
lib.fix (builders: {
66
# Curry a nixpkgs instance into the *With functions below, dropping the `With` suffix
@@ -147,7 +147,7 @@ lib.fix (builders: {
147147
prev:
148148
{
149149
nativeBuildInputs = prev.nativeBuildInputs or [ ] ++ [
150-
(lib.nixvim.builders.byteCompileLuaHookWith pkgs)
150+
(builders.byteCompileLuaHookWith pkgs)
151151
];
152152
}
153153
// lib.optionalAttrs (prev ? buildCommand) {

lib/default.nix

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -10,9 +10,12 @@ lib.fix (
1010
let
1111
# Used when importing parts of helpers
1212
call = lib.callPackageWith {
13-
inherit call pkgs self;
14-
helpers = self; # TODO: stop using `helpers` in the subsections
15-
lib = self.extendedLib;
13+
inherit
14+
call
15+
lib
16+
pkgs
17+
self
18+
;
1619
};
1720

1821
# Define this outside of the attrs to avoid infinite recursion,
@@ -35,12 +38,13 @@ lib.fix (
3538
{
3639
autocmd = call ./autocmd-helpers.nix { };
3740
deprecation = call ./deprecation.nix { };
38-
extendedLib = call ./extend-lib.nix { inherit lib; };
41+
extendedLib = call ./extend-lib.nix { }; # TODO: provide an overlay instead
3942
keymaps = call ./keymap-helpers.nix { };
4043
lua = call ./to-lua.nix { };
4144
modules = call ./modules.nix { };
4245
neovim-plugin = call ./neovim-plugin.nix { };
4346
options = call ./options.nix { };
47+
types = call ./types.nix { }; # NOTE: you should usually prefer the extended `lib.types`
4448
utils = call ./utils.nix { inherit _nixvimTests; };
4549
vim-plugin = call ./vim-plugin.nix { };
4650

lib/deprecation.nix

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,7 @@
1-
{ lib }:
1+
{
2+
lib,
3+
self,
4+
}:
25
rec {
36
# Get a (sub)option by walking the path,
47
# checking for submodules along the way
@@ -54,7 +57,7 @@ rec {
5457
let
5558
option = lib.toList option';
5659
oldPath = oldPrefix ++ option;
57-
newPath = newPrefix ++ map lib.nixvim.toSnakeCase option;
60+
newPath = newPrefix ++ map self.toSnakeCase option;
5861
in
5962
lib.mkRenamedOptionModule oldPath newPath
6063
);

lib/extend-lib.nix

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,7 @@
11
# Extends nixpkg's lib with our functions, as expected by our modules
22
{
3-
call,
43
lib,
5-
self,
4+
self ? import ./. { inherit lib; },
65
}:
76
lib.extend (
87
final: prev: {
@@ -13,6 +12,6 @@ lib.extend (
1312
maintainers = prev.maintainers // import ./maintainers.nix;
1413

1514
# Merge in our custom types
16-
types = prev.types // call ./types.nix { };
15+
types = prev.types // self.types;
1716
}
1817
)

lib/keymap-helpers.nix

Lines changed: 15 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,27 +1,30 @@
1-
{ lib, helpers }:
1+
{
2+
lib,
3+
self,
4+
}:
25
let
36
inherit (lib) optionalAttrs isAttrs types;
47
in
58
rec {
69
# These are the configuration options that change the behavior of each mapping.
710
mapConfigOptions = {
8-
silent = helpers.defaultNullOpts.mkBool false "Whether this mapping should be silent. Equivalent to adding `<silent>` to a map.";
11+
silent = self.defaultNullOpts.mkBool false "Whether this mapping should be silent. Equivalent to adding `<silent>` to a map.";
912

10-
nowait = helpers.defaultNullOpts.mkBool false "Whether to wait for extra input on ambiguous mappings. Equivalent to adding `<nowait>` to a map.";
13+
nowait = self.defaultNullOpts.mkBool false "Whether to wait for extra input on ambiguous mappings. Equivalent to adding `<nowait>` to a map.";
1114

12-
script = helpers.defaultNullOpts.mkBool false "Equivalent to adding `<script>` to a map.";
15+
script = self.defaultNullOpts.mkBool false "Equivalent to adding `<script>` to a map.";
1316

14-
expr = helpers.defaultNullOpts.mkBool false "Means that the action is actually an expression. Equivalent to adding `<expr>` to a map.";
17+
expr = self.defaultNullOpts.mkBool false "Means that the action is actually an expression. Equivalent to adding `<expr>` to a map.";
1518

16-
unique = helpers.defaultNullOpts.mkBool false "Whether to fail if the map is already defined. Equivalent to adding `<unique>` to a map.";
19+
unique = self.defaultNullOpts.mkBool false "Whether to fail if the map is already defined. Equivalent to adding `<unique>` to a map.";
1720

18-
noremap = helpers.defaultNullOpts.mkBool true "Whether to use the `noremap` variant of the command, ignoring any custom mappings on the defined action. It is highly advised to keep this on, which is the default.";
21+
noremap = self.defaultNullOpts.mkBool true "Whether to use the `noremap` variant of the command, ignoring any custom mappings on the defined action. It is highly advised to keep this on, which is the default.";
1922

20-
remap = helpers.defaultNullOpts.mkBool false "Make the mapping recursive. Inverses `noremap`.";
23+
remap = self.defaultNullOpts.mkBool false "Make the mapping recursive. Inverses `noremap`.";
2124

22-
desc = helpers.mkNullOrOption lib.types.str "A textual description of this keybind, to be shown in which-key, if you have it.";
25+
desc = self.mkNullOrOption lib.types.str "A textual description of this keybind, to be shown in which-key, if you have it.";
2326

24-
buffer = helpers.defaultNullOpts.mkBool false "Make the mapping buffer-local. Equivalent to adding `<buffer>` to a map.";
27+
buffer = self.defaultNullOpts.mkBool false "Make the mapping buffer-local. Equivalent to adding `<buffer>` to a map.";
2528
};
2629

2730
modes = {
@@ -118,9 +121,9 @@ rec {
118121
// (optionalAttrs (isAttrs action || action) {
119122
action = lib.mkOption (
120123
{
121-
type = types.maybeRaw types.str;
124+
type = self.types.maybeRaw types.str;
122125
description = "The action to execute.";
123-
apply = v: if options.lua.isDefined or false && config.lua then helpers.mkRaw v else v;
126+
apply = v: if options.lua.isDefined or false && config.lua then self.mkRaw v else v;
124127
}
125128
// (optionalAttrs (isAttrs action) action)
126129
// (optionalAttrs (defaults ? action) { default = defaults.action; })

lib/modules.nix

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ in
3131
lib.evalModules {
3232
modules = [ ../modules/top-level ] ++ modules;
3333
specialArgs = {
34-
inherit lib;
34+
lib = self.extendedLib;
3535
# TODO: deprecate `helpers`
3636
helpers = self;
3737
} // extraSpecialArgs;

lib/neovim-plugin.nix

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,7 @@
1-
{ lib, helpers }:
1+
{
2+
lib,
3+
self,
4+
}:
25
{
36
# TODO: DEPRECATED: use the `settings` option instead
47
extraOptionsOptions = {
@@ -68,7 +71,7 @@
6871

6972
setupCode = ''
7073
require('${luaName}')${setup}(${
71-
lib.optionalString (cfg ? settings) (helpers.toLuaObject cfg.settings)
74+
lib.optionalString (cfg ? settings) (self.toLuaObject cfg.settings)
7275
})
7376
'';
7477

@@ -106,15 +109,15 @@
106109
};
107110
}
108111
// lib.optionalAttrs hasSettings {
109-
settings = helpers.mkSettingsOption {
112+
settings = self.mkSettingsOption {
110113
description = settingsDescription;
111114
options = settingsOptions;
112115
example = settingsExample;
113116
};
114117
}
115118
// lib.optionalAttrs hasConfigAttrs {
116119
luaConfig = lib.mkOption {
117-
type = lib.types.pluginLuaConfig;
120+
type = self.types.pluginLuaConfig;
118121
default = { };
119122
description = "The plugin's lua configuration";
120123
};
@@ -162,6 +165,6 @@
162165
++ (lib.optional deprecateExtraOptions (
163166
lib.mkRenamedOptionModule (basePluginPath ++ [ "extraOptions" ]) settingsPath
164167
))
165-
++ (lib.nixvim.mkSettingsRenamedOptionModules basePluginPath settingsPath optionsRenamedToSettings);
168+
++ self.mkSettingsRenamedOptionModules basePluginPath settingsPath optionsRenamedToSettings;
166169
};
167170
}

lib/options.nix

Lines changed: 22 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,10 @@
1-
{ lib, helpers }:
1+
{
2+
lib,
3+
self,
4+
}:
25
let
36
inherit (lib) types;
7+
types' = self.types;
48

59
removed = lib.mapAttrs (name: msg: throw "${name} is removed. ${msg}") {
610
# Removed 2024-09-05
@@ -87,16 +91,16 @@ rec {
8791
);
8892
mkCompositeOption = description: options: mkCompositeOption' { inherit description options; };
8993

90-
mkNullOrStr' = args: mkNullOrOption' (args // { type = with types; maybeRaw str; });
94+
mkNullOrStr' = args: mkNullOrOption' (args // { type = types'.maybeRaw types.str; });
9195
mkNullOrStr = description: mkNullOrStr' { inherit description; };
9296

9397
mkNullOrLua' =
9498
args:
9599
mkNullOrOption' (
96100
args
97101
// {
98-
type = types.strLua;
99-
apply = helpers.mkRaw;
102+
type = types'.strLua;
103+
apply = self.mkRaw;
100104
}
101105
);
102106
mkNullOrLua = description: mkNullOrLua' { inherit description; };
@@ -106,8 +110,8 @@ rec {
106110
mkNullOrOption' (
107111
args
108112
// {
109-
type = types.strLuaFn;
110-
apply = helpers.mkRaw;
113+
type = types'.strLuaFn;
114+
apply = self.mkRaw;
111115
}
112116
);
113117
mkNullOrLuaFn = description: mkNullOrLua' { inherit description; };
@@ -117,8 +121,8 @@ rec {
117121
mkNullOrOption' (
118122
args
119123
// {
120-
type = with types; either strLua type;
121-
apply = v: if lib.isString v then helpers.mkRaw v else v;
124+
type = types.either types'.strLua type;
125+
apply = v: if lib.isString v then self.mkRaw v else v;
122126
}
123127
);
124128
mkNullOrStrLuaOr = type: description: mkNullOrStrLuaOr' { inherit type description; };
@@ -128,8 +132,8 @@ rec {
128132
mkNullOrOption' (
129133
args
130134
// {
131-
type = with types; either strLuaFn type;
132-
apply = v: if lib.isString v then helpers.mkRaw v else v;
135+
type = types.either types'.strLuaFn type;
136+
apply = v: if lib.isString v then self.mkRaw v else v;
133137
}
134138
);
135139
mkNullOrStrLuaFnOr = type: description: mkNullOrStrLuaFnOr' { inherit type description; };
@@ -149,14 +153,14 @@ rec {
149153
in
150154
rec {
151155
# TODO: removed 2024-06-14; remove stub 2024-09-01
152-
mkDesc = abort "mkDesc has been removed. Use the `pluginDefault` argument or `helpers.pluginDefaultText`.";
156+
mkDesc = abort "mkDesc has been removed. Use the `pluginDefault` argument or `self.pluginDefaultText`.";
153157

154158
mkNullable' = args: mkNullOrOption' (processDefaultNullArgs args);
155159
mkNullable =
156160
type: pluginDefault: description:
157161
mkNullable' { inherit type pluginDefault description; };
158162

159-
mkNullableWithRaw' = { type, ... }@args: mkNullable' (args // { type = types.maybeRaw type; });
163+
mkNullableWithRaw' = { type, ... }@args: mkNullable' (args // { type = types'.maybeRaw type; });
160164
mkNullableWithRaw =
161165
type: pluginDefault: description:
162166
mkNullableWithRaw' { inherit type pluginDefault description; };
@@ -188,7 +192,7 @@ rec {
188192
mkUnsignedInt' = args: mkNullableWithRaw' (args // { type = types.ints.unsigned; });
189193
mkUnsignedInt = pluginDefault: description: mkUnsignedInt' { inherit pluginDefault description; };
190194
mkFlagInt = pluginDefault: description: mkFlagInt' { inherit pluginDefault description; };
191-
mkFlagInt' = args: mkNullableWithRaw' (args // { type = types.intFlag; });
195+
mkFlagInt' = args: mkNullableWithRaw' (args // { type = types'.intFlag; });
192196
mkBool' = args: mkNullableWithRaw' (args // { type = types.bool; });
193197
mkBool = pluginDefault: description: mkBool' { inherit pluginDefault description; };
194198
mkStr' = args: mkNullableWithRaw' (args // { type = types.str; });
@@ -198,13 +202,13 @@ rec {
198202
mkAttributeSet = pluginDefault: description: mkAttributeSet' { inherit pluginDefault description; };
199203

200204
mkListOf' =
201-
{ type, ... }@args: mkNullable' (args // { type = with types; listOf (maybeRaw type); });
205+
{ type, ... }@args: mkNullable' (args // { type = types.listOf (types'.maybeRaw type); });
202206
mkListOf =
203207
type: pluginDefault: description:
204208
mkListOf' { inherit type pluginDefault description; };
205209

206210
mkAttrsOf' =
207-
{ type, ... }@args: mkNullable' (args // { type = with types; attrsOf (maybeRaw type); });
211+
{ type, ... }@args: mkNullable' (args // { type = types.attrsOf (types'.maybeRaw type); });
208212
mkAttrsOf =
209213
type: pluginDefault: description:
210214
mkAttrsOf' { inherit type pluginDefault description; };
@@ -274,10 +278,7 @@ rec {
274278
]);
275279
apply = lib.mapNullable (
276280
value:
277-
if lib.isInt value then
278-
value
279-
else
280-
helpers.mkRaw "vim.diagnostic.severity.${lib.strings.toUpper value}"
281+
if lib.isInt value then value else self.mkRaw "vim.diagnostic.severity.${lib.strings.toUpper value}"
281282
);
282283
}
283284
);
@@ -288,10 +289,9 @@ rec {
288289
mkNullOrOption' (
289290
args
290291
// {
291-
type = with types; either ints.unsigned logLevel;
292+
type = with types; either ints.unsigned types'.logLevel;
292293
apply = lib.mapNullable (
293-
value:
294-
if lib.isInt value then value else helpers.mkRaw "vim.log.levels.${lib.strings.toUpper value}"
294+
value: if lib.isInt value then value else self.mkRaw "vim.log.levels.${lib.strings.toUpper value}"
295295
);
296296
}
297297
);

0 commit comments

Comments
 (0)