Skip to content

Conversation

@bfredl
Copy link
Member

@bfredl bfredl commented Oct 27, 2019

Add a direct implementation of calling a vimL function from lua.

This fixes some typing issues due to the indirect conversion via the API. float values are preserved as such, fixes #9389, as well as empty dicts/arrays (ideally these should be distinguishable by a metatable, which is the canonical way of creating table "subtypes" and far less intrusive than {[true] = 7, [false] = 3} thing). I might revisit this and other typing issues (such as a dedicated vim.NIL value as nil doesn't always work) in a following vim.rpcrequest PR (which is my next lua addition, for a full "rpc client lib" in lua code).

Also add convenient lua interface as vim.funcs.fname(...). The name funcs is fully bikeshedable. @norcalli has also suggested a similar interface as part of his "stdlib" work but instead nvim.fn.xx, though it would be vim.fn here if we use it. In fact perhaps this should be the only public interface, and vim.call instead be vim._call implementation detail.

@justinmk
Copy link
Member

nice! +1 for vim.fn . ref: https://github.com/norcalli/nvim.lua

In fact perhaps this should be the only public interface, and vim.call instead be vim._call implementation detail.

Seems reasonable. We could also make vim.fn() callable I believe, so it can behave like a table and a function. I'm not sure if that's "too cute" or if it might be an interesting pattern to follow for various interfaces...

@bfredl
Copy link
Member Author

bfredl commented Oct 27, 2019

We could also make vim.fn() callable I believe, so it can behave like a table and a function. I'm not sure if that's "too cute" or if it might be an interesting pattern to follow for various interfaces...

That a bit too magical maybe, unless it serves some specific important usecase. If we want a function I would just leave vim.call with a public name, as it is implemented already.

@bfredl bfredl force-pushed the luacall branch 2 times, most recently from 058df7f to 1e9b374 Compare October 27, 2019 14:38
@bfredl bfredl changed the title lua: add vim.funcs.{fn} for direct access to vimL function lua: add vim.fn.{func} for direct access to vimL function Oct 27, 2019
compared to vim.api.|nvim_call_function|, this fixes some typing issues
due to the indirect conversion via the API. float values are preserved
as such (fixes neovim#9389) as well as empty dicts/arrays.

Ref https://github.com/norcalli/nvim.lua for the call syntax
@bfredl bfredl merged commit e085cac into neovim:master Oct 27, 2019
@bfredl
Copy link
Member Author

bfredl commented Oct 27, 2019

I kept vim.call, I don't think it hurts much to have it public.


Note: unlike vim.api.|nvim_call_function| this converts values directly
between vimL values and lua values. If the vimL function returns a
float, it will be representeted directly as a lua number. Both empty
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Typo here with representeted

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

will fix it in next PR (if doc PR doesn't do it first :)

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

fixed in #11277

@justinmk justinmk added the lua stdlib label Oct 27, 2019
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

lua stdlib

Projects

None yet

Development

Successfully merging this pull request may close these issues.

nvim_call_function returns weird array in lua

3 participants