Skip to content

Commit

Permalink
feat: add tabnine.keymaps.has/accept/dismiss_suggestion
Browse files Browse the repository at this point in the history
  • Loading branch information
aarondill committed Mar 25, 2024
1 parent 5e813f8 commit d4a78a7
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 11 deletions.
2 changes: 2 additions & 0 deletions lua/tabnine/completion.lua
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
37 changes: 26 additions & 11 deletions lua/tabnine/keymaps.lua
Original file line number Diff line number Diff line change
Expand Up @@ -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

0 comments on commit d4a78a7

Please sign in to comment.