Skip to content

Added LuaLS generator#188

Open
darltrash wants to merge 19 commits intobjornbytes:devfrom
darltrash:patch-1
Open

Added LuaLS generator#188
darltrash wants to merge 19 commits intobjornbytes:devfrom
darltrash:patch-1

Conversation

@darltrash
Copy link

Decided to create a LuaLS annotation generator since the original one was only generating LuaCATS which is not compatible or used that much anymore.

This should be a great improvement for those using the LuaLS LSP, very common amongst most IDEs and code editors :)

Decided to create a LuaLS annotation generator since the original one was only generating LuaCATS which is not compatible or used that much anymore.

This should be a great improvement for those using the LuaLS LSP, very common amongst most IDEs and code editors :)
@bjornbytes
Copy link
Owner

Hmmm yeah, the generated definitions look really similar to the cats ones, just with slightly different formatting.

Some differences I noticed:

  • LuaLS includes related, LuaCATS doesn't
  • LuaCATS includes notes, LuaLS doesn't
  • LuaLS uses @overload for variants, LuaCATS adds the function multiple times.
  • LuaLS does @param p T?, LuaCATS does @param p? T for optional stuff
  • LuaLS has links to the docs pages, LuaCATS doesn't
  • Enum tables are formatted differently
  • Looks like LuaLS formats the argument/return lists slightly differently (in a table maybe?)

This is the definition syntax you're using right? https://luals.github.io/wiki/annotations/

It's the LuaLS definitions but it also says it's LuaCATS, so I'm confused. If they're the same thing, then this is just a different style of structuring the definitions, which is fine. But we should definitely pick one of the generators, or combine them somehow, instead of having 2 of them.

@darltrash
Copy link
Author

darltrash commented Dec 30, 2025

Oh god, I also got confused.

I had written a whole reply here, heck, I wrote a whole generator and tested it out myself on my own programs and the examples in the repository, all because apparently CATS was different from Sumneko's LuaLS annotation syntax, just to then realize that I was wrong about this, and that CATS and the LuaLS syntax seem to be one thing!

I am afraid I wasted everyone's time here.

We have two options now, fix up the existing CATS generator, or use my new one, though I see value on fixing what's already there instead of just rewriting the whole thing as I already did. I guess the problem all along was not that it was operating under a different syntax altogether but simply that the generator was not using all of the features or written for the new syntax

@darltrash
Copy link
Author

Might get onto fixing the existing generator, right now.

@darltrash
Copy link
Author

imagen The type syntax actually does seem to be quite different, though... Most of the annotations show warnings and errors like these in LuaLS

@bjornbytes
Copy link
Owner

Hm yeah, the {number} and number? types are a recent addition, it's possible I haven't updated the cats generator for those yet.

@darltrash
Copy link
Author

Hm yeah, the {number} and number? types are a recent addition, it's possible I haven't updated the cats generator for those yet.

Seems likely, lots of the stuff here seems like it was made for a different version of the same syntax, nowadays, a more "standarized" syntax is used, which is number[]

@bjornbytes
Copy link
Owner

To be clear {number} is just a random convention used in lovr's docs, either way it'll need to be converted to number[] for LuaLS/LuaCATS/TypeScript. It does happen to match Luau's syntax for tables, but that's just an accident.

@darltrash
Copy link
Author

darltrash commented Dec 30, 2025

To be clear {number} is just a random convention used in lovr's docs, either way it'll need to be converted to number[] for LuaLS/LuaCATS/TypeScript. It does happen to match Luau's syntax for tables, but that's just an accident.

Is that necessary? We could just translate in the generator, instead of having to attach to a specific format, which I feel would be the best course of action simply because CATS isn't the only "target" the generators have

@bjornbytes
Copy link
Owner

Yeah, sorry that's what I meant. The cats generators will have to map {number} to number[], I like using {number} in the docs pages.

@darltrash
Copy link
Author

Are there any decisions so far? I want to know whether it'd be preferred to update the existing generator or to replace it with the one i just made. I see positives to both outcomes, and I am willing to help iron out the kinks in the new generator, as well as just add the missing features to the original one.

@bjornbytes
Copy link
Owner

