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

---@aliases don't work in v3.x as they did in v2.6.8 #1061

Closed
C3pa opened this issue Apr 16, 2022 · 10 comments
Closed

---@aliases don't work in v3.x as they did in v2.6.8 #1061

C3pa opened this issue Apr 16, 2022 · 10 comments
Labels
enhancement New feature or request feat/LuaCats Annotations Related to Lua Language Server Annotations (LuaCats)
Milestone

Comments

@C3pa
Copy link
Contributor

C3pa commented Apr 16, 2022

Describe the bug
In v3.x fields defined in ---@alias with '' get converted to string literals (inside ""). That wasn't the behavior of the older versions.

To Reproduce
Steps to reproduce the behavior, use the following code:

local ngx = {}

ngx.DEBUG  = 8
ngx.INFO   = 7
ngx.NOTICE = 6
ngx.WARN   = 5
ngx.ERR    = 4
ngx.CRIT   = 3
ngx.ALERT  = 2
ngx.EMERG  = 1
ngx.STDERR = 0

--- NGINX log level constants
--- https://github.com/openresty/lua-nginx-module/#nginx-log-level-constants
---@alias ngx.log.level
---| 'ngx.DEBUG'  # debug
---| 'ngx.INFO'   # info
---| 'ngx.NOTICE' # notice
---| 'ngx.WARN'   # warning
---| 'ngx.ERR'    # error
---| 'ngx.ALERT'  # alert
---| 'ngx.CRIT'   # critical
---| 'ngx.EMERG'  # emergency
---| 'ngx.STDERR' # standard error
---@param level ngx.log.level
---@param ... string|number|'nil'|'ngx.null'
function ngx.log(level, ...) end

ngx.log
  1. Start typing (
  2. See the error

Expected behavior
The expected behavior is the behavior from v2.6.8. The alias entries enclosed in single '' shouldn't be converted to string literals (enclosed with "").

Screenshots

  • v3.0.2:

image

  • The autocomplete suggestion.

image

  • What the autocomplete expands to.

  • v2.6.8:

image

  • The autocomplete suggestion.

image

  • What the autocomplete expands to.

Environment:

  • OS: Windows
  • Client: VSCode

Additional context
If there is a reason why this is changed, could you instruct me on how to achieve the same result with a different approach?

@C3pa C3pa changed the title ---@alias Don't work in v3.x as they did in v2.6.8 ---@aliases don't work in v3.x as they did in v2.6.8 Apr 16, 2022
@sumneko sumneko added feat/LuaCats Annotations Related to Lua Language Server Annotations (LuaCats) beta Related to beta version bug Something isn't working and removed beta Related to beta version labels Apr 16, 2022
@sumneko sumneko reopened this Apr 16, 2022
@sumneko
Copy link
Collaborator

sumneko commented Apr 16, 2022

see #980
Dose not support this behavior after 3.X

@sumneko sumneko added enhancement New feature or request and removed bug Something isn't working labels Apr 16, 2022
@C3pa
Copy link
Contributor Author

C3pa commented Apr 16, 2022

Such a shame. This allowed providing autocomplete for functions with enumeration constants in a separate table. That also means that there are some ---@meta files here, that don't work anymore. Actually, that was the place I took the above example from.

@sumneko
Copy link
Collaborator

sumneko commented Apr 16, 2022

The new syntax will be used to implement this function in the plan:

---@alias ngx.log.level
---| `ngx.DEBUG`  # debug
---| `ngx.INFO`   # info
---| `ngx.NOTICE` # notice

@C3pa
Copy link
Contributor Author

C3pa commented Apr 16, 2022

Nice. Also, it does make sense to treat the values inside '' or "" as strings. Another benefit is that moving to `` quotes won't be hard to update in the existing code.

@C3pa
Copy link
Contributor Author

C3pa commented Apr 16, 2022

Thanks for responding quickly. Your project is so good!

@sumneko sumneko added this to the 3.3.0 milestone May 28, 2022
sumneko added a commit that referenced this issue Jun 9, 2022
@sumneko
Copy link
Collaborator

sumneko commented Jun 9, 2022

Have supported, need to update meta files

@flrgh
Copy link
Contributor

flrgh commented Jun 9, 2022

Thanks @sumneko! I am trying to update the OpenResty annotations for this now and running into a hiccup. Right now completion support isn't working how I expect.

Can you tell me if I'm doing something wrong here?

---@class ns
local ns = {}

ns.A = 1 ---@class ns.A
ns.B = 2 ---@class ns.B

---@alias ClassEnum
---| `ns.A`
---| `ns.B`


---@param e ClassEnum
local function foo(e)
  print(e)
  return e
end

foo(ns.A)

---@alias IntEnum
---| 1
---| 2


---@param e IntEnum
local function bar(e)
  print(e)
  return e
end

hover works as expected for foo:

image

However, foo receives no special completion results. If I explicitly request completion within the editor I get the default/global types:
Screenshot from 2022-06-09 11-54-59

Observe bar receives the proper completion results:
Screenshot from 2022-06-09 11-55-24

service.log

neovim-lsp.log

@C3pa
Copy link
Contributor Author

C3pa commented Jun 10, 2022

Hi @flrgh!

According to the commit that implements this feature, it is now supposed to be written as:

---@type `CONST.X` | `CONST.Y`
local x
if x == -- suggest `CONST.X` and `CONST.Y` here

So, a new local table with the constants needs to be constructed and then used as a parameter to functions that accept those.

@sumneko
Copy link
Collaborator

sumneko commented Jun 10, 2022

@flrgh this feature dose not include in v3.2.5, please checkout master

@C3pa
Copy link
Contributor Author

C3pa commented Jun 15, 2022

Thanks for implementing this!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request feat/LuaCats Annotations Related to Lua Language Server Annotations (LuaCats)
Projects
None yet
Development

No branches or pull requests

3 participants