zbirenbaum / copilot-cmp

Lua plugin to turn github copilot into a cmp source
MIT License
1.13k stars 41 forks source link

Missing/Extra Character in Intelligent Code Completion #90

Closed xiaofenglee closed 1 year ago

xiaofenglee commented 1 year ago

HI, I've recently encountered a problem. There's a missing character in the position suggested by Copilot. When selecting the cmp suggestion, an extra character is added. ( If more than one character is entered, there's no problem. )

Please refer to the image for more details.

CleanShot 2023-09-06 at 23 59 01

CleanShot 2023-09-07 at 00 09 40@2x

-- use lazyvim config

{
    "nvim-cmp",
    dependencies = {
      {
        "zbirenbaum/copilot-cmp",
        dependencies = "copilot.lua",
        opts = {},
        config = function(_, opts)
          local copilot_cmp = require("copilot_cmp")
          copilot_cmp.setup(opts)
          -- attach cmp source whenever copilot attaches
          -- fixes lazy-loading issues with the copilot cmp source
          require("lazyvim.util").on_attach(function(client)
            if client.name == "copilot" then
              copilot_cmp._on_insert_enter({})
            end
          end)
        end,
      },
    },
    ---@param opts cmp.ConfigSchema
    opts = function(_, opts)
      table.insert(opts.sources, 1, { name = "copilot", group_index = 2 })
      opts.sorting = opts.sorting or require("cmp.config.default")().sorting
      table.insert(opts.sorting.comparators, 1, require("copilot_cmp.comparators").prioritize)
    end,
  }
jellis206 commented 1 year ago

I can confirm this is happening for me as well! Looks like there were some recent commits to change the starting character and do some trimming. The commit 307e9aba4ad1f3133768e803e62ada995230e950 set the starting character to 0 and then 11eb015fbf9f07ad1c72dbdc9d830ebac610b5cd undid that change in lua/copilot_cmp/format.lua in the same commit there were also some changes made to lua/copilot_cmp/completion_functions.lua which could be the culprit. I am fairly new to neovim and doing plugin stuff, so I have no idea how I would start testing this out locally. This started happening after I updated neovim a couple hours ago and after checking out all my other plugins that could be messing with my autocomplete stuff this is what makes the most sense to me.

If I manually move my cursor one character to the left and then accept the copilot completion this bug doesn't occur. I have ghost text turned on and I notice that this will always happen if the first character is consumed or missing if the autocomplete should be ls.filetype_extend("c", { "cdoc" }) for example, the ghost text will display s.filetype_extend("c", { "cdoc" }). This is what made me think that the first character is being consumed and then when it gets inserted it is adding it back along with the full completion (idk what is really going on).

I have my nvim-cmp select and confirm behavior set to replace instead of insert but the issue occurs with either behavior.

jellis206 commented 1 year ago

I see there are quite a few issues open so I can try to figure this out locally and submit a PR because it will probably start driving me a little crazy tomorrow haha.

zbirenbaum commented 1 year ago

I can't seem to reproduce this at all, I can't even get the pictured completion to appear when I copy that file. Could you try reverting the commit and see if it does anything?

I had a tab completion function that was disabling completion on indent only lines. I found the trigger. I'll try to fix now

zbirenbaum commented 1 year ago

I see there are quite a few issues open so I can try to figure this out locally and submit a PR because it will probably start driving me a little crazy tomorrow haha.

Yeah there are a good amount. The problem is that fixing one issue typically causes a slew of other ones when dealing with cmp. When I had an api for users creating their own format handlers no one seemed to use them.

cmp and the lsp spec in general isn't really designed to handle multiline completions, and attempts to fix it seems to make things worse in half the cases.

jellis206 commented 1 year ago

Yeah that makes sense, I never remember having an issue with multiline completions but I also tend to avoid the multi line completions because I end up having to fix more than is worth it half the time. Out of curiosity what was the trigger? I was only like halfway through figuring out how to use a local copy of a plugin instead of the one my plugin manager was wanting to use.

zbirenbaum commented 1 year ago

Yeah that makes sense, I never remember having an issue with multiline completions but I also tend to avoid the multi line completions because I end up having to fix more than is worth it half the time. Out of curiosity what was the trigger? I was only like halfway through figuring out how to use a local copy of a plugin instead of the one my plugin manager was wanting to use.

I have a couple of theories, but the most likely scenario is an indent size mismatch since I haven't seen this before. The reason this is specifically happening with go is that copilot returns completions with 4 spaces per indent regardless of the apparent indent level. Really annoying, not sure what the best way to fix is.

Setting the insert start character to 0 fixes the problem. There's some weird stuff going on with the indent replacement parsing. The changes I made to get cmp to detect and match the correct part of multiline completions broke it and I'm going to have to rewrite it from scratch probably since the progressive 'minor fixes' have made things a mess.

My current plan is to use the text before the cursor to determine the starting indent level and the editor shiftwidth to determine the subsequent relative indent level.

zbirenbaum commented 1 year ago

Test out #92

xiaofenglee commented 1 year ago

I've downloaded and tested the newly submitted PR. The unexplained extra character issue has been resolved. Thanks. However, the format alignment issue seems to still occur (although this doesn't really affect my usage).

CleanShot 2023-09-07 at 21 13 06

For the indentation misalignment issue, please refer to the image below.

Intelligent suggestions that appear after a new line are missing a character. CleanShot 2023-09-07 at 21 18 44@2x

Upon selecting the intelligent suggestion, the format becomes disordered (The results are as expected when pressing Tab or Enter at this time). CleanShot 2023-09-07 at 21 19 35@2x

Thanks again for your bug fix. It's a great help to me.

zbirenbaum commented 1 year ago

I've downloaded and tested the newly submitted PR.

The unexplained extra character issue has been resolved. Thanks.

However, the format alignment issue seems to still occur (although this doesn't really affect my usage).

CleanShot 2023-09-07 at 21 13 06

For the indentation misalignment issue, please refer to the image below.

Intelligent suggestions that appear after a new line are missing a character.

CleanShot 2023-09-07 at 21 18 44@2x

Upon selecting the intelligent suggestion, the format becomes disordered (The results are as expected when pressing Tab or Enter at this time).

CleanShot 2023-09-07 at 21 19 35@2x

Thanks again for your bug fix. It's a great help to me.

What alignment issue are you talking about here? It looks perfect to me in the video so I'm a bit confused

Are you referring to the ghost text? That's the only problem I see

zbirenbaum commented 1 year ago

I'm going to merge the fix since this seems to work for most things and then address the alignment in a follow up, please make a new issue for it @xiaofenglee

jellis206 commented 1 year ago

Thank you @zbirenbaum!