From d0bebf0c2171b83a2a8d8bbd91ba216f08a6b875 Mon Sep 17 00:00:00 2001 From: aarondill Date: Sun, 30 Apr 2023 08:44:32 -0500 Subject: [PATCH] Export keymaps.accept_suggestion and dismiss_suggestion for remapping --- lua/tabnine/completion.lua | 7 +++++++ lua/tabnine/keymaps.lua | 28 ++++++++++++---------------- 2 files changed, 19 insertions(+), 16 deletions(-) diff --git a/lua/tabnine/completion.lua b/lua/tabnine/completion.lua index 8d8cd55..74962c0 100644 --- a/lua/tabnine/completion.lua +++ b/lua/tabnine/completion.lua @@ -13,6 +13,9 @@ local function valid_response(response) end function M.accept() + if not state.completions_cache then + return + end M.clear() local lines = utils.str_to_lines(state.rendered_completion) @@ -31,10 +34,14 @@ function M.accept() end function M.clear() + if not state.completions_cache then + return + end if state.cancel_completion then state.cancel_completion() end state.debounce_timer:stop() + api.nvim_buf_clear_namespace(0, consts.tabnine_namespace, 0, -1) end diff --git a/lua/tabnine/keymaps.lua b/lua/tabnine/keymaps.lua index 64ed2cb..0744ae3 100644 --- a/lua/tabnine/keymaps.lua +++ b/lua/tabnine/keymaps.lua @@ -3,25 +3,21 @@ local state = require("tabnine.state") local config = require("tabnine.config") local completion = require("tabnine.completion") +function M.accept_suggestion() + vim.schedule(completion.accept) +end +function M.dismiss_suggestion() + vim.schedule(function() + completion.clear() + state.completions_cache = nil + end) +end + function M.setup() local accept_keymap = config.get_config().accept_keymap local dismiss_keymap = config.get_config().dismiss_keymap - vim.keymap.set("i", accept_keymap, function() - if not state.completions_cache then - return accept_keymap - end - vim.schedule(completion.accept) - end, { expr = true }) - - vim.keymap.set("i", dismiss_keymap, function() - if not state.completions_cache then - return dismiss_keymap - end - vim.schedule(function() - completion.clear() - state.completions_cache = nil - end) - end, { expr = true }) + vim.keymap.set("i", accept_keymap, M.accept_suggestion, { expr = true }) + vim.keymap.set("i", dismiss_keymap, M.dismiss_suggestion, { expr = true }) end return M