[vim/vim] vim.list({}) index starts with 0 while neovim and lua index starts with 1 (#6342)

18 views
Skip to first unread message

Prabir Shrestha

unread,
Jun 25, 2020, 11:33:10 PM6/25/20
to vim/vim, Subscribed

I opened an issue in neovim to have compatibility with vim and neovim on the apis related to neovim. neovim/neovim#12537.

Does any one know why the vim.list in lua starts with 0 instead of 1? Arrays in lua starts with 1 and neovim seems to default to 1.
This has been one of the source of bugs in lua plugins in vim and neovim. Example of bugs:

  1. natebosch/vim-lsc@97fee80. vim-lsp also uses the same diff algorithm so updating should be simple. prabirshrestha/vim-lsp#821

Add support for neovim
There is a difference between vim.eval and vim.api.nvim_eval when
passed a list. With vim.eval the collection is zero indexed, with
vim.api.nvim_eval it is one indexed. Pass an offset of 1 to correct
for this by evaluating has("nvim") as a third argument.

  1. prabirshrestha/asyncomplete.vim#101

Problem
First candidate is truncated in vim when smart completion is enabled.
Cause
According to document of vim, the index of list starts from 0. However, according to the document of neovim, the index of table starts from 1.
Solution
Pass the start and end indexes from vim script to lua script.

Lua:

array = [1,2,3] 
print(array[1])

Neovim:

let s:array = [1,2,3] 
lua print(vim.api.nvim_eval('s:array')[1])

Vim:

let s:array = [1,2,3] 
lua print(vim.eval('s:array')[1])

This is the output and vim seems to be an outlier here.

Lua Neovim Vim
1 1 2

My thought is that we make the list index at 1 and make it a breaking change given that there are almost no one using lua plugins currently and only few plugins need to update their code. If the index is starting at 0 instead of 1 it won't play well with the entire lua ecosystem since everyone will assume it starts with 1 except vim.

One could do something like this if they really wanted to support old version of vim.

let s:array = [1,2,3] 
lua <<EOF
let offset = vim.fn.has('patch-8.2.x') == 1 then -1  else 0 end
print(vim.eval('s:array')[1 + offset])
EOF

As for me I would personally disallow old version of vim without this patch to run.

if !has('patch-8.2.x') | finish | endif

Thoughts?


You are receiving this because you are subscribed to this thread.
Reply to this email directly, view it on GitHub, or unsubscribe.

Christian Brabandt

unread,
Jun 26, 2020, 3:30:58 AM6/26/20
to vim/vim, Subscribed

I would think it starts at 0 as this is consistent with vimscript api.

Prabir Shrestha

unread,
Jun 26, 2020, 2:47:41 PM6/26/20
to vim/vim, Subscribed

The problem with starting with 0 is that non of the existing lua libraries or functions that works in lua ecosystem will work in vim. There is an entire luarocks ecosystem I would like to take advantage of https://luarocks.org/.

Here is a simple example. This print 1 2 3 in neovim and pure lua if local array = {1,2,3} but throws error in vim.

let s:array = [1,2,3]

lua
 <<EOF
local array = vim.eval('s:array')
--local array = {1, 2, 3}

local function concat(arr)
     local text = ''
     for i=1, #arr do
         text = text .. ' ' .. arr[i]
     end
     return text
end

print(concat(array))
EOF

So the proposal is that if a vim list is accessed in vimscript index starts at 0 if it is accessed in lua it starts with 1. This only has impact on devs writing in lua.

Bram Moolenaar

unread,
Jun 26, 2020, 3:17:38 PM6/26/20
to vim/vim, Subscribed

Most language use index zero for the first element of a list or array. If Lua does something different, better make sure that only happens when actually writing Lua. I suppose that means when converting a Vim list to Lua you just use the right Lua type for it and it should just work.

Prabir Shrestha

unread,
Jun 26, 2020, 3:49:22 PM6/26/20
to vim/vim, Subscribed

Sent a PR for this. #6347.

vim-dev ML

unread,
Jun 26, 2020, 3:54:56 PM6/26/20
to vim/vim, vim-dev ML, Your activity

Hi,

On Fri, Jun 26, 2020 at 12:49 PM Prabir Shrestha <vim-dev...@256bit.org>
wrote:

> Sent a PR for this. #6347 <https://github.com/vim/vim/pull/6347>.
>
>
>
As you are working on enhancing the Vim Lua interface, it will be useful to
improve the
test coverage for if_lua.c by adding some more tests:

https://codecov.io/gh/vim/vim/src/5d3c9f8c2a0fc29ba4ac8e0f052378b64d9e3dd3/src/if_lua.c


Thanks,
Yegappan

Yegappan Lakshmanan

unread,
Jun 26, 2020, 3:55:20 PM6/26/20
to vim_dev, reply+ACY5DGBV4SO7NJKVJS...@reply.github.com, vim/vim, Subscribed
Hi,

On Fri, Jun 26, 2020 at 12:49 PM Prabir Shrestha <vim-dev...@256bit.org> wrote:

Sent a PR for this. #6347.



Prabir Shrestha

unread,
Jun 26, 2020, 4:10:06 PM6/26/20
to vim/vim, vim-dev ML, Comment

Sounds good. Currently focusing more on compatibility with lua and neovim (neovim/neovim#12537) and making sure we have the right apis exposed to make lua a viable alternative for plugin authors. But as I add new features I'm trying to add much tests as possible.


You are receiving this because you commented.

Prabir Shrestha

unread,
Jun 27, 2020, 1:21:24 PM6/27/20
to vim/vim, vim-dev ML, Comment

Closing since it has the PR has been merged.


You are receiving this because you commented.

Prabir Shrestha

unread,
Jun 27, 2020, 1:21:26 PM6/27/20
to vim/vim, vim-dev ML, Comment

Closed #6342.


You are receiving this because you commented.

Reply all
Reply to author
Forward
0 new messages