zbirenbaum / copilot.lua

Fully featured & enhanced replacement for copilot.vim complete with API for interacting with Github Copilot
MIT License
2.63k stars 75 forks source link

Copilot + autopairs issue #215

Open alexkatz opened 1 year ago

alexkatz commented 1 year ago

Often when inserting a copilot suggestion, I'll have something like the following occur:

Screenshot 2023-09-26 at 1 23 33 PM Screenshot 2023-09-26 at 1 22 43 PM

In the first image, copilot guesses exactly what I need after I've opened a starting ' character. In my case, mini.pairs automatically inserts the closing '.

You don't see this in the screenshot though, because exactly at the same instant the copilot suggestion appears, hiding the closing ' and replacing it entirely with the suggestion. At this point, accepting the copilot suggestion looks fine, even though it actually isn't.

After accepting the copilot suggestion, that pesky closing ' comes back and finds its way into the suggestion, creating the syntax error seen in the second screenshot. Weirdly, it's before the closing ), which itself is part of the suggestion.

At first, I thought of solving this with my auto pairs plugin, but this doesn't quite make sense. In my case mini.pairs is doing a great job inserting the closing ' character, as it should. It's only an issue once copilot inserts a suggestion.

Ultimately this seems like some kind of race condition that I'm not quite sure how to approach.

If I disable mini.pairs, the issue goes away entirely, as you'd probably expect -- so please feel free to close this if you deem it a non issue.

MunifTanjim commented 1 year ago

I'm using windwp/nvim-autopairs and this doesn't happen:

Kapture 2023-09-27 at 10 08 44

Could be that mini.pairs is doing something differently? 🤔

alexkatz commented 1 year ago

https://github.com/zbirenbaum/copilot.lua/assets/4224538/5e3ca35a-6c2c-4c2d-b4ee-ef76d8294699

Ah, interesting. Here's me trying with windwp/nvim-autopairs and I'm still seeing it, though intermittently.

Again, this all goes away if auto-pair plugins are disabled. I'm curious though if anyone else has encountered this.

sangdth commented 10 months ago

I face similar issue, in my case it's the {} or []

https://github.com/zbirenbaum/copilot.lua/assets/1083478/f87773f1-1882-4152-962f-928f24e0dbde

I typed:

const [|] <-- the autopairs adds the ]

After typing the test, the copilot suggest the , setTest ... If I press Tab, it will bring the ] to the end.

It's using windwp/nvim-autopairs and copilot.lua.

brenopacheco commented 8 months ago

For anyone looking for a quick fix, I was able to get this working with windwp/nvim-autopairs by adding the following:

require('cmp').event:on('confirm_done', function(evt)
    if evt.entry.completion_item then
        require('nvim-autopairs.completion.cmp').on_confirm_done()(evt)
        vim.api.nvim_exec_autocmds('CompleteChanged', {})
    end
end)
magoz commented 8 months ago

For anyone looking for a quick fix, I was able to get this working with windwp/nvim-autopairs by adding the following:

require('cmp').event:on('confirm_done', function(evt)
  if evt.entry.completion_item then
      require('nvim-autopairs.completion.cmp').on_confirm_done()(evt)
      vim.api.nvim_exec_autocmds('CompleteChanged', {})
  end
end)

I've added your solution, @brenopacheco, but I still see the issue if I pick the Copilot suggestion while I've already typed one '.

https://github.com/zbirenbaum/copilot.lua/assets/9190753/86bb844c-4441-4cb9-a691-5b76f2d35f81

sangdth commented 8 months ago

I wonder if we missed something. I tried that suggestion from brenopacheco too and still facing issue like magoz.

mmirus commented 8 months ago

@sangdth are you using copilot-cmp? Or just suggestions from copilot.lua? The suggested fix would only apply if you're accepting copilot suggestions from cmp.

adamsitar commented 8 months ago

Supposedly this issue has been occurring in other editors as well, check out this discussion - https://github.com/orgs/community/discussions/48319#discussioncomment-8261019

mmirus commented 8 months ago

Interesting. So possibly whatever happened upstream needs to be replicated in copilot.lua.

In the meanwhile, I am testing the below out. I use suggestions from copilot.lua without cmp. The below mapping for accepting the suggestion disables nvim-autopairs, accepts the suggestion, and then reenables autopairs.

return {
  "zbirenbaum/copilot.lua",
  dependencies = { "windwp/nvim-autopairs" },
  cmd = "Copilot",
  event = "InsertEnter",
  config = function()
    require("copilot").setup({
      suggestion = {
        auto_trigger = true,
      },
    })

    local autopairs = require("nvim-autopairs")
    local suggestion = require("copilot.suggestion")
    vim.keymap.set("i", "<C-l>", function()
      autopairs.disable()
      suggestion.accept()
      autopairs.enable()
    end, { desc = "Accept Copilot suggestion" })
  end,
}

Too soon to say if this fixes the problems I was experiencing, but so far so good.

sangdth commented 7 months ago

Sorry for late response, @mmirus. I forgot to update here, after removing the tabnine plugin, look like the copilot alone works OK for me.