From 63ea5bd71fe00bf67cab6f1785630700fc7d1bf8 Mon Sep 17 00:00:00 2001 From: Ching Pei Yang Date: Mon, 23 Dec 2024 03:59:57 +0100 Subject: [PATCH] blink: keymap wrapper --- .../completion/blink-cmp/blink-cmp.nix | 50 ++++++++++++++----- .../plugins/completion/blink-cmp/config.nix | 31 ++++++++++++ 2 files changed, 69 insertions(+), 12 deletions(-) diff --git a/modules/plugins/completion/blink-cmp/blink-cmp.nix b/modules/plugins/completion/blink-cmp/blink-cmp.nix index 8b1aeb9c..a55dd43d 100644 --- a/modules/plugins/completion/blink-cmp/blink-cmp.nix +++ b/modules/plugins/completion/blink-cmp/blink-cmp.nix @@ -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 - "" = ["select_prev" "fallback"]; - "" = [ - (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"; + + "" = ["select_prev" "fallback"]; + "" = [ + (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]" ""; + confirm = mkMappingOption "Confirm [blink.cmp]" ""; + next = mkMappingOption "Next item [blink.cmp]" ""; + previous = mkMappingOption "Previous item [blink.cmp]" ""; + close = mkMappingOption "Close [blink.cmp]" ""; + scrollDocsUp = mkMappingOption "Scroll docs up [blink.cmp]" ""; + scrollDocsDown = mkMappingOption "Scroll docs down [blink.cmp]" ""; + }; }; } diff --git a/modules/plugins/completion/blink-cmp/config.nix b/modules/plugins/completion/blink-cmp/config.nix index 663e19c5..233fff3d 100644 --- a/modules/plugins/completion/blink-cmp/config.nix +++ b/modules/plugins/completion/blink-cmp/config.nix @@ -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" + ]; + }; }; }; };