Skip to content

Add support for generating LuaCATS definitions for the Lua API#3755

Open
kalimag wants to merge 35 commits intoTASEmulators:masterfrom
kalimag:pr/generate-lua-api-definitions
Open

Add support for generating LuaCATS definitions for the Lua API#3755
kalimag wants to merge 35 commits intoTASEmulators:masterfrom
kalimag:pr/generate-lua-api-definitions

Conversation

@kalimag
Copy link
Copy Markdown
Contributor

@kalimag kalimag commented Aug 21, 2023

Adds the ability to generate API definitions in the LuaCATS format to the Lua Console.

LuaCATS is primarily the annotation format of Lua Language Server, which drives the most popular VS Code Lua extension and can be used in other editors with language server support.

LuaCATS is an extension/fork of the annotation format used by EmmyLua, but I'm not sure what the status of the latter is. These definitions might be compatible with EmmyLua, but the only spec of its format I could find is sparse and partially in Chinese. EmmyLua extension now uses LuaCATS annotations, too.

The generated definitions could be further improved, e.g. enumerating valid values for surfaceName parameters, specifying the structure of certain table values, and callback function signatures. But that would require extending the API metadata and I wanted to get some feedback first.

Current output:

Check if completed:

will resolve #3860

@YoshiRulz YoshiRulz self-requested a review August 21, 2023 07:49
@kalimag
Copy link
Copy Markdown
Contributor Author

kalimag commented Aug 21, 2023

Just for example, some issues LuaLS can detect in the bundled scripts using these definitions:

image

image

@kalimag kalimag force-pushed the pr/generate-lua-api-definitions branch from bd55b04 to 72fc938 Compare October 31, 2025 12:06
@YoshiRulz YoshiRulz added Request: Feature/Enhancement For feature requests or possible improvements re: Lua API/scripting Relating to EmuHawk's Lua API (not the Lua Console) Meta Relating to code organisation or to things that aren't code labels Oct 31, 2025
Comment thread src/BizHawk.Client.Common/lua/Documentation/LuaCatsGenerator.cs Outdated
Comment thread src/BizHawk.Client.EmuHawk/tools/Lua/LuaConsole.cs Outdated
@vadosnaprimer
Copy link
Copy Markdown
Contributor

Let's move forward with this one!

I decided to move from notepad++ to vscode for lua coding after 15 years, and ended up making exactly the same thing independently yesterday, I didn't even remember this PR! And here it's done in a much nicer way, so let's have it!

Copy link
Copy Markdown
Member

@YoshiRulz YoshiRulz left a comment

Choose a reason for hiding this comment

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

After fixing malformed examples, works w/ SumnekoLua plugin in IDEA.

Comment thread src/BizHawk.Client.Common/lua/Documentation/LuaCatsGenerator.cs Outdated
Comment thread src/BizHawk.Client.Common/lua/Documentation/LuaCatsGenerator.cs
Comment thread src/BizHawk.Client.Common/lua/Documentation/LuaCatsGenerator.cs Outdated
Comment thread src/BizHawk.Client.EmuHawk/tools/Lua/LuaConsole.cs Outdated
Comment thread src/BizHawk.Client.EmuHawk/tools/Lua/LuaConsole.cs Outdated
Comment thread src/BizHawk.Client.Common/lua/Documentation/LuaCatsGenerator.cs Outdated
Comment thread src/BizHawk.Client.Common/lua/Documentation/LuaCatsGenerator.cs Outdated
Comment thread src/BizHawk.Client.Common/lua/Documentation/LuaCatsGenerator.cs Outdated
@vadosnaprimer vadosnaprimer force-pushed the pr/generate-lua-api-definitions branch from 48bca5b to 246190f Compare March 23, 2026 16:58
@vadosnaprimer

This comment was marked as off-topic.

