monkoose / neocodeium

free AI completion plugin for neovim
MIT License
66 stars 0 forks source link

Question: behavior when working with `nvim-cmp` #2

Closed dpetka2001 closed 3 months ago

dpetka2001 commented 3 months ago

In my configuration with codeium.vim and nvim-cmp I have the following in the nvim-cmp spec

      -- Make codeium suggestions appear only when `nvim-cmp` menu is closed
      cmp.event:on("menu_opened", function()
        vim.g.codeium_manual = true
        vim.fn["codeium#Clear"]()
      end)
      cmp.event:on("menu_closed", function()
        vim.g.codeium_manual = false
        vim.fn["codeium#Complete"]()
      end)

I tried something similar with neocodeium with the following in the spec

      -- Make codeium suggestions appear only when `nvim-cmp` menu is closed
      cmp.event:on("menu_opened", function()
        neocodeium.clear()
      end)
      cmp.event:on("menu_closed", function()
        neocodeium.cycle_or_complete()
      end)

But seems to not be working. After playing around a bit, I noticed that even without the above code snippet, if I manually call neocodeium.clear(), then neocodeium.cycle_or_complete() does not work unless I type in a letter. On the other hand with codeium.vim you can call vim.fn["codeium#Complete"]() even after you've done vim.fn["codeium#Clear"]() without typing any letter.

Is there a way to simulate the behavior achieved with codeium.vim with neocodeium as well?

monkoose commented 3 months ago

Hello, thanks for feedback.

if I manually call neocodeium.clear(), then neocodeium.cycle_or_complete() does not work unless I type in a letter.

This is definitely a bug, I will fix it.

monkoose commented 3 months ago

cycle_or_complete() now should work. Be aware that I renamed complete(), complete_word() and complete_line() in previous commit to accept(), accept_word() and accept_line().

It doesn't completely fix you issue, because plugin doesn't currently expose manual option outside setup().

I have tested cmp.events and have found strange behavior with cmp.setup({ completion = { autocomplete = false } })

cmp.event:on("menu_closed", function()
  vim.print("bye")
end)

It prints bye on every key press even if completion is already closed.

dpetka2001 commented 3 months ago

The code snippet in my first post now works as expected. Thank you for that.

However after the recent update, I get the following error whenever I leave insert mode when pressing <Esc>. This happens even if I don't type any letters at all. So, when I open Neovim, I press i to enter insert mode and then immediately press <Esc> to return to normal mode.

Error executing lua callback: .../newstarter/lazy/neocodeium/lua/neocodeium/completer.lua:111: InsertLeave Autocommands for "*"..CursorMovedI Autocommands for "*": Vim(append):E1510: Value too large: 
stack traceback:
    [C]: in function 'doautocmd'
    .../newstarter/lazy/neocodeium/lua/neocodeium/completer.lua:111: in function 'cycle_or_complete'
    ...share/newstarter/lazy/neocodeium/lua/neocodeium/init.lua:196: in function 'cycle_or_complete'
    /home/jrn23/.config/newstarter/lua/plugins/codeium.lua:39: in function 'callback'
    ...l/share/newstarter/lazy/nvim-cmp/lua/cmp/utils/event.lua:47: in function 'emit'
    ...3/.local/share/newstarter/lazy/nvim-cmp/lua/cmp/core.lua:36: in function 'callback'
    ...l/share/newstarter/lazy/nvim-cmp/lua/cmp/utils/event.lua:47: in function 'emit'
    ...3/.local/share/newstarter/lazy/nvim-cmp/lua/cmp/view.lua:162: in function 'close'
    ...3/.local/share/newstarter/lazy/nvim-cmp/lua/cmp/init.lua:356: in function 'callback'
    ...share/newstarter/lazy/nvim-cmp/lua/cmp/utils/autocmd.lua:49: in function 'emit'
    ...share/newstarter/lazy/nvim-cmp/lua/cmp/utils/autocmd.lua:23: in function <...share/newstarter/lazy/nvim-cmp/lua/cmp/utils/autocmd.lua:22>
monkoose commented 3 months ago

Yes, I'm aware of this (were testing you snippets too).

Should be fixed now

You can try this snippet to work it as you expected

cmp.event:on("menu_opened", function()
  vim.cmd("NeoCodeium disable")
  require("neocodeium").clear()
end)

cmp.event:on("menu_closed", function()
  vim.cmd("NeoCodeium enable")
  require("neocodeium").cycle_or_complete()
end)
dpetka2001 commented 3 months ago

For completeness the configuration I'm testing is the following

