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

[Suggestion] Add ability to define an enum without using a table #2721

Open
mycroftjr opened this issue Jun 19, 2024 · 5 comments
Open

[Suggestion] Add ability to define an enum without using a table #2721

mycroftjr opened this issue Jun 19, 2024 · 5 comments

Comments

@mycroftjr
Copy link

mycroftjr commented Jun 19, 2024

How are you using the lua-language-server?

Visual Studio Code Extension (sumneko.lua)

Which OS are you using?

Windows

What is the issue affecting?

Annotations, Type Checking, Completion

Suggestion

For existing codebases where enums are defined like this:

OPERATING_SYSTEM_WINDOWS = 1
OPERATING_SYSTEM_MAC = 2
KEY_A = 1
KEY_B = 2

it would be nice to be able to define all lines that follow (until the next blank line) as part of an enum without changing anything by introducing a table: i.e.

@enum OPERATING_SYSTEM
OPERATING_SYSTEM_WINDOWS = 1
OPERATING_SYSTEM_MAC = 2

@enum KEY
KEY_A = 1
KEY_B = 2

This would be convenient for type annotating when the values are passed as parameters and what-not. I'm not able to find any existing ways to designate these values as a "group" (or union) that maintains their use as global constants.

@MillhioreBT
Copy link

Good capture, this is something that has also bothered me a lot, and I agree with this new type of annotations

@CppCXY
Copy link
Collaborator

CppCXY commented Jun 20, 2024

Implementing this requirement would be the same as supporting setfenv, which seems unlikely at the moment.

@mycroftjr
Copy link
Author

mycroftjr commented Jun 20, 2024

Implementing this requirement would be the same as supporting setfenv

How so?

@A1steaksa
Copy link

This functionality would be very helpful to me. This is one of the few areas where LuaLS feels like it's missing an obvious feature

@TIMONz1535
Copy link
Contributor

TIMONz1535 commented Jan 22, 2025

I'll duplicate the message from luttje/glua-api-snippets#71 (comment) here so that people can use it

Perhaps the word "enum" is confusing people. Initially, aliases were like an enums to describe the function args, that are usually not real variables (like io.open openmode).

You can also use it to create an enum that does not exist at runtime. For an enum that does exist at runtime, see @enum.

But then enum appeared for working with real tables that you create in your code.

Mark a Lua table as an enum, giving it similar functionality to @alias, only the table is still usable at runtime.

So when you want to make an enumeration of global constants, you need to make an alias. To display variables instead of values, you can use a literal. In fact, you can use any text or code here

---@alias Literal `MyReal.Thing[1]("str")`

it will simply be inserted into the auto-completion.

ACT_INVALID = -1
ACT_RESET = 0
ACT_IDLE = 1
ACT_TRANSITION = 2
ACT_COVER = 3
ACT_COVER_MED = 4
ACT_COVER_LOW = 5
ACT_WALK = 6
...

---@alias ACT
---| `ACT_INVALID`
---| `ACT_RESET`
---| `ACT_IDLE`
---| `ACT_TRANSITION`
---| `ACT_COVER`
---| `ACT_COVER_MED`
---| `ACT_COVER_LOW`
---| `ACT_WALK`
---| `ACT_WALK_AIM`
---|>`ACT_WALK_CROUCH`
---|+`ACT_WALK_CROUCH_AIM`
---| `ACT_RUN`
---| `ACT_RUN_AIM`
...

---@param a ACT
function f(a) end

f(ACT_RUN)

Image

There is also a default modifier |> and an additional modifier |+, but it doesn't seem to affect anything other than the visual marker.

local function parseResume(parent)
local default, additional
if checkToken('symbol', '>', 1) then
nextToken()
default = true
end
if checkToken('symbol', '+', 1) then
nextToken()
additional = true
end
local result = parseTypeUnit(parent)
if result then
result.default = default
result.additional = additional
end
return result
end

local enumDes = (' %s %s'):format(
(enum.default and '->')
or (enum.additional and '+>')
or ' |',
vm.getInfer(enum):view(uri)
)

It is uncomfy that it unfolds to multiple values in the hover display (rip ACT functions). In this regard, I would like to see only a title ACT like with enum's.

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

No branches or pull requests

5 participants