LuaLS / LuaLS/lua-language-server

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

Đang mở
#1,932 14 bình luận 0 reaction 0 người được giao Xem trên GitHub

Chưa có ai nhận issue này.

enhancement
Ngôn ngữ chính
Lua
Star
4.4k
Fork
442
Chỉ số merge pull request
Không có pull request nào được merge trong 30 ngày

Mô tả

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.

Hướng dẫn đóng góp

Mở hướng dẫn đóng góp

Bắt đầu từ đâu

  1. Đọc hết issue, rồi đọc hướng dẫn đóng góp của dự án.
  2. Bình luận trên issue rằng bạn sẽ nhận — tránh hai người làm cùng một việc.
  3. Fork repository và làm thay đổi trên một nhánh.
  4. Mở pull request có tham chiếu số hiệu của issue.

Hướng nghiên cứu

Bắt đầu bằng cách tái hiện các ví dụ khai báo và gán enum trong một thiết lập Neovim/Lua-language-server, sau đó truy vết cách các chú thích @enum, việc gán kiểu và đầu ra hover được xử lý. Công việc được xem là hoàn tất khi có thể khai báo enum mà không cần gán cho một biến, vẫn giữ các trường có tên và mô tả, đồng thời hoạt động như kiểu của vim.log.levels và các tham số hàm.

Do mô hình lập chỉ mục viết ra từ nội dung của issue.

Đánh giá

Công nghệ
lua
Lĩnh vực
devtools
Loại issue
Tính năng
Độ khó
4/5
Thời gian dự kiến
3-5 ngày
Mức độ hoạt động
Đình trệ
Độ rõ ràng
Khá rõ ràng
Mức phù hợp với người mới
45/100

Nhận issue mới trong hộp thư của bạn

Bản tóm tắt ngắn những issue GitHub phù hợp với người mới.