zbirenbaum / copilot-cmp

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

Full code snippet appears on nvim-cmp window but I can only accept the first line #49

Closed Vinni-Cedraz closed 1 year ago

Vinni-Cedraz commented 1 year ago

EDIT: I SOLVED IT AND IT WAS REALLY DUMB : I just figured it out when posting the cmd.lua configs here. There is a specific key to accept the suggestion entry and only when that key is pressed the entire suggestion is accepted, otherwise just the first line is inserted.

This is a different issue than the similar one we have there. That guy was having only the first line of code appearing in the nvim-cmp window. In my case the code appears entirely and without any issues in the window:

image

But pressing tab to accept it only writes out the first line on the file: image

Here are my relevant configurations:

on plugins.lua (file where I install plugins using packer):

    use({
        "zbirenbaum/copilot.lua",
        event = { "vimenter" },
        config = function()
            vim.defer_fn(function()
                require("user.copilot").setup()
            end, 100)
        end,
    })
    -- copilot integration with nvim-cmp
    use({
        "zbirenbaum/copilot-cmp",
        after = { "copilot.lua" },
        config = function()
            require("copilot_cmp").setup({
                method = "getcompletionscycling",
            })
        end,
    })

on my copilot.lua file (config file required by init.lua where I configure the copilot.lua plugin):

local M = {}

function M.setup()
    require("copilot").setup({
        filetypes = { "lua", "c", "javascript", "typescript", "rust", "python" },
        suggestion = { enabled = false},
        panel = { enabled = false },
        plugin_manager_path = vim.call("stdpath", "data"),
    })
end

return M

cmp.lua (configuration file for nvim-cmp):

local cmp_status_ok, cmp = pcall(require, "cmp")
if not cmp_status_ok then
    return
end

-- Define a highlight group for the Copilot icon
vim.cmd('highlight MyCopilotIcon guifg=#ffa500')

local check_backspace = function()
    local col = vim.fn.col(".") - 1
    return col == 0 or vim.fn.getline("."):sub(col, col):match("%s")
end

local kind_icons = {
    Text = "",
    Method = "",
    Function = "",
    Constructor = "",
    Field = "",
    Variable = "",
    Class = "",
    Interface = "",
    Module = "",
    Property = "",
    Unit = "",
    Value = "",
    Enum = "",
    Keyword = "",
    Snippet = "",
    Color = "",
    File = "",
    Reference = "",
    Folder = "",
    EnumMember = "",
    Constant = "",
    Struct = "",
    Event = "",
    Operator = "",
    TypeParameter = "",
    Copilot = vim.fn.nr2char(0xe708)
}

cmp.setup({
    snippet = {
        expand = function() end,
    },
    mapping = cmp.mapping.preset.insert({
        ["<C-k>"] = cmp.mapping.select_prev_item(),
        ["<C-j>"] = cmp.mapping.select_next_item(),
        ["<C-b>"] = cmp.mapping(cmp.mapping.scroll_docs(-1), { "i", "c" }),
        ["<C-f>"] = cmp.mapping(cmp.mapping.scroll_docs(1), { "i", "c" }),
        ["<C-Space>"] = cmp.mapping(cmp.mapping.complete(), { "i", "c" }),
        ["<C-e>"] = cmp.mapping({
            i = cmp.mapping.abort(),
            c = cmp.mapping.close(),
        }),
        ["<space>"] = cmp.mapping.confirm({ select = false }),
        ["<Tab>"] = cmp.mapping(function(fallback)
            if cmp.visible() then
                cmp.select_next_item()
            elseif check_backspace() then
                fallback()
            else
                fallback()
            end
        end, {
            "i",
            "s",
        }),
        ["<S-Tab>"] = cmp.mapping(function(fallback)
            if cmp.visible() then
                cmp.select_prev_item()
            else
                fallback()
            end
        end, {
            "i",
            "s",
        }),
    }),
    formatting = {
        fields = { "kind", "abbr", "menu" },
        format = function(entry, vim_item)
            vim_item.kind = kind_icons[vim_item.kind]
            vim_item.menu = ({
                nvim_lsp = "",
                nvim_lua = "",
                buffer = "",
                path = "",
                emoji = "",
            })[entry.source.name]
            return vim_item
        end,
    },
    sources = {
        { name = "nvim_lsp" },
        { name = "nvim_lua" },
        { name = "buffer" },
        { name = "path" },
        { name = "copilot", priority = 100 },
    },
    confirm_opts = {
        behavior = cmp.ConfirmBehavior.Replace,
        select = false,
    },
    window = {
        completion = cmp.config.window.bordered(),
        documentation = cmp.config.window.bordered(),
    },
    experimental = {
        ghost_text = true,
    },
})

That's all folks.

zbirenbaum commented 1 year ago

Since this is resolved I'm going to close it, glad you got it figured out!