LuaLS / lua-language-server

A language server that offers Lua language support - programmed in Lua
https://luals.github.io
MIT License
3.25k stars 305 forks source link

cannot recognize emmy field #1971

Open darjun opened 1 year ago

darjun commented 1 year ago

I have defined an emmy class role:

---@class role
---@field equipment_mgr equipment_mgr

an emmy class equipment_mgr:

---@class equipment_mgr
local data = {
        role = role, ---@type role
        equipment = orm_equipment, ---@type table<integer, db.Equipment>
}

then add some method to equipment_mgr:

---@type equipment_mgr
local mt = {}
mt.__index = mt

---@return db.Equipment
function mt:get_equipment(uid)
    return self.equipment[uid]
end

Lua Diagnostics cannot recognize the method get_equipment: image

while other extension can

sumneko commented 1 year ago

You should use:

---@class equipment_mgr
local mt = {}
mt.__index = mt

---@return db.Equipment
function mt:get_equipment(uid)
    return self.equipment[uid]
end
sumneko commented 1 year ago

@carsakiller It seems that many people will try to declare fields for class by ---@type. Can you add the correct method to the wiki (using ---@class)?

carsakiller commented 1 year ago

The usage of @field to document class fields is already mentioned in @class and @field, the latter including multiple examples of it as well.

carsakiller commented 1 year ago

Would it not be documented like this? I believe this is how the wiki explains it.

---@class role
---@field equipment_mgr equipment_mgr

---@class equipment_mgr
---@field role role
---@field equipment db.Equipment[]
local data = {
        role = role,
        equipment = orm_equipment,
}

---@type equipment_mgr
local mt = {}
mt.__index = mt

---@return db.Equipment
function mt:get_equipment(uid)
    return self.equipment[uid]
end

Or would we have to use ---@class equipment_mgr like in your example so that mt:get_equipment is added to the @class type and not just to mt?

darjun commented 1 year ago

@carsakiller It seems that many people will try to declare fields for class by ---@type. Can you add the correct method to the wiki (using ---@class)?

If I declare class in this way, then how would I declare data fields in emmy class? It seems declaring fields to class annotated with @type does not workπŸ€”

carsakiller commented 1 year ago

@sumneko I am confused πŸ˜„. Could you clarify how and why this should be done?

sumneko commented 1 year ago

@sumneko I am confused πŸ˜„. Could you clarify how and why this should be done?

incorrect

---@class A

---@type A
local m = {}
m.someValue = 1 -- can not declare field `someValue` into `A`

---@type A
local n

print(n.someValue) -- undefined-field

correct

---@class A

---@class A
local m = {}
m.someValue = 1 -- declare field `someValue` into `A`

---@type A
local n

print(n.someValue) -- integer

just think about this:

---@class A
local m = {}
m.count = 0

---@type A
local n

n.coount = n.coount + 1 -- don't declare `coount` into `A`
darjun commented 1 year ago

@sumneko I have three modules, each module declares some methods to a same class, how could I add these methods into the class?

-- mod1.lua
---@class role
local mt = {}

function mt:method1_1()
end

function mt:method1_2()
end

return mt
-- mod2.lua
---@class role
local mt = {}

function mt:method2_1()
end

function mt:method2_2()
end

return mt
-- mod3.lua
---@class role
local mt = {}

function mt:method3_1()
end

function mt:method3_2()
end

return mt

use @class in all these files will lead to duplicate class definition. Do you have some suggestions? Thank you 😘

carsakiller commented 1 year ago

I added a note to @type in the wiki that asks users to instead use @class when trying to add a field. It also links to the above explanation.