Skip to content

add vim.has() for lua#6056

Closed
prabirshrestha wants to merge 3 commits intovim:masterfrom
prabirshrestha:lua_vim_has
Closed

add vim.has() for lua#6056
prabirshrestha wants to merge 3 commits intovim:masterfrom
prabirshrestha:lua_vim_has

Conversation

@prabirshrestha
Copy link

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.

@dpelle
Copy link
Member

dpelle commented May 10, 2020

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

@prabirshrestha
Copy link
Author

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

@vim-ml
Copy link

vim-ml commented May 10, 2020 via email

@prabirshrestha
Copy link
Author

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

@prabirshrestha
Copy link
Author

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

@brammool
Copy link
Contributor

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

@prabirshrestha
Copy link
Author

@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))

@h-michael
Copy link

h-michael commented May 11, 2020

@prabirshrestha

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

You can call has by lua like this.

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.

EDITED

@prabirshrestha
Copy link
Author

@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?

@prabirshrestha
Copy link
Author

closing in favor of #6063.

@prabirshrestha prabirshrestha deleted the lua_vim_has branch May 11, 2020 06:57
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

5 participants