Comment thread src/BizHawk.Client.EmuHawk/tools/Lua/LuaAutocompleteInstaller.cs Outdated
@vadosnaprimer vadosnaprimer force-pushed the pr/generate-lua-api-definitions branch from 18425a1 to 0678148 Compare March 24, 2026 17:06
@vadosnaprimer vadosnaprimer marked this pull request as ready for review March 24, 2026 17:25
@YoshiRulz YoshiRulz self-requested a review March 26, 2026 11:06
{
// technically any string parameter can be passed a number, but let's just focus on the ones where it's commonly used
// like `gui.text` and `forms.settext` instead of polluting the entire API surface
if (parameter.Name is "message" or "caption" && parameter.ParameterType == typeof(string))
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

We don't want to encourage this. There are enough footguns in calling gui.* already with Lua not having named arguments.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Removing this will likely cause a lot of unnecessary warnings

Copy link
Copy Markdown
Member

@YoshiRulz YoshiRulz Mar 30, 2026

Choose a reason for hiding this comment

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

I think type coercion warnings are good, but then I also consider dynamic languages like Lua to always be inferior to statically-typed languages. Is passing numbers for strings idiomatic in general, or only in concatenation?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Well, number-to-string conversion is specifically a feature in BizHawk's Lua bindings, not a feature of the Lua runtime. That's why LLS warns about it unless the parameter definition allows numbers.

But reading numbers from memory and passing them to gui functions is a pretty common pattern in BizHawk scripts, so I added this special case for those functions to avoid warnings in existing, working scripts. I have a test directory with the bundled Lua scripts and some from the Lua scripts catalog, and removing this code causes 73 additional warnings, 16 of which are in BizHawk's own scripts (for example).

I have no strong opinions on deprecating this behavior, but currently it's valid so the definitions say it's valid.


if (parameter.Member.DeclaringType == typeof(GuiLuaLibrary) && parameter.Name == "surfaceName" && parameter.ParameterType == typeof(string))
{
return "surface";
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

We should introduce new attributes if this grows to any more enums.

Comment thread src/BizHawk.Client.Common/lua/Documentation/LuaCatsGenerator.cs Outdated
Comment thread src/BizHawk.Client.EmuHawk/tools/Lua/LuaAutocompleteInstaller.cs
@YoshiRulz
Copy link
Copy Markdown
Member

YoshiRulz commented Apr 11, 2026

Please rebase.
Do you mind if I add [LuaZeroIndexed] myself? It could be used in the existing TASVideos wiki markup, and those functions' current documentation make no mention of it. edit: fdec9a3

@kalimag kalimag force-pushed the pr/generate-lua-api-definitions branch from d7a355e to aee330b Compare April 12, 2026 22:43
@YoshiRulz YoshiRulz added Waiting on dev consensus and removed Request: Feature/Enhancement For feature requests or possible improvements labels Apr 13, 2026
YoshiRulz added a commit that referenced this pull request Apr 14, 2026
the PR #3755 adds a button to generate these automatically, similar to
the existing TASVideos wiki generation
@kalimag kalimag force-pushed the pr/generate-lua-api-definitions branch from 7bf828c to 06bdc0a Compare April 17, 2026 03:20
Copy link
Copy Markdown
Member

@YoshiRulz YoshiRulz left a comment

Choose a reason for hiding this comment

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

Works great, code LGTM. If I don't return to finish #4688 soon enough then by all means merge both PRs. I think nullable joypad.get would be the most annoying false positive.

@vadosnaprimer
Copy link
Copy Markdown
Contributor

How soon is "soon enough"? I'd like to release within a week.

@SuuperW
Copy link
Copy Markdown
Contributor

SuuperW commented Apr 18, 2026

When I hover over a gui.draw method in VS Code to see the documentation, color is replaced with string|integer|dotnetcolor which I don't think is good. If there's a way for the definition to hint to IDEs that expansion isn't desired I think that'd be good, but as far as I can tell there doesn't seem to be a way to do that.

I can change the settings in the VS Code extension to disable alias expansion at least.

@vadosnaprimer
Copy link
Copy Markdown
Contributor

Maybe making a derived class instead of alias would do what you want, but I personally don't see a problem in it showing the expanded thing.

@kalimag
Copy link
Copy Markdown
Contributor Author

kalimag commented Apr 18, 2026

I can change the settings in the VS Code extension to disable alias expansion at least.

Yeah I don't think the annotations have any control over that. That's up to the IDE and individual users' settings. EmmyLua doesn't do this by default, FWIW.

Maybe making a derived class instead of alias would do what you want

No, that wouldn't work because a class can't inherit a union of types.

@YoshiRulz
Copy link
Copy Markdown
Member

Can you put dotnetcolor first in the union?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Meta Relating to code organisation or to things that aren't code re: Lua API/scripting Relating to EmuHawk's Lua API (not the Lua Console) Waiting on dev consensus

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Output Lua annotations for editors

4 participants