LuaLS / LuaLS/lua-language-server

[Request] Improve support for custom number types

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

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

feat/LuaCats Annotations question
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ả

In the builtin definitions, the integer type is defined as a child class of the number type:

---@class integer : number

Despite that, it is still possible to write such code without any type-checking errors:

---@param lives integer
local function printLives(lives)
    print(lives)
end

---@type number
local lives = 9.0
printLives(lives)

I think this is fine, it makes sense.

However, this behaviour seems to be hardcoded in the extension.
Many of the functions from the API I use expect fixed-point values as parameters; such arguments are indicated by the fixed type, which is an alternate number type defined in much the same way as integer:

---@class fixed : number

To represent literal fixed-point values, the API provides a unit contant which is used something like this:

move(4*UNIT, 10*UNIT, UNIT*3/2) -- 40.0, 100.0, 1.5

Technically, fixed is just a regular number, however it is extremely helpful to the user to see this:
image
rather than this:
image

In older versions of the extension, this works perfectly fine; however, recent versions perform type-checking, which is a very useful feature but does not work nicely with the fixed type:
image
It looks like the extension considers number * fixed to be a number, which is a reasonable guess, but causes the error shown in the image above.

To solve this, I have thought of the following solutions:

  1. Completely disabling type-checking. It works, obviously, but it would be preferable to not have to give up on such a useful feature.
  2. Manually casting every literal value. This works too, but is extremely unpractical and results in bloating code.
  3. Somehow make it so that number * fixed resolves to fixed instead of number. But that sounds like a very arbitrary solution to me.
  4. Defining fixed as an alias: ---@alias fixed number. It prevents the error from occuring, and the code is readable, but the completion/hover hints show "number" instead of "fixed", like in the second image.
  5. Forking the extension to add a special case in the type checker.

There may also be other solutions I am not aware of, but my first impression is that solution 4 suits my usecase best, and would probably be perfect if there was some way to make an alias that behaves like in the first image (much like typedef in C, for instance).

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 với các định nghĩa tích hợp sẵn cho integernumber, sau đó truy theo hành vi của trình kiểm tra kiểu đối với phép toán số học có liên quan đến các kiểu số tùy chỉnh như fixed. Được xem là hoàn thành khi các kiểu số tùy chỉnh vẫn giữ được thông tin hữu ích về kiểm tra kiểu và hoàn thành hoặc hover mà không yêu cầu casts, đồng thời bảo toàn hành vi hiện có của integer.

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ó
5/5
Thời gian dự kiến
Hơn một tuần
Mức độ hoạt động
Đình trệ
Độ rõ ràng
Cần làm rõ
Mức phù hợp với người mới
25/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.