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

FR: flexible keymap support #128

Open
AbaoFromCUG opened this issue Oct 17, 2024 · 0 comments
Open

FR: flexible keymap support #128

AbaoFromCUG opened this issue Oct 17, 2024 · 0 comments

Comments

@AbaoFromCUG
Copy link

Thanks for your amazing plugin!

I setup keymap Super-Tab which support snippet forward and select next via Tab, which means when I press Tab, will check snippet forward, select next in order, or fallback to normal key(Tab).

Here is my workaround of blink.cmp via hacking require("blink.cmp.keymap").apply_keymap_to_current_buffer. But I would like a official solution, like nvim-cmp

    local keymap = require("blink.cmp.keymap")
    local function apply_keymap_to_current_buffer()
        -- skip if we've already applied the keymaps
        for _, mapping in ipairs(vim.api.nvim_buf_get_keymap(0, "i")) do
            if mapping.desc == "blink.cmp" then
                return
            end
        end
        local insert_keys_to_commands = {
            ["<CR>"] = { "accept", "hide" },
            ["<C-Space>"] = { "show" },
            ["<C-d>"] = { "scroll_documentation_down" },
            ["<C-u>"] = { "scroll_documentation_up" },
            ["<Tab>"] = { "snippet_forward", "select_next" },
            ["<Down>"] = { "snippet_forward", "select_next" },
            ["<C-n>"] = { "snippet_forward", "select_next" },
            ["<S-Tab>"] = { "snippet_backward", "select_prev" },
            ["<Up>"] = { "snippet_backward", "select_prev" },
            ["<C-p>"] = { "snippet_backward", "select_prev" },
        }
        local snippet_keys_to_commands = {
            ["<Tab>"] = { "snippet_forward" },
            ["<Down>"] = { "snippet_forward" },
            ["<C-n>"] = { "snippet_forward" },
            ["<S-Tab>"] = { "snippet_backward" },
            ["<Up>"] = { "snippet_backward" },
            ["<C-p>"] = { "snippet_forward" },
        }

        -- insert mode: uses both snippet and insert commands
        for key, commands in pairs(insert_keys_to_commands) do
            keymap.set("i", key, function()
                for _, command in ipairs(commands) do
                    local did_run = require("blink.cmp")[command]()
                    if did_run then
                        return
                    end
                end
                return keymap.run_non_blink_keymap("i", key)
            end)
        end

        -- snippet mode
        for key, commands in pairs(snippet_keys_to_commands) do
            keymap.set("s", key, function()
                for _, command in ipairs(commands) do
                    local did_run = require("blink.cmp")[command]()
                    if did_run then
                        return
                    end
                end
                return keymap.run_non_blink_keymap("s", key)
            end)
        end
    end

    keymap.apply_keymap_to_current_buffer = apply_keymap_to_current_buffer
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant