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

inline @class does not work properly without empty line in between #2673

Closed
tomlau10 opened this issue May 20, 2024 · 1 comment
Closed

inline @class does not work properly without empty line in between #2673

tomlau10 opened this issue May 20, 2024 · 1 comment

Comments

@tomlau10
Copy link

How are you using the lua-language-server?

Visual Studio Code Extension (sumneko.lua)

Which OS are you using?

Windows

What is the issue affecting?

Annotations

Expected Behaviour

I suppose @class can be used inline like local ClassA = {} ---@class ClassA ?
(please correctly me if I am wrong)

So the following code should work, which 3 classes are defined

local ClassA = {}   ---@class ClassA
local ClassB = {}   ---@class ClassB
local ClassC = {}   ---@class ClassC

Actual Behaviour

However, with the above test code, only the last class ClassC is recognized.
And if I add empty lines in between, then it will work properly.

local ClassA = {}   ---@class ClassA

local ClassB = {}   ---@class ClassB

local ClassC = {}   ---@class ClassC

Reproduction steps

As described with the minimal test code above.

Additional Notes

My actual use case is that I have a file defined some class namespace, to act as an common table reference. And the actual class implementation is spitted into multiple files, with each requires the type namespace then extend it from there to prevent circular require.
Right now the workaround is to add blank line in between the namespace declaration.

  • model/Type.lua
local Type = {
    Pc = {},    ---@class Pc

    Npc = {},   ---@class Npc

    Item = {},  ---@class Item
}
return Type
  • model/pc/Pc.lua
local Type = require "model.Type"

---@class Pc
local Pc = Type.Pc
function Pc.new() end

-- require all other pc.* module at the bottom of file
  • model/pc/Hp.lua
local Type = require "model.Type"

---@class Pc
local Pc = Type.Pc
function Pc:setHp(hp) end
  • logic/pccmd.lua
local Type = require "model.Type"
local Pc = Type.Pc

-- no completion for `Pc` here if I don't add blank line in `model/Type.lua`

Log File

No response

@tomlau10
Copy link
Author

tomlau10 commented Jun 6, 2024

I just found that in the above case I should use ---@type instead

local Type = {
    Pc = {},    ---@type Pc
    Npc = {},   ---@type Npc
    Item = {},  ---@type Item
}
return Type

problem solved 😄

@tomlau10 tomlau10 closed this as completed Jun 6, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant