Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

key map value of enum #2914

Open
renshengongji opened this issue Oct 26, 2024 · 1 comment
Open

key map value of enum #2914

renshengongji opened this issue Oct 26, 2024 · 1 comment
Labels
feat/LuaCats Annotations Related to Lua Language Server Annotations (LuaCats)

Comments

@renshengongji
Copy link

renshengongji commented Oct 26, 2024

Issue Description

I want to try creating an Event enum to use for the registering Event method, but enum doesn't seem to support key table values
image
image
If it can be completed normally like this
but
image
If you define a variable or parameter as an enumeration, lls will not know what value it has
What I want is to use extend like class, like --- @ class Event: {functionName: string,...}
but enum does not seem to support extend

Additional Notes

No response

@renshengongji renshengongji added the documentation Has to do with documentation either in the wiki or in the repo label Oct 26, 2024
@tomlau10
Copy link
Contributor

@enum actually works like an @alias, which it is just a type of union values: https://luals.github.io/wiki/annotations/#enum

T = {
    a = 1,
    b = 2,
}

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

Currently @enum only support value types (string / number / boolean) but not reference type (table / function).
(I don't know if this is the accurate way to describe them)

And when you use (literal) table as enum values, luals seems only recognize them as a generic table, without knowing its content
=> thus you have no completion inside the function with @param event Event, because luals only treat Event as @alias Event table|table (?)


Workaround

I think I have found a workaround to attach the class type to it:

---@class Event
---@field functionName string
---@field allowCancel boolean

---@diagnostic disable-next-line: duplicate-doc-alias
---@enum Event
Event = {
    RENDER = { functionName = "on_render", allowCancel = false },
    MOTION = { functionName = "on_motion", allowCancel = true },
}

---@param event Event
function registerEvent(event)
    event.  --< will suggest: allowCancel, functionName
end

This works by declaring Event type as a class as well. 🤔
However I don't know if it would cause any other side effect

@carsakiller carsakiller removed their assignment Oct 27, 2024
@carsakiller carsakiller added feat/LuaCats Annotations Related to Lua Language Server Annotations (LuaCats) and removed documentation Has to do with documentation either in the wiki or in the repo labels Oct 27, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
feat/LuaCats Annotations Related to Lua Language Server Annotations (LuaCats)
Projects
None yet
Development

No branches or pull requests

3 participants