Adding Lua type annotations for yazi #981
Replies: 3 comments 2 replies
-
Started work on adding types here #986 |
Beta Was this translation helpful? Give feedback.
-
This has been stalled for a while now with no discussion, so I'm hoping to get some going by throwing ideas around. How about an approach where type annotations could be generated from the documentation? I have seen this approach being used in the neovim community in https://github.com/folke/neodev.nvim?tab=readme-ov-file#-features, providing lua_ls type annotation files for neovim's lua apis. My understanding of this workflow is that it uses
|
Beta Was this translation helpful? Give feedback.
-
Most of Yazi's methods and properties are defined in Rust, and there are only a few utility methods in Lua. Unfortunately, it seems there's no reliable way to export types from Rust at the moment. Do you think it's a good idea to maintain the type definitions in a separate file and generate markdown documentation from that? This way, I'd only need to maintain one set of type definitions, which would give me more time to ensure they're kept up to date. |
Beta Was this translation helpful? Give feedback.
-
Introduction
There has previously been discussion on adding to yazi for writing lua code. As a (big) fan of types, I was happy to see that there seems to be significant interest in this!
Benefits
Although I might be preaching to the choir here, I'll state some of the benefits that this can bring:
The targets to accomplish this
Since the lua code in yazi is written both in lua files, as well as generated from rust at runtime, ideally the types would be applied to both:
A proposed solution
LuaCATS type annotations are widely in use in the neovim plugin ecosystem. They are a simple "pseudo type system" that is only in use when developing and has no effect at runtime. In my experience with yazi.nvim, the types are not perfect and don't always work correctly, but they still provide a lot of the benefits of a mature type system.
They work nicely with the lua-language-server which I think is the most popular (perhaps the only?) language server for lua.
https://luals.github.io/wiki/annotations/
Technically, I believe it's possible to publish Lua Language Server addons (https://github.com/LuaLS/LLS-Addons) that are totally separate from the lua code. The types can be picked up by the lua language server (according to the addon documentation at https://luals.github.io/wiki/addons/), as there are many examples of this for popular lua libraries in the previous link.
To me that seems like a very good and mature solution. This means the remaining issue is the generation and maintenance of the type annotations.
lua source code files (target 1)
To address lua source code files, let's manually add and maintain LuaCATS type annotations to all lua files in yazi.
It will not be difficult to do this, but it will require some work to maintain. However, because the type system is quite simple and can be applied incrementally, I don't see any issues in doing this.
I think this can implemented as long as it's approved and desired as a feature. I think this is a simple task and can also work on it myself.
lua code generated from rust (target 2)
Currently I don't think any good solution exists for this. The problems that I see are:
I have an idea that I can work on if it is approved:
I already have written a prototype for a rust crate that implements writing LuaLS annotation files. It implements the LuaCATS type system on a structural level, which means
The idea is to improve colocation which will make it much easier to keep the lua types up to date when they change, and when new lua features are added.
schemars
crate. An example of this can be seen here: feat(config): add JSON schema for validation,completion,documentation mfontanini/presenterm#228#[inline]
any extra constructs, although I don't have any experience in doing that at the moment.@sxyazi I would love to hear your thoughts on all of this to see what direction you would like yazi to go.
Beta Was this translation helpful? Give feedback.
All reactions