blink: keymap wrapper

This commit is contained in:
Ching Pei Yang 2024-12-23 03:59:57 +01:00
parent 581c5cbb85
commit 18843914f3
No known key found for this signature in database
GPG key ID: B3841364253DC4C8
2 changed files with 69 additions and 12 deletions

View file

@ -1,7 +1,19 @@
{lib, ...}: let
inherit (lib.options) mkEnableOption mkOption literalMD;
inherit (lib.types) listOf str either oneOf attrsOf;
inherit (lib.types) listOf str either attrsOf submodule enum;
inherit (lib.nvim.types) mkPluginSetupOption luaInline;
inherit (lib.nvim.binds) mkMappingOption;
keymapType = submodule {
freeformType = attrsOf (listOf (either str luaInline));
options = {
preset = mkOption {
type = enum ["default" "none" "super-tab" "enter"];
default = "none";
description = "keymap presets";
};
};
};
in {
options.vim.autocomplete.blink-cmp = {
enable = mkEnableOption "blink.cmp";
@ -15,24 +27,38 @@ in {
};
keymap = mkOption {
type = attrsOf (oneOf [luaInline str (listOf (either str luaInline))]);
type = keymapType;
default = {};
description = "blink.cmp keymap";
example = literalMD ''
```nix
"<Up>" = ["select_prev" "fallback"];
"<C-n>" = [
(lib.generators.mkLuaInline ''''
function(cmp)
if some_condition then return end -- runs the next command
return true -- doesn't run the next command
end,
'''')
"select_next"
];
vim.autocomplete.blink-cmp.setupOpts.keymap = {
preset = "none";
"<Up>" = ["select_prev" "fallback"];
"<C-n>" = [
(lib.generators.mkLuaInline ''''
function(cmp)
if some_condition then return end -- runs the next command
return true -- doesn't run the next command
end,
'''')
"select_next"
];
};
```
'';
};
};
mappings = {
complete = mkMappingOption "Complete [blink.cmp]" "<C-Space>";
confirm = mkMappingOption "Confirm [blink.cmp]" "<CR>";
next = mkMappingOption "Next item [blink.cmp]" "<Tab>";
previous = mkMappingOption "Previous item [blink.cmp]" "<S-Tab>";
close = mkMappingOption "Close [blink.cmp]" "<C-e>";
scrollDocsUp = mkMappingOption "Scroll docs up [blink.cmp]" "<C-d>";
scrollDocsDown = mkMappingOption "Scroll docs down [blink.cmp]" "<C-f>";
};
};
}

View file

@ -5,7 +5,9 @@
}: let
inherit (lib.modules) mkIf;
inherit (lib.generators) mkLuaInline;
cfg = config.vim.autocomplete.blink-cmp;
inherit (cfg) mappings;
in {
vim = mkIf cfg.enable {
lazy.plugins.blink-cmp = {
@ -35,6 +37,35 @@ in {
'';
jump = mkLuaInline "function(direction) require('luasnip').jump(direction) end";
};
keymap = {
${mappings.complete} = ["show" "fallback"];
${mappings.close} = ["hide" "fallback"];
${mappings.scrollDocsUp} = ["scroll_documentation_up" "fallback"];
${mappings.scrollDocsDown} = ["scroll_documentation_down" "fallback"];
${mappings.confirm} = ["accept" "fallback"];
${mappings.next} = [
"select_next"
"snippet_forward"
(mkLuaInline ''
function(cmp)
local line, col = unpack(vim.api.nvim_win_get_cursor(0))
has_words_before = col ~= 0 and vim.api.nvim_buf_get_lines(0, line - 1, line, true)[1]:sub(col, col):match("%s") == nil
if has_words_before then
return cmp.show()
end
end
'')
"fallback"
];
${mappings.previous} = [
"select_prev"
"snippet_backward"
"fallback"
];
};
};
};
};