Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Copilot + autopairs issue #215

Open
alexkatz opened this issue Sep 26, 2023 · 10 comments
Open

Copilot + autopairs issue #215

alexkatz opened this issue Sep 26, 2023 · 10 comments

Comments

@alexkatz
Copy link

alexkatz commented Sep 26, 2023

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
Copy link
Collaborator

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
Copy link
Author

h3Mfhr5.-.Imgur.mp4

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
Copy link

sangdth commented Nov 14, 2023

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

Screen.Recording.2023-11-14.at.23.26.05.mov

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
Copy link

brenopacheco commented Jan 12, 2024

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
Copy link

magoz commented Jan 21, 2024

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

copilot-autopairs.mp4

@sangdth
Copy link

sangdth commented Jan 21, 2024

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

@mmirus
Copy link

mmirus commented Jan 30, 2024

@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
Copy link

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
Copy link

mmirus commented Jan 30, 2024

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
Copy link

sangdth commented Feb 7, 2024

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

7 participants