-
-
Notifications
You must be signed in to change notification settings - Fork 325
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
rawset
does not support ---@class
annotations
#2862
Comments
I think currently it is not supported, because a
Can you write it as ---@class EntitySerializer
_G.EntitySerializer = {} workaround 1If it's not possible either, another approach is to fake the language server by writing another dummy assignment statement: rawset(_G, "EntitySerializer", {})
---@class EntitySerializer
EntitySerializer = EntitySerializer
-- or even _G.EntitySerializer = _G.EntitySerializer workaround 2If you found the above too hacky (or it doesn't work well due to your environment), then you may want to write a -- meta.lua
-- whatever filename is ok, but make sure it is in your workspace for luals to preload it
-- or add this file in your `workspace.library` setting
-- say if you put all meta files under `meta/` then you can just add `meta/` in `workspace.library[]`
---@meta _
---@class EntitySerializer
EntitySerializer = {} workaround 3this is not good... newly defined fields seems get injected into `table` class instead of `EntitySerializer` ... (you may still toggle to see this old suggestion)edit: just another workaround 🤔 -- EntitySerializer.lua
---@class EntitySerializer
local EntitySerializer = {}
rawset(_G, "EntitySerializer", EntitySerializer) but in this way |
Workaround 1 is too hacky. Workaround 3 is too unreliable. Workaround 2 is what I am currently using, but it is still far from ideal because you need to remember to do it for every new module, and because less-experienced modders in the community will be either confused or avoid the extension if they have to rely on hacks for basic functionality. That said, workaround 2 is probably what I will continue using if I have no better option. |
May I ask what's special in your environment? Does it have special metatable set to |
As a recent contributor having fixed a few issues, I would say many features of luals are just patching here and there, especially for those very specific use cases / features. Sometimes new features added in luals might break / not be compatible with existing features. Unless someone can create a simple PR to fix a specific issue, I would never say that supporting / fixing that specific thing is easy. 😅 |
Yes, this Lua API has a protection against users accidentally creating globals, as it is a terrible practice 99% of the time and new users commonly forget to use the With that said, the idea of using a plugin to replace |
Yeah though I am not 100% sure, luals may not plan to support using The limited support for
|
After testing a bit, I think I am able to make it work (support
local bindDocAccept = {
'local' , 'setlocal' , 'setglobal',
'setfield' , 'setmethod' , 'setindex' ,
'tablefield', 'tableindex', 'self' ,
'function' , 'return' , '...' ,
'call', -- add `call` type
}
local function bindDocs(state)
local text = state.lua
local sources = {}
guide.eachSourceTypes(state.ast, bindDocAccept, function (src)
-- allow binding docs with rawset(_G, ...)
if src.type == 'call' then
if src.node.special ~= 'rawset' or not src.args then
return
end
local g, key = src.args[1], src.args[2]
if not g or not key or g.special ~= '_G' then
return
end
end
sources[#sources+1] = src
end)
...
or src.type == 'return'
or src.type == '...'
or src.type == 'call' -- for `rawset`
then
...
---@class A
---@field id integer
rawset(_G, "A", {})
print(A. <-- here will suggest `id` as integer field But ...the bug (?) that I mentioned in the previous comment still persists: |
Thank you for your insightful comments! I think I'll look into opening a separate issue for the The |
The related PR and bugfix is merged, so this feature will be in the next release of LuaLS 😄 @LJSonik |
That was fast! Thank you!! |
How are you using the lua-language-server?
Visual Studio Code Extension (sumneko.lua)
Which OS are you using?
Windows
What is the issue affecting?
Annotations
Expected Behaviour
I expect
EntitySerializer.serializePlayer
to autocomplete properly inmain.lua
.Actual Behaviour
It does not autocomplete because the
---@class
annotation is apparently ignored withrawset
.It works if I do
EntitySerializer = {}
instead, but I cannot do that in the environment I work with.Reproduction steps
EntitySerializer.lua:
Player.lua:
main.lua:
Additional Notes
No response
Log File
No response
The text was updated successfully, but these errors were encountered: