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

cannot recognize emmy field #1971

Open
darjun opened this issue Mar 3, 2023 · 9 comments
Open

cannot recognize emmy field #1971

darjun opened this issue Mar 3, 2023 · 9 comments
Labels
feat/LuaCats Annotations Related to Lua Language Server Annotations (LuaCats) question User has a question

Comments

@darjun
Copy link

darjun commented Mar 3, 2023

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 sumneko added question User has a question feat/LuaCats Annotations Related to Lua Language Server Annotations (LuaCats) labels Mar 3, 2023
@sumneko
Copy link
Collaborator

sumneko commented Mar 3, 2023

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
Copy link
Collaborator

sumneko commented Mar 3, 2023

@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
Copy link
Collaborator

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
Copy link
Collaborator

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
Copy link
Author

darjun commented Mar 4, 2023

@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
Copy link
Collaborator

@sumneko I am confused 😄. Could you clarify how and why this should be done?

@sumneko
Copy link
Collaborator

sumneko commented Mar 7, 2023

@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
Copy link
Author

darjun commented Mar 7, 2023

@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
Copy link
Collaborator

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
feat/LuaCats Annotations Related to Lua Language Server Annotations (LuaCats) question User has a question
Projects
None yet
Development

No branches or pull requests

3 participants