LuaLS / lua-language-server

A language server that offers Lua language support - programmed in Lua
https://luals.github.io
MIT License
3.36k stars 319 forks source link

Request: Support `@field` in both enum and table definitions. #2960

Open rhys-vdw opened 3 hours ago

rhys-vdw commented 3 hours ago

Request: Allow adding a field type to tables and enum definitions using the @field annotation.

This is useful for generating meta files from C code where there is no Lua table literal to copy.

See current behaviour below:

@class

✅ Using @field

---@class Foo
---@field myField 5
Foo = {}

✅ Using literal code

---@class Foo
Foo = {
  myField = 5,
}

✅ Using type annotation

---@class Foo
Foo = {
  ---@type 5
  myField = nil,
}

@enum

❌ Using field

---@enum Foo
---@field myField 5
Foo = {}

Error: The field must be defined after the class.Lua Diagnostics.(doc-field-no-class)

✅ Using literal code

---@enum Foo
Foo = {
  myField = 5,
}

✅ Using type annotation

---@enum Foo
Foo = {
  ---@type 5
  myField = nil,
}

Untyped table

❌ Using field

---@field myField 5
Foo = {}

Error: The field must be defined after the class.Lua Diagnostics.(doc-field-no-class)

✅ Using literal code

Foo = {
  myField = 5,
}

✅ Using type annotation

Foo = {
  ---@type 5
  myField = nil,
}
CppCXY commented 2 hours ago

Why do you want to write an extra annotation? Isn't it better to just use field = 5?

rhys-vdw commented 2 hours ago

Why do you want to write an extra annotation? Isn't it better to just use field = 5?

I am just trying to make the process of exporting from C a bit easier.

An example:

https://github.com/beyond-all-reason/spring/blob/38598d14b900a9790d3e23818bf18593993249a1/rts/Lua/LuaConstCMD.cpp#L18-L82

It would be convenient to be able to just copy this out into a meta.lua file directly.

That said, I can generate Lua too, and maybe it's more appropriate.

CppCXY commented 2 hours ago

I suggest you use AI to convert it directly: test

rhys-vdw commented 2 hours ago

I don't want to run AI in CI, I will just write a program to do it.

Thanks for the reply.

It's okay to close this issue if you don't like it. :-)

tomlau10 commented 36 minutes ago

In LuaLS @enum actually works like an @alias, which it is just a type of union values: https://luals.github.io/wiki/annotations/#enum, as described in here: https://github.com/LuaLS/lua-language-server/issues/2914#issuecomment-2439798833, https://github.com/LuaLS/lua-language-server/issues/2820#issuecomment-2310034789

---@enum T
T = {
    a = 1,
    b = 2,
}

--> the same as
---@alias T 1|2

So the type in @enum is just union of values, not a class container. When you specify an enum type in function param, the union values are checked against, instead of checking the object type. In this sense, @field seems just not fit in the concept of @enum in LuaLS. 🤔


With that said, there is another feature request to Add ability to define an enum without using a table: https://github.com/LuaLS/lua-language-server/issues/2721, maybe it suits you better for this case and you would like to +1 to it. 😄

rhys-vdw commented 4 minutes ago

@tomlau10 thanks for the detailed response! However I don't believe either of these notations are sufficient. e.g.

---@enum MyBool
MyBool {
  ---This is true.
  myTrue = 1,

  ---This is not true.
  myFalse = 0
}

Where do I define the name and comment for myTrue and myFalse?

---@alias MyBool 1|0

But it's okay, I think it's fine to define a table. I have actually coded up a generator: https://github.com/rhys-vdw/lua-doc-extractor