Darazaki / indent-o-matic

Dumb automatic fast indentation detection for Neovim written in Lua
MIT License
176 stars 13 forks source link

fix: check if buffer line table is empty before getting the line size #20

Closed andrewmustea closed 1 year ago

andrewmustea commented 1 year ago

If a buffer has no lines, vim.api.get_buf_get_lines() will return an empty table. Attempting to index into the table will return a nil value.

Change the function detect() to verify if the buffer line table is empty before trying to use its first indexed value.

Darazaki commented 1 year ago

Interesting. But wouldn't vim.api.nvim_buf_get_lines() return {{}} rather than {}?

Inside an empty file:

:lua print(#vim.api.nvim_buf_get_lines(0, 0, 1, true))
1
:lua print(#vim.api.nvim_buf_get_lines(0, 0, 1, true)[1])
0

Do you have an example of a buffer where vim.api.nvim_buf_get_lines() returns {}?

andrewmustea commented 1 year ago

Yes. I think it has to do something with how I lazy load the plugin but here's what happens when I open a file:

Error detected while processing /home/andrew/.config/nvim/plugin/packer_compiled.lua:
E5113: Error while calling lua chunk: /home/andrew/.config/nvim/plugin/packer_compiled.lua:931: /home/andrew/.config/nvim/plugin/packer_compiled.lua: Vim(echomsg):E15: Invalid expression: \\"..BufReadPost Autocommands for \\"*\\": Vim(lua):E5108: Error executing lua ...te/pack/packer/opt/indent-o-matic/lua/indent-o-matic.lua:172: attempt to get length of local 'line' (a nil value)
stack traceback:
        [C]: in function 'cmd'
        ...m/site/pack/packer/start/packer.nvim/lua/packer/load.lua:179: in function <...m/site/pack/packer/start/packer.nvim/lua/packer/load.lua:175>
        [string \":lua\"]:1: in main chunk
        [C]: at 0x561a567efa91
        /home/andrew/.config/nvim/plugin/packer_compiled.lua:921: in function </home/andrew/.config/nvim/plugin/packer_compiled.lua:10>
        [C]: in function 'pcall'
        /home/andrew/.config/nvim/plugin/packer_compiled.lua:10: in main chunk" | echom "Please check your config for correctness" | echohl None
stack traceback:
        [C]: in function 'nvim_command'
        /home/andrew/.config/nvim/plugin/packer_compiled.lua:931: in main chunk
andrewmustea commented 1 year ago

I added print(#vim.api.nvim_buf_get_lines(0,0,1,true)) right before line 172 and got 0.

Darazaki commented 1 year ago

Got it I'll merge this PR. Thanks a lot :)