New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
[WIP] Lua helpers for plugin development #8677
Conversation
Definitely in favor of this, thanks for getting it started. I would suggest not to think of it as "plugin help", but rather "augmenting the Nvim stdlib". Up to now the stdlib has been defined by We want to enhance the Nvim stdlib with Lua functions. These functions are implemented in Lua, but available also from VimL.
One weakness of Lua is the lack of canonical "best practices" to draw from. I think https://github.com/leafo/moonscript and https://github.com/leafo/lapis might be good sources for that. |
Ok, so I'll try to do this with 1-2 functions I found I needed, and we'll see how this works out. About |
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
After a good night's sleep I was able to solve this, but I'm not sure that's the way it should be done. Seems like I can't use |
b8c4666
to
0a057a4
Compare
With the patient help of bfredl I was able to make it work! Now there's a test to check if all functions in There's snag though: The code isn't loaded automatically. Bfredl suggested putting a file in Suggestions? Next step would be to write documentation (I'd say (e) My confusion above stemmed from the fact that I confused |
Startup time difference could be measured :) Otherwise |
I tried to make Otoh, all the other |
I've started bugging people about what they'd like to see in a "lua stdlib". @tjdevries, @nhooyr, @keidax I think you've dabbled in lua code for nvim, care to chime in? Thanks! |
Thanks for calling me in. They mostly drill down to those: For table management:For string management:For functional-ish style:
For debbuging (now this is somewhat biased and optional though):Even though there may be more, specially on string management, I feel those are the bits I need to stop thinking about the plugin and start thinking about the language. It'd be great if some of those could be provided. Also, I'd like to add that it feels great to use lua to develop plugins for neovim except for when I need to expose lua to VimL. Somethings like mapping a lua function or exposing some commands can be tricky/messy and sometimes require some indirection to work properly. I would love if the API could expose functions to define mappings and/or create commands in a way that those could natively access lua functions/tables. Obviously I understand the complexity of those, but asking wouldn't hurt, right? Sorry if this got too dense. I'm glad to help. Cheers, |
I second For mappings and commands, we already have API functions to inspect them, it seems logical to also have API functions for adding mappings and commands (this will also benefit To natively support lua references in API would require a bit work, but a quicker workaround could be to have a lua-side wrapper function with the same signature as the API functions, but also accepts lua closures and converts them to the vimL lua commands accessing them from a reference table (as in the linked examples of indirection). |
Taking the indirection out of plugins scope can greatly reduce the cost of writing lua plugins. Even if such mapping is still required, it is acceptable and could be further optimized without requiring to migrate the plugins. |
This comment has been minimized.
This comment has been minimized.
We definitely want |
Seconded what @hkupty has said. I made some helper functions while working on the LSP branch: Not exactly "complete" but should give an idea of a few of the things it would be helpful to have when writing lua plugins. |
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
Alternatively, we could also add some native lua without having to do things to the vim api.
|
Not sure I like monkeypatching builtin metatables: it might then look a piece of code only uses builtin methods, but then one runs it outside of nvim and it unexpectedly fails.
For lua util functions we should also consider the standard module pattern of |
The name is less important than the structure I'd say... I think the intent is to make sure all those functions were provided by neovim, thus are accessible from the We could have them as I just want to explicitly separate those 4 cases. A flat structure would be:
|
As far as I can see, the current api is useable directly from lua, what do you mean by "native wrapper"? Or am I missing something? |
I've now added inspect. License is included in the file, which should be enough. Is it enough to refer to the github site for documentation? Personally, I wouldn't want to blow up the doc file with all the possible options and details, since most of the time it simply "does what you want". |
I also added trim and deepcopy. I'd say for a first shot, |
After some iteration over at tycho, we settled for a simple design for I've removed the dependency on the extmarks PR by providing namespaces in the lua code, but in a maximally trivial way. |
Just pinging to check if this is still alive. I've been on a rush and couldn't be closer to this change. |
It's somewhat on hiatus, since I don't have much time these days. I'll pick it up later, or if somebody else wants to take over, be my guest :) |
3f1e815
to
e1d4804
Compare
Used to proved `map`. [ci skip]
e1d4804
to
87452ad
Compare
If we want people to write lua plugins, we should give them some tools to ease development.
A somewhat loose to-do list, just some things I need to remember:
Add
v:lua
toeval.txt
, reference toif_lua.txt
for the individial functionsNeed help:
v:lua
workvim.helpers
for plain lua help function likesplit
, andvim.lua_api.osname
for the functionosname
that gets exported to viml).to actually make
v:lua
work[ci skip]