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

feat: add mappings api #28

Merged
merged 3 commits into from
Jun 26, 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
22 changes: 0 additions & 22 deletions build.lua

This file was deleted.

22 changes: 22 additions & 0 deletions lua/neocomplete/init.lua
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,30 @@ local function on_insert_enter()
neocomplete.core:setup()
end

neocomplete.mappings = {
get_fallback = function(key)
return require("neocomplete.mappings").get_fallback(key)
end,
is_open = function()
return neocomplete.core and neocomplete.core.menu:is_open()
end,
confirm = function()
neocomplete.core.menu:confirm()
end,
close = function()
neocomplete.core.menu:close()
end,
select_prev = function(count)
neocomplete.core.menu:select_prev(count)
end,
select_next = function(count)
neocomplete.core.menu:select_next(count)
end,
}

--- Sets up neocomplete
function neocomplete.setup()
require("neocomplete.mappings").setup()
require("neocomplete.config").setup()
require("neocomplete.highlights")

Expand Down
49 changes: 49 additions & 0 deletions lua/neocomplete/mappings.lua
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
local mappings = {}

function mappings.get_fallback(key)
local lhs = ("<Plug>(NeocompleteFallback.%s)"):format(key)
vim.keymap.set("i", lhs, key, { noremap = false })
return function()
vim.api.nvim_feedkeys(vim.keycode(lhs), "im", false)
end
end

local function get_mapping(rhs)
for _, map in pairs(vim.api.nvim_get_keymap("i")) do
---@diagnostic disable-next-line: undefined-field
if type(map.rhs) == "string" and map.rhs == rhs then
---@diagnostic disable-next-line: undefined-field
return map.lhs
end
end
end

local function map(plug, callback)
vim.keymap.set("i", plug, function()
if require("neocomplete").mappings.is_open() then
callback()
else
mappings.get_fallback(get_mapping(plug))()
end
end)
end

function mappings.setup()
map("<Plug>(NeocompleteConfirm)", function()
require("neocomplete").mappings.confirm()
end)

map("<Plug>(NeocompleteSelectNext)", function()
require("neocomplete").mappings.select_next(1)
end)

map("<Plug>(NeocompleteSelectPrev)", function()
require("neocomplete").mappings.select_prev(1)
end)

map("<Plug>(NeocompleteClose)", function()
require("neocomplete").mappings.close()
end)
end

return mappings
1 change: 1 addition & 0 deletions lua/neocomplete/menu/draw.lua
Original file line number Diff line number Diff line change
Expand Up @@ -115,6 +115,7 @@ return function(self)
virt_text_pos = "overlay",
})
end

local line = vim.api.nvim_get_current_line()
local cursor = vim.api.nvim_win_get_cursor(0)
local cursor_col = cursor[2]
Expand Down
2 changes: 2 additions & 0 deletions lua/neocomplete/menu/init.lua
Original file line number Diff line number Diff line change
Expand Up @@ -113,6 +113,7 @@ function Menu:set_scroll(direction)
end

function Menu:select_next(count)
count = count or 1
self.index = self.index + count
if self.index > #self.entries then
self.index = self.index - #self.entries - 1
Expand All @@ -122,6 +123,7 @@ function Menu:select_next(count)
end

function Menu:select_prev(count)
count = count or 1
self.index = self.index - count
if self.index < 0 then
self.index = #self.entries + self.index + 1
Expand Down
Loading