copilot.lua icon indicating copy to clipboard operation
copilot.lua copied to clipboard

Copilot + autopairs issue

Open alexkatz opened this issue 1 year ago • 10 comments

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.

alexkatz avatar Sep 26 '23 17:09 alexkatz

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? 🤔

MunifTanjim avatar Sep 27 '23 04:09 MunifTanjim

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.

alexkatz avatar Sep 27 '23 19:09 alexkatz

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.

sangdth avatar Nov 14 '23 21:11 sangdth

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)

brenopacheco avatar Jan 12 '24 15:01 brenopacheco

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

magoz avatar Jan 21 '24 11:01 magoz

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

sangdth avatar Jan 21 '24 14:01 sangdth

@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.

mmirus avatar Jan 30 '24 21:01 mmirus

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

adamsitar avatar Jan 30 '24 23:01 adamsitar

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.

mmirus avatar Jan 30 '24 23:01 mmirus

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

sangdth avatar Feb 07 '24 12:02 sangdth