zbirenbaum / copilot-cmp

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

Extra brackets when using copilots completion #41

Closed xbladesub closed 1 year ago

xbladesub commented 1 year ago

How to avoid extra brackets when accept suggestion from copilot-cmp? It is not constantly but sometimes quite annoying.

my config:

        -- {
        --  "github/copilot.vim"
        -- },
        { "zbirenbaum/copilot.lua",
            event = "VimEnter",
            config = function()
                vim.defer_fn(function()
                    require("copilot").setup()
                end, 100)
            end,
        },

        { "zbirenbaum/copilot-cmp",
            after = { "copilot.lua" },
            config = function()
                require("copilot_cmp").setup()
            end
        },

Got something like this when accepting suggestions:

Screenshot 2022-12-06 at 7 27 51 PM
zbirenbaum commented 1 year ago

This shouldn't occur if cmp is set up properly. Take a look at the snippets in the readme, and make sure something like this one is in your config:

    ["<CR>"] = cmp.mapping.confirm({
      behavior = cmp.ConfirmBehavior.Replace,
      select = false,
    }),

If that still doesn't work, you can try the snippet in the readme related to format.remove_existing, but I find it causes more problems than it fixes if you set up cmp properly and don't recommend it.

technicalpickles commented 1 year ago

I am getting this too, but sometimes it's parens, brackets, quotes, etc. I suspect it might have to do with nvim-autopairs? There are some notes on setup with cmp that I didn't have, but doesn't seem to help.

local lsp = require("lsp-zero")
local cmp = require("cmp")

-- ...

local cmp_mappings = lsp.defaults.cmp_mappings({
  ["<CR>"] = cmp.mapping.confirm({
          behavior = cmp.ConfirmBehavior.Replace,
           select = false,
  }),
})

lsp.setup_nvim_cmp({
  mappings = cmp_mappings,
})

lsp.setup()

-- for cmp + autopairs: https://github.com/windwp/nvim-autopairs#mapping-cr
-- and it needs to come after lsp-zero is configured: https://github.com/VonHeikemen/lsp-zero.nvim/discussions/119
local cmp_autopairs = require('nvim-autopairs.completion.cmp')
cmp.event:on(
  'confirm_done',
  cmp_autopairs.on_confirm_done()
)

So maybe a duplicate of https://github.com/zbirenbaum/copilot-cmp/issues/31?

eerii commented 1 year ago

Hey @technicalpickles, I was having the same issue and wen't over everything and my issue was that I was calling cmp.event before lsp-zero.setup(), so thank you so much for your comment.

I think you have a small typo on lsp.setup_nvim_cmp(), because as far as I can tell the property you want is mapping, not mappings (link). After changing those things it seems to be working great now. Here is my config in case it is useful for you:

local lsp = require('lsp-zero')
local cmp = require('cmp')

-- ...

local cmp_config = {
    method = 'getCompletionsCycling',
    sources = {
        { name = 'copilot', max_item_count = 3},
        { name = 'path' },
        { name = 'nvim_lsp', keyword_length = 3 },
        { name = 'buffer', keyword_length = 3 },
        { name = 'luasnip', keyword_length = 2 },
    },
    mapping = lsp.defaults.cmp_mappings({
        ['<Tab>'] = vim.schedule_wrap(function(fallback)
            if cmp.visible() and has_words_before() then
                cmp.select_next_item({ behavior = cmp.SelectBehavior.Select })
            else
                fallback()
            end
        end),
        ['<S-CR>'] = vim.schedule_wrap(function(fallback) -- don't select anything
            if cmp.visible() then
                cmp.close()
            end
            fallback()
        end),
        ["<CR>"] = cmp.mapping.confirm({ -- copilot-cmp
            behavior = cmp.ConfirmBehavior.Replace,
            select = false,
        }),
    }),
    window = {
        border = 'rounded',
    },
    experimental = {
        ghost_text = true,
    }
}

-- ...

-- LSP and CMP preferences
lsp.set_preferences(lsp_config)
lsp.setup_nvim_cmp(cmp_config)

-- Setup LSP
lsp.setup()

-- Fix for autopairs with cmp
cmp.event:on('confirm_done', require('nvim-autopairs.completion.cmp').on_confirm_done())
zbirenbaum commented 1 year ago

Since there hasn't been any update for a while and a solution was provided I'm going to close this.

ch3n9w commented 1 year ago

Hello, seems the issue still exists. My configration:

    local cmp = require('cmp')
    local compare = require('cmp.config.compare')
    local lspkind = require('lspkind')
    local luasnip = require 'luasnip'
    require("copilot").setup({
        suggestion = { enabled = false },
        panel = { enabled = false },
    })
    require("copilot_cmp").setup()
...

    cmp.setup({
        completion = {
            completeopt = 'menu,menuone,noinsert',
        },
        window = {
            completion = {
                border = border,
                scrollbar = '║',
            },
            documentation = {
                border = 'rounded'
            }
        },
        snippet = {
            expand = function(args)
                require('luasnip').lsp_expand(args.body)
            end,
        },
        formatting = {
            format = lspkind.cmp_format({
                mode = 'symbol_text',
                maxwidth = 50,
                symbol_map = { Copilot = "" }
            }),
        },
        mapping = {
            ['<CR>'] = cmp.mapping.confirm({
                behavior = cmp.ConfirmBehavior.Replace,
                select = false
            }),
            ['<Down>'] = cmp.mapping(function(fallback)
                if cmp.visible() then
                    cmp.select_next_item()
                else
                    fallback()
                end
            end, { 'i', 's' }),
            ['<Up>'] = cmp.mapping(function(fallback)
                if cmp.visible() then
                    cmp.select_prev_item()
                else
                    fallback()
                end
            end, { 'i', 's' }),
        },
        sources = cmp.config.sources({
            { name = 'nvim_lsp' },
            { name = 'luasnip' },
            { name = 'copilot' },
        }, {
            { name = 'buffer' },
        }),
        sorting = {
            priority_weight = 1.0,
            comparators = {
                require("copilot_cmp.comparators").prioritize,
                compare.offset,
                compare.exact,
                -- compare.scopes,
                compare.score,
                compare.recently_used,
                compare.locality,
                compare.kind,
                compare.sort_text,
                compare.length,
                compare.order,
            },
        }
    })

    local cmp_autopairs = require('nvim-autopairs.completion.cmp')
    cmp.event:on(
        'confirm_done',
        cmp_autopairs.on_confirm_done()
    )

... (lsp configuration)

the when I try to insert suggestions from copilot, like: image I press Up key: image and press CR: image

The extra brackets still exists.

bartoszluka commented 1 year ago

maybe solve it like this? https://github.com/jcdickinson/codeium.nvim/pull/55

zbirenbaum commented 1 year ago

maybe solve it like this? https://github.com/jcdickinson/codeium.nvim/pull/55

Copilot-cmp already replaces full range unfortunately. Any existing bugs are in spite of that fix.