LuaLS / lua-language-server

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

improve the missing-fields logic to be able to correctly handle classes defined several times #2770

Closed NeOzay closed 3 months ago

NeOzay commented 3 months ago

linked to https://github.com/LuaLS/lua-language-server/issues/2769.

this PR allows:

---@class Foo
---@field a number
---@field b number
---@field c number

---@class Foo

---@type Foo
local x = {
    a = 1,
    b = 2,
}

image

better display of missing fields

---@class Foo
---@field a number
---@field b number
---@field c number

---@class Foo
---@field d number

---@type Foo
local x = {
    a = 1,
    b = 2,
}

image

detects missing index fields

---@class A
---@field [1] string

---@type A
local t = {}

image

support class unions

---@class Foo
---@field a number
---@field b number
---@field c number

---@class Foo
---@field d number

---@class Bar
---@field ba number
---@field bb number
---@field bc number

---@class Bar
---@field bd number

---@type Foo|Bar
local x = {
    a = 1,
    b = 2,
}
---@type Foo|Bar
local y = {
    ba = 1,
    bb = 2,
    bc = 3,
    bd = 4,
}

image

lewis6991 commented 3 months ago

---@type Foo|Bar local x = { a = 1, b = 2, }

The diagnostic for this looks wrong to me. Foo|Bar doesn't mean it is both types, it means it is one of those types, just as string|integer is not both a string and an integer.

I'm not sure what the correct diagnostic should be, maybe it should be missing c since it has most of Foo.

NeOzay commented 3 months ago

What I can do is only list the Classes that include all the specified fields.

CppCXY commented 3 months ago

Please resolve the conflict.

max397574 commented 3 months ago

What I can do is only list the Classes that include all the specified fields.

I think that would make more sense (if there is such a class) Because in the example if you say missing fields for Bar there would also have to be a diagnostic because additional fields which aren't defined for Bar where added

sumneko commented 3 months ago

Thank you!