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

nvim-cmp crash, error "'start' is higher than 'end' " on selecting copilot item #79

Open
Rhymond opened this issue Jul 5, 2023 · 6 comments

Comments

@Rhymond
Copy link

Rhymond commented Jul 5, 2023

When I select a Copilot generated item, I encounter an error and nvim-cmp crashes with the error mentioned below. It looks like cmp tries to replace the entire line. However, I think that when my cursor is at the end of the line, it causes the cmp to crash and returns an error (this is just my wild guess).

Here is a video demonstrating the issue being replicated:
https://cln.sh/t6z8MwwN4YXHVDcMyTYr

cmp error:

E5108: Error executing lua ...lauskas/.local/share/nvim/lazy/nvim-cmp/lua/cmp/core.lua:396: 'start' is higher than 'end'                                                                     
stack traceback:                                                                                                                                                                             
        [C]: in function 'nvim_buf_set_text'                                                                                                                                                 
        ...lauskas/.local/share/nvim/lazy/nvim-cmp/lua/cmp/core.lua:396: in function <...lauskas/.local/share/nvim/lazy/nvim-cmp/lua/cmp/core.lua:384>                                       
        ...ocal/share/nvim/lazy/nvim-cmp/lua/cmp/utils/feedkeys.lua:47: in function <...ocal/share/nvim/lazy/nvim-cmp/lua/cmp/utils/feedkeys.lua:45> 

cmp configuration

local luasnip = require("luasnip")
local cmp = require("cmp")

local has_words_before = function()
    if vim.api.nvim_buf_get_option(0, "buftype") == "prompt" then return false end
    local line, col = unpack(vim.api.nvim_win_get_cursor(0))
    return col ~= 0 and vim.api.nvim_buf_get_text(0, line - 1, 0, line - 1, col, {})[1]:match("^%s*$") == nil
end

cmp.setup({
    snippet = {
        expand = function(args)
            require('luasnip').lsp_expand(args.body) -- For `luasnip` users.
        end,
    },
    window = {
        documentation = cmp.config.window.bordered()
    },
    mapping = cmp.mapping.preset.insert({
        ['<C-b>'] = cmp.mapping.scroll_docs(-4),
        ['<C-f>'] = cmp.mapping.scroll_docs(4),
        ['<C-Space>'] = cmp.mapping.complete(),
        ['<C-e>'] = cmp.mapping.abort(),
        ['<CR>'] = cmp.mapping.confirm({
            behavior = cmp.ConfirmBehavior.Replace,
            select = false,
        }),
        ["<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
            -- if cmp.visible() then
            --     cmp.select_next_item()
            -- elseif luasnip.expand_or_jumpable() then
            --     luasnip.expand_or_jump()
            -- elseif has_words_before() then
            --     cmp.complete()
            -- else
            --     fallback()
            -- end
        end, { "i", "s" }),

        ["<S-Tab>"] = vim.schedule_wrap(function(fallback)
            if cmp.visible() and has_words_before() then
                cmp.select_prev_item({ behavior = cmp.SelectBehavior.Select })
            else
                fallback()
            end
        end, { "i", "s" }),
    }),
    sources = cmp.config.sources({
        { name = "copilot", group_index = 2 },
        { name = 'nvim_lsp' },
        { name = 'luasnip' },
        -- { name = 'buffer' },
        { name = 'path' },
        -- { name = 'cmdline' },
    }),
    formatting = {
        format = function(entry, item)
            local menu_icon = {
                nvim_lsp = 'L',
                luasnip = 'S',
                buffer = 'B',
                path = 'P',
            }

            item.menu = menu_icon[entry.source.name]
            return item
        end,
    }
})

copilot settings

    require("copilot").setup({
        suggestion = { enabled = false },
        panel = { enabled = false },
    })

    require("copilot_cmp").setup()

I'm not sure if the issue lies with nvim-cmp or copilot-cmp package. However, I cannot reproduce the same error with other cmp selections.

@rsarv3006
Copy link

Running into this as well.

@sommerper
Copy link

sommerper commented Jul 16, 2023

I'm having the same issue, however, it's always on the third time I select something.

@zbirenbaum
Copy link
Owner

I haven't run into this, but I would guess it has to do with completion insert and replace ranges since I've seen similar errors when experimenting with those. Try out #92

@ivanlee1999
Copy link

ivanlee1999 commented Nov 7, 2023

Still encounter same issue
It happens every time when I try to edit fields of golang structs

@palcalde
Copy link

Same here, with golang structs.

@mahmoudk1000
Copy link

Running into this same problem.

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