sol2 icon indicating copy to clipboard operation
sol2 copied to clipboard

Idea: generating annotations

Open hfn92 opened this issue 2 years ago • 1 comments

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.

hfn92 avatar Oct 08 '23 11:10 hfn92

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.

deadlocklogic avatar Nov 20 '23 14:11 deadlocklogic