return {
  {
    "monkoose/neocodeium",
    event = "VeryLazy",
    config = function()
      local neocodeium = require("neocodeium")
      neocodeium.setup({
        -- manual = true,
        filetypes = {
          TelescopePrompt = false,
          ["neo-tree-popup"] = false,
          ["dap-repl"] = false,
        },
      })
      -- stylua: ignore start
      vim.keymap.set("i", "<A-f>", function() neocodeium.accept() end)
      vim.keymap.set("i", "<A-w>", function() neocodeium.accept_word() end)
      vim.keymap.set("i", "<A-a>", function() neocodeium.accept_line() end)
      vim.keymap.set("i", "<A-e>", function() neocodeium.cycle_or_complete() end)
      vim.keymap.set("i", "<A-r>", function() neocodeium.cycle_or_complete(-1) end)
      vim.keymap.set("i", "<A-c>", function() neocodeium.clear() end)
      -- stylua: ignore end
    end,
  },
  {
    "nvim-cmp",
    opts = function(_, opts)
      local cmp = require("cmp")
      local neocodeium = require("neocodeium")

      opts.completion = {
        -- autocomplete = false,
      }
      -- Make codeium suggestions appear only when `nvim-cmp` menu is closed
      cmp.event:on("menu_opened", function()
        neocodeium.clear()
      end)
      cmp.event:on("menu_closed", function()
        neocodeium.cycle_or_complete()
      end)
    end,
  },
}
dpetka2001 commented 3 months ago

Wow, you're so fast!! Thank you a lot for helping me with this. Can verify the latest plugin update and the snippet you provided work as expected. Really appreciate it :smile:

monkoose commented 3 months ago

Thank you for reporting the issue. Wasn't aware of it.

The plugin is still beta quality, so if you find any other issues I will be appreciated of any feedback.

dpetka2001 commented 3 months ago

I will be using it in my testing configuration, so if I find anything else I will let you know.

monkoose commented 3 months ago

Seems like the fix was not tested well, now for me neocodeium suggestions flickers when cmp events are enabled, because of the nvim-cmp bug that invoke menu_closed way to often https://github.com/hrsh7th/nvim-cmp/issues/1187 and it is stale.

Will try to find a solution, but it is hard to fix something broken by desing.

dpetka2001 commented 3 months ago

I don't see any flickering?? Here is a screencast from my testing configuration

Screencast 2024-04-02 18:04:55.webm

dpetka2001 commented 3 months ago

I did however bump into another bug maybe? When I open Telescope, i get the following

Error detected while processing CursorMovedI Autocommands for "*"..CursorMovedI Autocommands for "*"..CursorMovedI Autocommands for "*"..CursorMovedI Autocommands for "*"..CursorMovedI Autocommands for "*"..CursorMovedI Autocommands for "*"..CursorMovedI Autocommands for "*"..CursorMovedI Autocommands for "*"..CursorMovedI Autocommands for "*"..CursorMovedI Autocommands for "*":
E218: Autocommand nesting too deep

even though I have

      neocodeium.setup({
        filetypes = {
          ["TelescopePrompt"] = false,
          ["neo-tree-popup"] = false,
          ["dap-repl"] = false,
        },
      })
dpetka2001 commented 3 months ago

I was able to overcome it by wrapping

    if utils.is_insert() then
      vim.schedule(function()
        nvim_exec_autocmds("CursorMovedI", { modeline = false })
      end)
    end

in a vim.schedule function. Not sure if it's the best solution. So far, I haven't seen any other breakage by this change. Maybe something you'd like to consider.

monkoose commented 3 months ago

I did however bump into another bug maybe? When I open Telescope, i get the following


Error detected while processing CursorMovedI Autocommands for "*"..CursorMovedI Autocommands for "*"..CursorMovedI Autocommands for "*"..CursorMovedI Autocommands for "*"..CursorMovedI Autocommands for "*"..CursorMovedI Autocommands for "*"..CursorMovedI Autocommands for "*"..CursorMovedI Autocommands for "*"..CursorMovedI Autocommands for "*"..CursorMovedI Autocommands for "*":

Should be fixed now.

I don't see any flickering?? Here is a screencast from my testing configuration

It visible when you type something, that is already shown in virtual text. Like in your example if you will continue typing vim.keymap.set... without accepting suggestion. It doesn't flicker that much as codeium.vim, but occasianally it now does.

dpetka2001 commented 3 months ago

Hmmmm, I tried continuing typing without accepting suggestion, but I still don't see anything. Maybe my eyes are not good as yours :stuck_out_tongue:

monkoose commented 3 months ago

Maybe depends on some other thing like pc spec or something else (but does codeium.vim flickering for you?). But anyway if you have checked cmp issue I linked, I don't think it is good idea to use menu_closed event. I guess I should add something like disable_fn option which will disable suggestions when it returns true, something like:

neocodeium.setup({
  disable_fn = function() require("cmp").visible() end
})

But it still would not clear suggestion when menu is opened, only when you scroll it and it makes it so so solution.

dpetka2001 commented 3 months ago

I really don't know what to tell you. For me it seems to be working perfectly as is. I'm using LazyVim distro as my config btw. Could you test it in LazyVim and see if it occurs? Maybe it's something with your configuration? Really no idea.