-
-
Notifications
You must be signed in to change notification settings - Fork 353
Call method operator not resolved properly when using Hump's class.lua
#3167
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
Comments
LuaLS is not a Lua interpreter, it is a static code analyzer to provide type inferencing. So it doesn't have the same magic with a Lua interpreter and it will not run your actual code. To leverage its full potential, you need to use type annotations: https://luals.github.io/wiki/annotations/ While LuaLS can do basic type inferencing without annotation, it cannot dynamically create the class and constructor for you. You will need a simple
---@class Player
---@overload fun(x, y): Player
Player = class{}
function Player:init(x, y)
self.x = x
self.y = y
end
function Player:update(dt)
-- [...]
end
class = require 'class'
require 'player'
p = Player(5, 10) --> should now see `p: Player`
p: --> will suggest `init:(x, y)`, `update(dt)`
local mylib = {}
function mylib.a() end
function mylib.b() end
return mylib => In this case when you do
-- the module
return setmetatable({new = new, include = include, clone = clone},
{__call = function(_,...) return new(...) end}) => so not many analysis can be done on LuaLS side, especially you are using
No, You may consider using the discussion page as well: https://github.com/LuaLS/lua-language-server/discussions, since issues are mainly for reporting bugs. |
Thank you, very much! This explains everything. I've seen annotations on the wiki, and I was struggling to deal with it, but your answer made it crystal clear. 😀 And sorry for the ticket - closing. Discussions seem to be a fairly new addition to GitHub. I wasn't aware of them. |
How are you using the lua-language-server?
NeoVim
Which OS are you using?
Linux
What is the issue affecting?
Completion
Expected Behaviour
When using
class.lua
from a seemingly popular Hump library for Love2D, LS should autocomplete class methods, e.g.:Actual Behaviour
LS says
Player
is:...but
p
is:My understanding is that
p:update(dt)
is just a syntactic sugar forp.update(self, dt)
, sop:
should trigger LuaLS to look up methods from the table and the meta table. AFAIUclass.lua
does some magic withsetmetatable()
to make that work. The code itself works fine, so Lua interpreter doesn't have issues finding the methods in meta, just LS does.Reproduction steps
Additional Notes
I'm new to Lua, and don't understand yet what
class.lua
does exactly, so it might be that (old) library's fault... especially because the same issue doesn't happen forpush.lua
(a resolution scaling library for Love2D), butpush.lua
doesn't try to extend Lua objects.I wasted 14 hours trying to solve it with AI and all models I tried say it should work. I looked for similar issues, but I only found some about
exact
class, and IDK what that is yet - if it's the same thing, sorry.Both, Hump's
class.lua
andpush.lua
, are used in Harvard CS50's gamedev courses.Log File
log file on Gist
(it doesn't fit, because it loads Luv library and the entire Neovim's runtime - sorry for that 🙁 that's what the "initial" Neovim's config does, so many Neovim users will have exactly that)
The text was updated successfully, but these errors were encountered: