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

[Req] —-@enum #1255

Closed
FlashHit opened this issue Jun 29, 2022 · 11 comments
Closed

[Req] —-@enum #1255

FlashHit opened this issue Jun 29, 2022 · 11 comments
Labels
enhancement New feature or request feat/LuaCats Annotations Related to Lua Language Server Annotations (LuaCats)
Milestone

Comments

@FlashHit
Copy link

how do i do this?

---@class Realm
local Realm = {
	Realm_Client = 0,
	Realm_Server = 1,
	Realm_ClientAndServer = 2,
	Realm_None = 3,
	Realm_Pipeline = 4,
}

---@param p_Realm Realm?
function Test(p_Realm)
	p_Realm = Realm.Realm_Client
end

grafik

@well-in-that-case
Copy link

well-in-that-case commented Jun 29, 2022

This is happening because you're assigning a parameter — that you defined as Realm type — to a field of Realm that's an integer. Not sure what the purpose of this is, but you can set the type of p_Realm as Realm|integer? to specify Realm or integer as the types. This may be what you want, since Realm_Client looks like a normal integer.

Take a look into the @alias, @cast, and @type tokens while you're at it too. These may help as well.

@FlashHit
Copy link
Author

I could do this:

---@param p_Realm `Realm.Realm_Client` | `Realm.Realm_Server` | `Realm.Realm_ClientAndServer` | `Realm.Realm_None` | `Realm.Realm_Pipeline`

but thats not really a solution

@flrgh
Copy link
Contributor

flrgh commented Jun 29, 2022

Enums are most easily annotated as @alias types. The OpenResty annotations in the repo have a good example:

https://github.com/sumneko/lua-language-server/blob/befc89c83b248aa4dbfd3bac8e744ca9969b1208/meta/3rd/OpenResty/library/ngx.lua#L39-L70

This makes it possible to write functions that take ngx.http.method as a param type:

https://github.com/sumneko/lua-language-server/blob/befc89c83b248aa4dbfd3bac8e744ca9969b1208/meta/3rd/OpenResty/library/ngx.lua#L2258-L2263

If that doesn't work you may need to explain a bit more about what you are trying to accomplish with this Test function.

@FlashHit
Copy link
Author

would be nice if we could use sth like ---@enum Realm instead of ---@class Realm

@FlashHit
Copy link
Author

grafik
well this doesn't look nice. it not optimal imo

@Nexela
Copy link
Contributor

Nexela commented Jun 29, 2022 via email

@FlashHit
Copy link
Author

You can make that an alias and use the alias as the param type

thats what I did

@sumneko
Copy link
Collaborator

sumneko commented Jun 30, 2022

---@enum Realm --> Equivalent to ---@alias Realm Realm.Realm_Client|Realm.Realm_Server|...
local Realm = {
	Realm_Client = 0, --> type: Realm.Realm_Client
	Realm_Server = 1, --> type: Realm.Realm_Server
	Realm_ClientAndServer = 2,
	Realm_None = 3,
	Realm_Pipeline = 4,
}

---@param p_Realm Realm?
function Test(p_Realm)
	p_Realm = Realm.Realm_Client
end

proposal of ---@enum

@sumneko sumneko added enhancement New feature or request feat/LuaCats Annotations Related to Lua Language Server Annotations (LuaCats) labels Jun 30, 2022
@FlashHit
Copy link
Author

Yes please 👍

@FlashHit
Copy link
Author

please add this to the next milestone :)

@sumneko sumneko added this to the 3.5.0 milestone Jun 30, 2022
sumneko added a commit that referenced this issue Jul 17, 2022
diagnostic for enum
@carsakiller
Copy link
Collaborator

When this is ready for testing feel free to @ me and I can work on adding a description for it like I did back in e2b791e

sumneko added a commit that referenced this issue Jul 18, 2022
sumneko added a commit that referenced this issue Jul 18, 2022
carsakiller added a commit to carsakiller/lua-language-server that referenced this issue Jul 18, 2022
Ketho added a commit to Ketho/vscode-wow-api that referenced this issue Jul 31, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request feat/LuaCats Annotations Related to Lua Language Server Annotations (LuaCats)
Projects
None yet
Development

No branches or pull requests

6 participants