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:
Add support for neovim
There is a difference betweenvim.evalandvim.api.nvim_evalwhen
passed a list. Withvim.evalthe collection is zero indexed, with
vim.api.nvim_evalit is one indexed. Pass an offset of1to correct
for this by evaluatinghas("nvim")as a third argument.
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.![]()
I would think it starts at 0 as this is consistent with vimscript api.
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.
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.
Sent a PR for this. #6347.
Sent a PR for this. #6347.
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.
Closing since it has the PR has been merged.
—
You are receiving this because you commented.