diff --git a/build.lua b/build.lua deleted file mode 100644 index c4200f7..0000000 --- a/build.lua +++ /dev/null @@ -1,22 +0,0 @@ --- From https://github.com/nvim-neorg/neorg - --- This build.lua exists to bridge luarocks installation for lazy.nvim users. --- It's main purposes are: --- - Shelling out to luarocks.nvim for installation --- - Installing neocomplete's dependencies as rocks - --- Important note: we execute the build code in a vim.schedule --- to defer the execution and ensure that the runtimepath is appropriately set. - -vim.schedule(function() - local ok, luarocks = pcall(require, "luarocks-nvim.rocks") - - assert(ok, "Unable to install neocomplete: required dependency `vhyrro/luarocks.nvim` not found!") - - luarocks.ensure({ - "fzy == 1.0.3", - }) - - package.loaded["neocomplete"] = nil - require("neocomplete").setup() -end) diff --git a/lua/neocomplete/init.lua b/lua/neocomplete/init.lua index 823f733..91347b1 100644 --- a/lua/neocomplete/init.lua +++ b/lua/neocomplete/init.lua @@ -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") diff --git a/lua/neocomplete/mappings.lua b/lua/neocomplete/mappings.lua new file mode 100644 index 0000000..4f3e76c --- /dev/null +++ b/lua/neocomplete/mappings.lua @@ -0,0 +1,49 @@ +local mappings = {} + +function mappings.get_fallback(key) + local lhs = ("(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("(NeocompleteConfirm)", function() + require("neocomplete").mappings.confirm() + end) + + map("(NeocompleteSelectNext)", function() + require("neocomplete").mappings.select_next(1) + end) + + map("(NeocompleteSelectPrev)", function() + require("neocomplete").mappings.select_prev(1) + end) + + map("(NeocompleteClose)", function() + require("neocomplete").mappings.close() + end) +end + +return mappings diff --git a/lua/neocomplete/menu/draw.lua b/lua/neocomplete/menu/draw.lua index 0e06d10..92b3591 100644 --- a/lua/neocomplete/menu/draw.lua +++ b/lua/neocomplete/menu/draw.lua @@ -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] diff --git a/lua/neocomplete/menu/init.lua b/lua/neocomplete/menu/init.lua index 114628d..d4bc946 100644 --- a/lua/neocomplete/menu/init.lua +++ b/lua/neocomplete/menu/init.lua @@ -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 @@ -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