Skip to content

Commit

Permalink
feat: add mappings api
Browse files Browse the repository at this point in the history
  • Loading branch information
max397574 committed Jun 25, 2024
1 parent 7b4fc19 commit 169b511
Show file tree
Hide file tree
Showing 4 changed files with 73 additions and 22 deletions.
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_next(count)
end,
select_next = function(count)
neocomplete.core.menu:select_prev(count)
end,
}

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

Expand Down
50 changes: 50 additions & 0 deletions lua/neocomplete/mappings.lua
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
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)
local key = get_mapping(plug)
vim.keymap.set("i", plug, function()
if require("neocomplete").core.menu:is_open() then
callback()
else
mappings.get_fallback(key)()
end
end)
end

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

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

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

map("<Plug>(NeocompleteClose)", function()
require("neocomplete").core.menu: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

0 comments on commit 169b511

Please sign in to comment.