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

fix: improve type narrow with literal alias param during completion and signature help #2932

Merged
merged 4 commits into from
Nov 5, 2024

Conversation

tomlau10
Copy link
Contributor

@tomlau10 tomlau10 commented Nov 3, 2024

This supports type narrow during completion and signature help when function params are using @alias with literal values, as requested here: #1456 (comment)

Example use case:

---@alias events.AAA
---| "AAA" # description to AAA
---@alias events.BBB
---| "BBB" # description to BBB

---@class Emitter
---@field on fun(self: self, event: events.AAA,  cb: fun(a: integer))
---@field on fun(self: self, event: events.BBB, cb: fun(b: string))
local Emitter = {}

Emitter:on('BBB', <??>) --< trigger completion will suggest `fun(b: string)`, instead of showing both

中文版

上述 issue comment 作者想用 @alias 來定義 event name,因為這樣可以對 enum value 加 description,但發現用 alias 後做 completion 時無法做 type narrow。
這個 PR 是想在做 completion 和 signature help 時,增加檢查 function param 對應的 literal values 是否符合 event name

  • 如果 function param 沒有 literal value => 無從判斷,維持原狀不會 narrow
  • 有 literal values,並且至少 1個跟 call param literal value 是 match => 允許
  • 有 literal values,但無一跟 call param literal value 是 match => 過濾掉

@sumneko sumneko merged commit 1753f07 into LuaLS:master Nov 5, 2024
11 checks passed
@tomlau10 tomlau10 deleted the fix/completion_type_narrow branch November 9, 2024 07:57
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

Successfully merging this pull request may close these issues.

2 participants