Skip to content

Call method operator not resolved properly when using Hump's class.lua #3167

@cprn

Description

@cprn

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.:

-- player.lua
Player = class{}

function Player:init(x, y)
    self.x = x
    self.y = y
end

function Player:update(dt)
    -- [...]
end
-- main.lua
class = require('class')
require('player')

p = Player( -- it should autocomplete `Player:init()` params
p: -- ctrl+space after `:` should show `update(dt)`, etc.

Actual Behaviour

LS says Player is:

(global) Player: {
    init: function,
    update: function,
    x: any,
    y: any,
}

...but p is:

(global) p: unknown

My understanding is that p:update(dt) is just a syntactic sugar for p.update(self, dt), so p: should trigger LuaLS to look up methods from the table and the meta table. AFAIU class.lua does some magic with setmetatable() 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

  1. Go to https://github.com/vrld/hump/blob/master/class.lua - download raw file
  2. Put it in a fresh project
  3. Create sample class:
-- player.lua
Player = class{}

function Player:init(x, y)
    self.x = x
    self.y = y
end

function Player:update(dt)
    -- [...]
end
  1. Require both in main:
-- main.lua
class = require('class')
require('player')

p = Player(5, 10)
  1. Try autocompleting methods in main

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 for push.lua (a resolution scaling library for Love2D), but push.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 and push.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)

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions