ThePrimeagen / vim-be-good

vim-be-good is a nvim plugin designed to make you better at Vim Movements.
3.04k stars 108 forks source link

Some error in ci{ game #136

Open deeperlearner opened 4 months ago

deeperlearner commented 4 months ago

The error come out sometimes in the ci{ game:

Error executing vim.schedule lua callback: ...pack/packer/start/vim-be-good/lua/vim-be-good/buffer.lua:103: Unexpected type
stack traceback:
        [C]: in function 'nvim_buf_set_lines'
        ...pack/packer/start/vim-be-good/lua/vim-be-good/buffer.lua:103: in function 'render'
        ...packer/start/vim-be-good/lua/vim-be-good/game-runner.lua:305: in function 'run'
        ...packer/start/vim-be-good/lua/vim-be-good/game-runner.lua:245: in function ''
        vim/_editor.lua: in function <vim/_editor.lua:0>

Neovim Version: v0.9.5

MannyC commented 4 months ago

I don't want to fix this because I don't know exactly how it was supposed to work, but this error is because the lines table being passed to nvim_buf_set_lines sometimes has something at the zeroth index. This has something to do with CiRound:Render interacting getRandomInsertionLocation but it's not clear to me how this was actually supposed to work (for instance I can't tell if gameLineCount is supposed to include the instructions or not, at 20 it's too low to include them but linesAfterInstructions = gameLineCount - #instructions seems to imply that they are included).

On the plus side I learned a little about Lua.

roguecat0 commented 4 months ago

I replace the vim.api.nvim_buf_set_lines(self.bufh, idx, idx + #lines, false, lines) line with this small snippet that shifts by 1 if the table starts from 0.

    local new_lines = {}
    if lines[0] ~= nil then
        new_lines[#new_lines+1] = lines[0]
        for i=1,#lines do
            new_lines[#new_lines+1] = lines[i]
        end
    else
        new_lines = lines
    end
    vim.api.nvim_buf_set_lines(self.bufh, idx, idx + #new_lines, false, new_lines)

It's pretty dirty but it works.