diff --git a/README.md b/README.md index 305b86b..5fd866b 100644 --- a/README.md +++ b/README.md @@ -138,6 +138,23 @@ lua <", function() + if require("tabnine.keymaps").has_suggestion() then + return require("tabnine.keymaps").accept_suggestion() + elseif require("luasnip").jumpable(1) then + return require("luasnip").jump(1) + else + return "" + end +end, { expr = true }) +``` + ## Activate Tabnine Pro - `:TabnineHub` - to open Tabnine Hub and log in to your account diff --git a/doc/tabnine.txt b/doc/tabnine.txt index f4598d9..186f026 100644 --- a/doc/tabnine.txt +++ b/doc/tabnine.txt @@ -9,6 +9,7 @@ Introduction |tabnine-nvim-introduction| Usage |tabnine-nvim-usage| Commands |tabnine-nvim-commands| Configuration |tabnine-nvim-configuration| +Advanced keymaps |tabnine-nvim-advanced-keymaps| ============================================================================== INTRODUCTION *tabnine-nvim-introduction* @@ -32,7 +33,7 @@ This will initialize and setup the plugin to start using completion. Further configuration can be passed to the setup function (|CONFIGURATION|) ============================================================================== -COMMANDS +COMMANDS *tabnine-nvim-commands* Print Tabnine status :TabnineStatus @@ -82,9 +83,11 @@ See https://vim.fandom.com/wiki/Disable_automatic_comment_insertion *accept_keymap* - String - Default: "" The key to press to accept the current completion. +Note: Set to `false` to disable the accept keymap *dismiss_keymap* - String - Default: "" The key to press to hide the current completion. +Note: Set to `false` to disable the dismiss keymap *debounce_ms* - Integer - Default: 800 - Minumum: 0 The number of milliseconds to wait between keystrokes before giving @@ -105,5 +108,25 @@ Use :|setfiletype| to see available file types. An absolute path to Tabnine log file. +============================================================================== +ADVANCED KEYMAPS *tabnine-nvim-advanced-keymaps* + +You can set `accept_keymap` and `dismiss_keymap` to `false` to disable them +then you can create mappings using `require('tabnine.keymaps')` +>lua +--- Example integration with Tabnine and LuaSnip +--- falling back to inserting tab if neither has a completion +vim.keymap.set("i", "", function() + if require("tabnine.keymaps").has_suggestion() then + return require("tabnine.keymaps").accept_suggestion() + elseif require("luasnip").jumpable(1) then + return require("luasnip").jump(1) + else + return "" + end +end, { expr = true }) +< + + ============================================================================== vim:tw=78:ts=4:ft=help:norl:noet:fen:noet: diff --git a/lua/tabnine/completion.lua b/lua/tabnine/completion.lua index 4a3f811..41d3fe6 100644 --- a/lua/tabnine/completion.lua +++ b/lua/tabnine/completion.lua @@ -14,6 +14,8 @@ end function M.accept() M.clear() + --- Don't error if no completion available! + if not state.rendered_completion or state.rendered_completion == "" then return end local lines = utils.str_to_lines(state.rendered_completion) if utils.starts_with(state.rendered_completion, "\n") then diff --git a/lua/tabnine/keymaps.lua b/lua/tabnine/keymaps.lua index 9eb6554..d0119db 100644 --- a/lua/tabnine/keymaps.lua +++ b/lua/tabnine/keymaps.lua @@ -3,21 +3,36 @@ local completion = require("tabnine.completion") local config = require("tabnine.config") local state = require("tabnine.state") +---@return boolean +function M.has_suggestion() + return state.active == true and state.completions_cache ~= nil +end +M.accept_suggestion = vim.schedule_wrap(function() + if not M.has_suggestion() then return end + return completion.accept() +end) +M.dismiss_suggestion = vim.schedule_wrap(function() + if not M.has_suggestion() then return end + completion.clear() + state.completions_cache = nil +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 }) + if accept_keymap then -- allow setting to `false` to disable + vim.keymap.set("i", accept_keymap, function() + if not M.has_suggestion() then return accept_keymap end + return M.accept_suggestion() + end, { expr = true }) + end - 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 }) + if dismiss_keymap then -- allow setting to `false` to disable + vim.keymap.set("i", dismiss_keymap, function() + if not M.has_suggestion() then return dismiss_keymap end + return M.dismiss_suggestion() + end, { expr = true }) + end end return M