LuaLS / LuaLS/lua-language-server

[Feature request]: Add ability to define enums without setting variable

Open
#1,932 14 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

enhancement
Dominant language
Lua
Stars
4.4k
Forks
442
PR merge metrics
No merged PRs in 30d

Description

### How are you using the lua-language-server?

NeoVim

### Which OS are you using?

Linux

### What is the issue affecting?

Annotations

### Expected Behaviour

Enum fields are defined and shown when hovering over the variable they are assigned to.

### Actual Behaviour

Not all enum fields are picked up. The fields that are picked up are displayed as `unknown`.

### Reproduction steps

Not needed for a feature request.

### Additional Notes

When using Neovim, the standard library that comes with it is defined in files which use `any` a lot. An issue that I have ran into when trying to define an `enum` for a `table` that looks like the following is that it just won't work.

Table I am trying to define (this is what is show when hovering):

```lua
vim.log.levels: {
TRACE: integer = 0,
DEBUG: integer = 1,
INFO: integer = 2,
WARN: integer = 3,
ERROR: integer = 4,
OFF: integer = 5,
}
```

I have tried a couple of ways to define an enum to cover this:

```lua
-- Method 1
---@enum LogLevels
---| '0'
---| '1'
---| '2'
---| '3'
---| '4'
---| '5'

-- Method 2
---@enum LogLevels
---| TRACE = '0'
---| DEBUG = '1'
---| INFO = '2'
---| WARN = '3'
---| ERROR = '4'
---| OFF = '5'

-- Method 3
---@enum LogLevels
---| TRACE: 0
---| DEBUG: 1
---| INFO: 2
---| WARN: 3
---| ERROR: 4
---| OFF: 5

-- Method 4
---@enum LogLevels
---| TRACE: integer = 0 # Trace level
---| DEBUG: integer = 1 # Debug level
---| INFO: integer = 2 # Info level
---| WARN: integer = 3 # Warn level
---| ERROR: integer = 4 # Error level
---| OFF: integer = 5 # Off level
```

And then, when trying to set the variable to the `enum` `vim.log.levels`, I have done a couple of things as well:

```lua
-- Method 1
---@type LogLevels
M.levels = vim.log.levels --[=[@as LogLevels]=]

-- Method 2
---@type LogLevels
local tmp_levels = vim.log.levels --[=[@as LogLevels]=]
---@type LogLevels
M.levels = tmp_levels
```

When doing any of these, if I hover over `M.levels` only 3 of the fields are shown:

```lua
(field) M.levels: LogLevels {
ERROR: unknown,
INFO: unknown,
WARN: unknown,
}
```

I am aware that the following can be done to do something similar.
However, I would like to be able to add descriptions to fields and I would like to not see a table literal in the inlayhints.
Also, this won't work as a parameter type for a function because it would expect a table literal instead of an enum field.

This works for type declaration:

```lua
---@alias LogLevels { TRACE: 0, DEBUG: 1, INFO: 2, WARN: 3, ERROR: 4, OFF: 5 }

---@type LogLevels
M.levels = vim.log.levels
```

### Log File

Not needed for a feature request.

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start by reproducing the enum declaration and assignment examples in a Neovim/Lua-language-server setup, then trace how @enum annotations, type assignment, and hover output are handled. Done means enums can be declared without assigning a variable, retain named fields and descriptions, and work as the type of vim.log.levels and function parameters.

Written by the indexing model from the issue text.

Assessment

Tech stack
lua
Domain
devtools
Issue type
Feature
Difficulty
4/5
Estimated time
3-5 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
45/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.