LuaLS / LuaLS/lua-language-server
[Feature request]: Add ability to define enums without setting variable
还没有人认领这个 Issue。
- 主要语言
- Lua
- 星标
- 4.4k
- 派生
- 442
- PR 合并指标
- 30 天内没有已合并 PR
描述
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):
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:
-- 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:
-- 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:
(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:
---@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.
贡献指南
从这里开始
- 先读完整个 Issue,再读项目的贡献指南。
- 在 Issue 下留言说明你要接手 —— 这能避免两个人做同样的事。
- Fork 仓库,在一个分支上完成修改。
- 提交 Pull Request,并在描述里引用这个 Issue 编号。
调研方向
首先在 Neovim/Lua-language-server 环境中复现 enum 声明和赋值示例,然后跟踪 @enum 注解、类型赋值和悬停输出的处理方式。完成的标准是:可以在不为变量赋值的情况下声明 enum,保留命名字段和描述,并且能够作为 vim.log.levels 和函数参数的类型使用。
由索引模型根据 Issue 内容生成。
评估
- 技术栈
- lua
- 领域
- devtools
- Issue 类型
- 功能
- 难度
- 4/5
- 预计耗时
- 3-5 天
- 活跃度
- 停滞
- 描述清晰度
- 基本清楚
- 新手友好度
- 45/100