ThePhD / sol2

Sol3 (sol2 v3.0) - a C++ <-> Lua API wrapper with advanced features and top notch performance - is here, and it's great! Documentation:
http://sol2.rtfd.io/
MIT License
4.06k stars 492 forks source link

Idea: generating annotations #1537

Open hfn92 opened 9 months ago

hfn92 commented 9 months ago

I was wondering if it's possible or reasonable for sol2 to generate lua annotations.

For example when defining a type like this:

  lua.new_usertype<Vec2f>("Vec2f",
                          sol::call_constructor, sol::constructors<Vec2f(), Vec2f(float), Vec2f(float, float)>(),
                          "x", &Vec2f::x,
                          "y", &Vec2f::y);

One could generate a lua file like this:

--- @class _Vec2f
local _Vec2f = {}

--- @type number
_Vec2f.x = 0
--- @type number
_Vec2f.y = 0

--- @return _Vec2f
--- @param x? number
--- @param y? number
function Vec2f(x, y) end

After defining all usertypes one would call some function to generate the annotations lua file, which can be pointed to via the workspace.library field in the .luarc.json

This would allow your custom scripting api to have auto-completion and basic type checking when using lua-language-server for example.

deadlocklogic commented 7 months ago

This is out of sol's scope and requires a similar approach to binding generation. You can however write wrappers around sol registration functions which create a database for each registered usertype/function/variable and populate it with proper metadata: name/type etc... This approach is used in this project: nbind And then use this created database to create you own annotation file. This topic should be treated in a separate fashion IMO.