[vim/vim] add vim.has() for lua (#6056)

15 views
Skip to first unread message

Prabir Shrestha

unread,
May 10, 2020, 2:11:46 PM5/10/20
to vim/vim, Subscribed

Feature detection is a must in order to support backwards compatibility. Was writing a plugin in lua and noticed that it is currently not possible to support it without eval.

lua print(vim.has('perl'))
lua print(vim.has('perl', 1))

Signature is the same as vim's has() function.


You can view, comment on, or merge this pull request online at:

  https://github.com/vim/vim/pull/6056

Commit Summary

  • add vim.has() for lua

File Changes

Patch Links:


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

Dominique Pellé

unread,
May 10, 2020, 2:34:37 PM5/10/20
to vim/vim, Subscribed

Thanks. How about also updating the tests in src/testdir/test_lua.vim?

Prabir Shrestha

unread,
May 10, 2020, 2:56:45 PM5/10/20
to vim/vim, Push

@prabirshrestha pushed 1 commit.


You are receiving this because you are subscribed to this thread.

View it on GitHub or unsubscribe.

Prabir Shrestha

unread,
May 10, 2020, 2:57:28 PM5/10/20
to vim/vim, Subscribed

Added tests. How do I only run that file or function for test.

Yegappan Lakshmanan

unread,
May 10, 2020, 3:02:20 PM5/10/20
to vim_dev, reply+ACY5DGETTB2PKH3EDJ...@reply.github.com, vim/vim, Subscribed
Hi,

On Sun, May 10, 2020 at 11:57 AM Prabir Shrestha <vim-dev...@256bit.org> wrote:

Added tests. How do I only run that file or function for test.



In your local view, you can run the following command to run all the tests
in the test_lua.vim file (for a Linux/Unix/MacOS system):

      $ cd testdir
      $ make test_lua

 To run only the Test_lua_has() test function:

      $ cd testdir
      $ ../vim -u NONE -S runtest.vim test_lua.vim Test_lua_has

- Yegappan

vim-dev ML

unread,
May 10, 2020, 3:02:38 PM5/10/20
to vim/vim, vim-dev ML, Your activity

Prabir Shrestha

unread,
May 10, 2020, 3:11:09 PM5/10/20
to vim/vim, vim-dev ML, Comment

Getting test failures since luaeval seems to auto-convert integer to float. Now that I think of it it might be better to use boolean in lua side instead of number.

1 FAILED:
Found errors in Test_lua_has():
function RunTheTest[39]..Test_lua_has line 1: Expected 0 but got 0.0
function RunTheTest[39]..Test_lua_has line 2: Expected 1 but got 1.0
function RunTheTest[39]..Test_lua_has line 3: Expected 1 but got 1.0
function RunTheTest[39]..Test_lua_has line 4: Expected 1 but got 1.0
function RunTheTest[39]..Test_lua_has line 5: Expected 0 but got 0.0
function RunTheTest[39]..Test_lua_has line 6: Expected 0 but got 0.0


You are receiving this because you commented.

Prabir Shrestha

unread,
May 10, 2020, 3:22:42 PM5/10/20
to vim/vim, vim-dev ML, Push

@prabirshrestha pushed 1 commit.

  • 156d4ef use boolean for return value


You are receiving this because you are subscribed to this thread.

Prabir Shrestha

unread,
May 10, 2020, 3:24:10 PM5/10/20
to vim/vim, vim-dev ML, Comment

Updated to return boolean for lua instead of number. Fixed the tests.


You are receiving this because you commented.

Bram Moolenaar

unread,
May 10, 2020, 3:27:13 PM5/10/20
to vim/vim, vim-dev ML, Comment

Neovim is doing a lot with Lua, don't they have this?


You are receiving this because you commented.

Prabir Shrestha

unread,
May 10, 2020, 4:11:18 PM5/10/20
to vim/vim, vim-dev ML, Comment

@justinmk Any idea if neovim already include has() api in lua? I coudn't find one.

Neovim has vim.api which is usually prefixed with nvim. I guess that is to avoid conflict with future vim apis but I'm not sure about this. Will let Justin comment on this. Example from docs:

function! CurrentLineInfo()
    lua << EOF
    local linenr = vim.api.nvim_win_get_cursor(0)[1]
    local curline = vim.api.nvim_buf_get_lines(
            0, linenr, linenr + 1, false)[1]
    print(string.format("Current line [%d] has %d bytes",
            linenr, #curline))
EOF
endfunction

I'm planning to add few more functions in lua primarily for use in asyncomplete.vim for perf. Given that it is very small and easy to port goal is to have both vimscript and lua port of the plugin but if it has lua support I want to avoid the plugin to use vimscript at all and viceversa. @brammool @justinmk Would you instead prefer me to send a PR with only doc changes so we can discuss on just the apis and do the actual implementation separately.

One thing to note is that neovim exposes the entire luv bindings. This allows one to even easily create http servers and clients. But for vim this is not possible without eval. I would like to propose something like vim.sleep(1000) so that vim can use internal sleep and neovim can use luv. This would avoid if..else in plugin code.

local luv = require('luv')
luv.sleep(1000)

Primary reason for starting the doc PR is I want to have some high level apis such as timer, jobs, autocmd, augroup, json, complete(), pumvisible() without going through too much jumping between vimscript and lua. Neovim also includes std-lib for lua.

    local timer = vim.loop.new_timer()
    timer:start(1000, 0, vim.schedule_wrap(function()
      vim.api.nvim_command('echomsg "test"')
    end))


You are receiving this because you commented.

Hirokazu Hata

unread,
May 10, 2020, 10:51:14 PM5/10/20
to vim/vim, vim-dev ML, Comment

@prabirshrestha

Any idea if neovim already include has() api in lua? I coudn't find one.

You can call has by lua like this.

:lua vim.api.nvim_call_function("has", {"nvim"})

If you use Nvim-HEAD version( > v0.4.3), you can use vim.fn like this.

:lua print(vim.fn.has("nvim"))

:lua vim.fn will include Nvim v0.5.0.


You are receiving this because you commented.

Prabir Shrestha

unread,
May 10, 2020, 11:41:12 PM5/10/20
to vim/vim, vim-dev ML, Comment

@h-michael That is great. I think it would be good to officially bring vim.fn.* to vim. vim would then also need to support passing functions around. We would first need to implement vim.call which is internally used by vim.fn. https://github.com/neovim/neovim/blob/1407899c32018f1988936adfddc1dede73c559cb/src/nvim/lua/vim.lua#L262-L271

My JavaScript PR support also had similar __vimcall() which would allow this. #5198

@h-michael Do you know how neovim deals with float vs number. I had issues with timer_stop only supporting integer and not float. How does neovim handle this?

@brammool What are your thoughts on this?


You are receiving this because you commented.

Prabir Shrestha

unread,
May 11, 2020, 2:57:17 AM5/11/20
to vim/vim, vim-dev ML, Comment

closing in favor of #6063.


You are receiving this because you commented.

Prabir Shrestha

unread,
May 11, 2020, 2:57:18 AM5/11/20
to vim/vim, vim-dev ML, Comment

Closed #6056.


You are receiving this because you commented.

Reply all
Reply to author
Forward
0 new messages