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

Refactor(keymaps): export keymaps #70

Merged
merged 4 commits into from
Mar 27, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 17 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -138,6 +138,23 @@ lua <<EOF
EOF
```

### Advanced use cases:

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", "<tab>", 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 "<tab>"
end
end, { expr = true })
```

## Activate Tabnine Pro

- `:TabnineHub` - to open Tabnine Hub and log in to your account
Expand Down
25 changes: 24 additions & 1 deletion doc/tabnine.txt
Original file line number Diff line number Diff line change
Expand Up @@ -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*
Expand All @@ -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
Expand Down Expand Up @@ -82,9 +83,11 @@ See https://vim.fandom.com/wiki/Disable_automatic_comment_insertion

*accept_keymap* - String - Default: "<Tab>"
The key to press to accept the current completion.
Note: Set to `false` to disable the accept keymap

*dismiss_keymap* - String - Default: "<C-]>"
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
Expand All @@ -105,5 +108,25 @@ Use :|setfiletype| <C-d> 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", "<tab>", 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 "<tab>"
end
end, { expr = true })
<


==============================================================================
vim:tw=78:ts=4:ft=help:norl:noet:fen:noet:
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
Loading