zbirenbaum / copilot-cmp

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

line is cleared on confirm #35

Closed rebelot closed 1 year ago

rebelot commented 1 year ago

Hi, I'm having this issue since recently, where using <CR> to confirm a suggestion clears the entire line, in the sense that no matter what was on the line and cursor position I end up with a blank line and cursor at column 1. This seems to happen only for single-line suggestions, multiline snippets get inserted correctly.

EDIT: behaviour seems related to clear_after_cursor option, as setting it to false does not clear the entire line, although suggestion is inserted with super f'd up indenting. force_autofmt seems to do nothing.

I am not sure if this is an interaction problem with other plugins, load-order hell or a bug in copilot-cmp. These are some bits of configuration. Removing all lazy loading does not change anything.

copilot config:

    use({
        "zbirenbaum/copilot.lua",
        event = { "VimEnter" },
        config = function()
            vim.defer_fn(function()
                require("copilot").setup({
                    ft_disable = { "julia", "dap-repl" },
                    suggestion = {
                        keymap = {
                            accept = "<M-CR>",
                            next = "<M-n>",
                            prev = "<M-p>",
                            dismiss = "<M-]>",
                        },
                    },
                })
            end, 1000)
        end,
    })
    use({
        "zbirenbaum/copilot-cmp",
        after = { "copilot.lua", "nvim-cmp" },
        config = function()
            require("copilot_cmp").setup()
        end,
    })

relevant cmp config:

    use({
        "hrsh7th/nvim-cmp",
        event = { "InsertEnter", "CmdLineEnter" },
        after = "ultisnips",
        config = function()
            require("plugins.cmp")
        end,
    })
        ["<CR>"] = cmp.mapping({
            i = cmp.mapping.confirm({ behavior = cmp.ConfirmBehavior.Replace, select = false }),
            c = function(fallback)
                if cmp.visible() and cmp.get_selected_entry() then
                    cmp.confirm({ behavior = cmp.ConfirmBehavior.Replace, select = false })
                else
                    fallback()
                end
            end,
        }

relevant autopairs config:

    use({
        "windwp/nvim-autopairs",
        after = { "nvim-cmp" },
        config = function()
            require("plugins.autopairs")
        end,
    })
local cmp_autopairs = require("nvim-autopairs.completion.cmp")
local cmp = require("cmp")
cmp.event:on(
    "confirm_done",
    cmp_autopairs.on_confirm_done({
        map_char = {
            tex = "",
        },
    })
)
zbirenbaum commented 1 year ago

I noticed this last night as well. The strange thing is that it seems language dependent, as it works fine in lua but breaks for typescript. Debugging the cmp entry data didn't yield any unexpected results either. There haven't been any changes that could cause this in copilot.lua or cmp, so I think the problem is either upstream or in the copilot backend.

I'll do some some more testing to make sure that the issue isn't on my end, but I think that this might be a Microsoft problem...

zbirenbaum commented 1 year ago

Ok I found the issue, nvim-cmp made some changes that caused this by breaking clear_after_cursor. It seems like those same changes made clear after cursor unnecessary as long as you are using the 'replace' behavior for confirming your completion in the cmp config. I'll remove the option for now.

rebelot commented 1 year ago

Great, any clue about indentation? seems like indentation of the first line of a suggestion is "doubled": that is, if there's one indentation level before the cursor when accepting the suggestion, the suggestion will appear at two indentation levels, if there's two indentation levels before the cursor when accepting, the suggestion will appear at four indentation levels and so on. This happens only to the first line of a suggestion, whether it is a single-line or multi-line suggestion.

https://user-images.githubusercontent.com/36300441/194833173-9ea507f9-a0f4-4142-a149-a2ce96809071.mov

(P.S. code makes no sense, just fiddling to trigger some copilot completion)

zbirenbaum commented 1 year ago

Great, any clue about indentation? seems like indentation of the first line of a suggestion is "doubled": that is, if there's one indentation level before the cursor when accepting the suggestion, the suggestion will appear at two indentation levels, if there's two indentation levels before the cursor when accepting, the suggestion will appear at four indentation levels and so on. This happens only to the first line of a suggestion, whether it is a single-line or multi-line suggestion.

Screen.Recording.2022-10-10.at.11.08.15.mov (P.S. code makes no sense, just fiddling to trigger some copilot completion)

Now this is interesting. I'm not sure why that's happening, but you should be able to override the indentlevel by editing the completion's contents with a function supplied in the formatters parameter. However, I found a substantially better way of implementing it the other day, and the api might change slightly in a few days.

Would you mind opening a new issue so that once a solution is found, anyone else with indent issues will be able to easily find it?

zbirenbaum commented 1 year ago

Ok I reproduced the indentation issue while working. I think this is caused by changes in cmp that broke other stuff too. I'll have a fix out soon.

zbirenbaum commented 1 year ago

I pushed a fix for indentation. Still not positive whether it was a change in cmp or copilot or some combination, but everything seems to be working fine for me now @rebelot

rebelot commented 1 year ago

Thanks @zbirenbaum I just did some quick tests and all seems fine to me as well!

zbirenbaum commented 1 year ago

Awesome, glad its resolved!