LuaLS / lua-language-server

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

Plugin not recognizing class functions properly? #1475

Closed jakubg1 closed 2 years ago

jakubg1 commented 2 years ago

Hello, I apologize if the following issue has been already mentioned somewhere. I've been searching through the results, however none of them fully described a problem nor any solution helped.

First of all, I have three files (plus a batch script to run the code) of the following contents:

main.lua

local Car = require("Car")

---@type Car
_MyCar = Car()

print(_MyCar:getEngineRunning())
_MyCar:ignite(true)
print(_MyCar:getEngineRunning())

Car.lua

---@class Car

local class = require "class"
local Car = class:derive("Car")

function Car:new()
    self.engineRunning = false
end

--- Ignites this car.
---@param parameter boolean: A dummy parameter which has absolutely no use.
function Car:ignite(parameter)
    self.engineRunning = true
end

--- Returns `true` if this car's engine is running.
function Car:getEngineRunning()
    return self.engineRunning
end

return Car

class.lua: a straight up copy of this file: https://github.com/bncastle/love2d-tutorial/blob/Episode4/class.lua

Brief description so it's not that dry. Car.lua defines a Car class which contains simple functions involving altering its state. main.lua creates an object of type Car and does stuff on it. And class.lua is just an OOP juice.

After opening these in Visual Studio Code, main.lua gives warnings: obraz

It seems that it can't find a class associated with that variable, despite giving it a ---@type indicator. I'd expect it to look something like that, along with proper autocompletion: obraz

Now, is this my omission, is it a bug, or is it a thing that is just not implemented? Or maybe something else? I'd be glad to provide more information if needed, or tell me what I need to change/fix.

Thank you in advance!

NeOzay commented 2 years ago

---@class is simply misplaced

local class = require "class"
---@class Car
---@overload fun():Car 'to make the class callable'
local Car = class:derive("Car")

function Car:new()
    self.engineRunning = false
end

--- Ignites this car.
---@param parameter boolean: A dummy parameter which has absolutely no use.
function Car:ignite(parameter)
    self.engineRunning = true
end

--- Returns `true` if this car's engine is running.
function Car:getEngineRunning()
    return self.engineRunning
end

return Car
jakubg1 commented 2 years ago

It's working now. Thank you so much!