copilot.lua
copilot.lua copied to clipboard
Copilot + autopairs issue
Often when inserting a copilot suggestion, I'll have something like the following occur:
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.
I'm using windwp/nvim-autopairs
and this doesn't happen:
Could be that mini.pairs is doing something differently? 🤔
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.
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
.
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)
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
I wonder if we missed something. I tried that suggestion from brenopacheco too and still facing issue like magoz.
@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.
Supposedly this issue has been occurring in other editors as well, check out this discussion - https://github.com/orgs/community/discussions/48319#discussioncomment-8261019
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.
Sorry for late response, @mmirus. I forgot to update here, after removing the tabnine
plugin, look like the copilot
alone works OK for me.