Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

lib/keymaps: remove lua attr in type's merge fn #2342

Draft
wants to merge 1 commit into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
118 changes: 63 additions & 55 deletions lib/keymap-helpers.nix
Original file line number Diff line number Diff line change
Expand Up @@ -61,11 +61,8 @@ rec {

mapOptionSubmodule = mkMapOptionSubmodule { };

# NOTE: options that have the deprecated `lua` sub-option must use `removeDeprecatedMapAttrs`
# to ensure `lua` isn't evaluated when (e.g.) generating lua code.
# Failure to do so will result in "option used but not defined" errors!
# NOTE: `lua` was deprecated 2024-05-26
deprecatedMapOptionSubmodule = mkMapOptionSubmodule { lua = true; };
removeDeprecatedMapAttrs = v: builtins.removeAttrs v [ "lua" ];

mkModeOption =
default:
Expand Down Expand Up @@ -99,57 +96,68 @@ rec {
extraOptions ? { },
extraModules ? [ ],
}:
types.submodule (
{ config, options, ... }:
{
imports = extraModules;

options =
(lib.optionalAttrs (isAttrs key || key) {
key = lib.mkOption (
{
type = types.str;
description = "The key to map.";
example = "<C-m>";
}
// (optionalAttrs (isAttrs key) key)
// (optionalAttrs (defaults ? key) { default = defaults.key; })
);
})
// (optionalAttrs (isAttrs action || action) {
action = lib.mkOption (
{
type = types.maybeRaw types.str;
description = "The action to execute.";
apply = v: if options.lua.isDefined or false && config.lua then lib.nixvim.mkRaw v else v;
}
// (optionalAttrs (isAttrs action) action)
// (optionalAttrs (defaults ? action) { default = defaults.action; })
);
})
// optionalAttrs (isAttrs lua || lua) {
lua = lib.mkOption (
{
type = types.bool;
description = ''
If true, `action` is considered to be lua code.
Thus, it will not be wrapped in `""`.

This option is deprecated and will be removed in 24.11.
You should use a "raw" action instead, e.g. `action.__raw = ""`.
'';
visible = false;
}
// optionalAttrs (isAttrs lua) lua
);
}
// {
mode = mkModeOption defaults.mode or "";
options = mapConfigOptions;
}
// extraOptions;
}
);
let
type = types.submodule (
{ config, options, ... }:
{
imports = extraModules;

options =
(lib.optionalAttrs (isAttrs key || key) {
key = lib.mkOption (
{
type = types.str;
description = "The key to map.";
example = "<C-m>";
}
// (optionalAttrs (isAttrs key) key)
// (optionalAttrs (defaults ? key) { default = defaults.key; })
);
})
// (optionalAttrs (isAttrs action || action) {
action = lib.mkOption (
{
type = types.maybeRaw types.str;
description = "The action to execute.";
apply = v: if options.lua.isDefined or false && config.lua then lib.nixvim.mkRaw v else v;
}
// (optionalAttrs (isAttrs action) action)
// (optionalAttrs (defaults ? action) { default = defaults.action; })
);
})
// optionalAttrs (isAttrs lua || lua) {
lua = lib.mkOption (
{
type = types.bool;
description = ''
If true, `action` is considered to be lua code.
Thus, it will not be wrapped in `""`.

This option is deprecated and will be removed in 24.11.
You should use a "raw" action instead, e.g. `action.__raw = ""`.
'';
visible = false;
}
// optionalAttrs (isAttrs lua) lua
);
}
// {
mode = mkModeOption defaults.mode or "";
options = mapConfigOptions;
}
// extraOptions;
}
);
in
type
// {
# Remove deprecated attrs from the keymap
merge =
loc: defs:
builtins.removeAttrs (type.merge loc defs) [
"lua"
];
};

# Correctly merge two attrs (partially) representing a mapping.
mergeKeymap =
Expand Down
4 changes: 2 additions & 2 deletions modules/keymaps.nix
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,7 @@
extraConfigLua = lib.mkIf (config.keymaps != [ ]) ''
-- Set up keybinds {{{
do
local __nixvim_binds = ${helpers.toLuaObject (map helpers.keymaps.removeDeprecatedMapAttrs config.keymaps)}
local __nixvim_binds = ${helpers.toLuaObject config.keymaps}
for i, map in ipairs(__nixvim_binds) do
vim.keymap.set(map.mode, map.key, map.action, map.options)
end
Expand All @@ -99,7 +99,7 @@
callback = helpers.mkRaw ''
function()
do
local __nixvim_binds = ${helpers.toLuaObject (map helpers.keymaps.removeDeprecatedMapAttrs mappings)}
local __nixvim_binds = ${helpers.toLuaObject mappings}
for i, map in ipairs(__nixvim_binds) do
vim.keymap.set(map.mode, map.key, map.action, map.options)
end
Expand Down
1 change: 0 additions & 1 deletion plugins/by-name/barbar/default.nix
Original file line number Diff line number Diff line change
Expand Up @@ -206,7 +206,6 @@ lib.nixvim.neovim-plugin.mkNeovimPlugin {
};
lua = true;
};
apply = v: if v == null then null else keymaps.removeDeprecatedMapAttrs v;
description = "Keymap for function Buffer${funcName}";
}
) keymapsActions;
Expand Down
1 change: 0 additions & 1 deletion plugins/by-name/tmux-navigator/default.nix
Original file line number Diff line number Diff line change
Expand Up @@ -180,7 +180,6 @@ helpers.vim-plugin.mkVimPlugin {
lua = true;
}
);
apply = map helpers.keymaps.removeDeprecatedMapAttrs;
};
};

Expand Down
1 change: 0 additions & 1 deletion plugins/by-name/wtf/default.nix
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,6 @@ in
lua = true;
}
);
apply = v: if v == null then null else helpers.keymaps.removeDeprecatedMapAttrs v;
description = "Keymap for the ${action} action.";
}
) defaultKeymaps;
Expand Down
1 change: 0 additions & 1 deletion plugins/lsp/default.nix
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,6 @@ in

extra = mkOption {
type = with types; listOf helpers.keymaps.deprecatedMapOptionSubmodule;
apply = map helpers.keymaps.removeDeprecatedMapAttrs;
description = ''
Extra keymaps to register when an LSP is attached.
This can be used to customise LSP behaviour, for example with "telescope" or the "Lspsaga" plugin, as seen in the examples.
Expand Down