I don't have a strong preference, whatever's easiest and looks best in editors. I do like a few aspects of the old definitions

  • The files are named module.lua instead of lovr.module.lua (minor).
  • It might be nice to keep the "Notes" section, if it's not too cluttered?

@darltrash
Copy link
Author

I don't have a strong preference, whatever's easiest and looks best in editors. I do like a few aspects of the old definitions

* The files are named `module.lua` instead of `lovr.module.lua` (minor).

* It might be nice to keep the "Notes" section, if it's not too cluttered?

It is VERY cluttered, but I think it's okay that way, this is meant to be consumed mostly by the LSP, and it is still quite readable! Added both suggestions, plus the globals file. (I sincerely do not know why I didn't do it that way before!)

Everything should work as expected, it essentially looks like this:

imagen

The main drawback here though is that callbacks are extremely verbose, and must all be defined riiiight next to eachother due to some stupid limitation of how this thing handles the ---@field marker.

Something I am coming up with though, I think it would make sense to actually define the function types and the actual "fields" as separate things, so we can have the TYPE of the functions have the annotations, while the fields themselves are all tightly packed and put together

@darltrash
Copy link
Author

darltrash commented Dec 30, 2025

imagen

oh my god what in the heck is this 😭

I dont think I could parse this

@darltrash
Copy link
Author

Actually, I have an idea for it, but I'll have to think about it proper

@bjornbytes
Copy link
Owner

We could probably restructure that one.

{{ name: string, type: DataType | table, offset: number, length: number?, stride: number? }}

Or it could just be table and documented in the notes.

@darltrash
Copy link
Author

We could probably restructure that one.

{{ name: string, type: DataType | table, offset: number, length: number?, stride: number? }}

Or it could just be table and documented in the notes.

No need! I just implemented it in the latest commit, I think the syntax is good since it just has to make sense for the user.

@darltrash
Copy link
Author

imagen

@darltrash
Copy link
Author

imagen

@darltrash
Copy link
Author

I think it is ready, I do not have any more meaningful changes to make, perhaps just code cleanups and optimization, but it does seem to work correctly for most of the cases in the Lövr API, I think feedback would be awesome to make this better, I'm open to any suggestions and/or changes.

@bjornbytes
Copy link
Owner

  • Should this PR delete the old generator, if it's intended to replace it?
  • Is cats or luals a better name for the generator?

@bjornbytes
Copy link
Owner

Could you use 2 space indentation and single quotes, to match the rest of the code in the repository?

-- Plus, they often have their own, separately mantained annotations instead.
if module.external then
print("SKIP")
goto continue
Copy link
Owner

Choose a reason for hiding this comment

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

It would be nice to remain compatible with Lua 5.1 and Luau, which don't support goto.

Copy link
Author

Choose a reason for hiding this comment

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

done :)

end

-- Assumes that an object with :sub, also has :__sub, for example
local function writeOperator(func, f)
Copy link
Owner

Choose a reason for hiding this comment

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

Note that add/sub/mul/div methods are going away, might need another way to detect metamethods eventually.

Copy link
Author

Choose a reason for hiding this comment

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

I think it would be awesome to simply define those metamethods as functions that start with __, and mark them as metamethods. or maybe have a special field within each object for metamethods specifically, which could help for the website UI to describe them as operator overloads

@darltrash
Copy link
Author

darltrash commented Dec 31, 2025

  • Should this PR delete the old generator, if it's intended to replace it?

    • Is cats or luals a better name for the generator?

I believe CATS might be a better name, but just judging by how confused we both were by the naming scheme, I am not exactly sure.

This generator does supersede the existing CATS generator, as it pretty much does the same thing but with an updated syntax which allows us to do more complex type checking, documentation, etc.

@darltrash
Copy link
Author

Could you use 2 space indentation and single quotes, to match the rest of the code in the repository?

Done, as well as change it to be compatible with Lua 5.1 and Luau.

@bjornbytes
Copy link
Owner

Ok sorry I finally got a chance to try this out. Here are the things I'd change:

  • The NOTES thing is very loud, I like the #### Notes: compared to the new ## NOTES header.
  • The @return values don't have names anymore.

Also, I think last time we were talking we decided to keep cats as the final name for the generator. If that seems right, this PR should probably replace the existing cats generator with this new one.

Happy to take care of any of this post-merge if you don't want to, just let me know

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

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants