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

fix: missing field completion for generic class object #3055

Merged
merged 3 commits into from
Feb 6, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions changelog.md
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
* `FIX` Generic return can be optional.
* `FIX` Fixed the comment calculating in docs `---@param a string?Comment` - now its `Comment` instead of `omment`.
* `FIX` Fixed cannot bind variables using tail comment `@class` [#2673](https://github.com/LuaLS/lua-language-server/issues/2673)
* `FIX` Fixed missing field completion for generic class object [#2196](https://github.com/LuaLS/lua-language-server/issues/2196) [#2945](https://github.com/LuaLS/lua-language-server/issues/2945) [#3041](https://github.com/LuaLS/lua-language-server/issues/3041)
* `NEW` `---@class` supports attribute `partial`, which will not check missing inherited fields [#3023](https://github.com/LuaLS/lua-language-server/issues/3023)
```lua
---@class Config
Expand Down
11 changes: 11 additions & 0 deletions script/vm/compiler.lua
Original file line number Diff line number Diff line change
Expand Up @@ -304,6 +304,17 @@ local searchFieldSwitch = util.switch()
end
end
end)
: case 'doc.type.sign'
: call(function (suri, source, key, pushResult)
if not source.node[1] then
return
end
local global = vm.getGlobal('type', source.node[1])
if not global then
return
end
vm.getClassFields(suri, global, key, pushResult)
end)
: case 'global'
: call(function (suri, node, key, pushResult)
if node.cate == 'variable' then
Expand Down
41 changes: 41 additions & 0 deletions test/completion/common.lua
Original file line number Diff line number Diff line change
Expand Up @@ -4572,3 +4572,44 @@ M.create():optional(<??>):self()
kind = define.CompletionItemKind.EnumMember,
},
}

TEST [[
---@class Array<T>: { [integer]: T }
---@field length integer
local Array

function Array:push() end

---@type Array<string>
local a
print(a.<??>)
]]
{
include = true,
{
label = 'length',
kind = define.CompletionItemKind.Field,
},
{
label = 'push(self)',
kind = define.CompletionItemKind.Method,
},
}

TEST [[
---@class Array<T>: { [integer]: T }
---@field length integer
local Array

function Array:push() end

---@type Array<string>
local a
print(a:<??>)
]]
{
{
label = 'push()',
kind = define.CompletionItemKind.Method,
},
}
14 changes: 12 additions & 2 deletions test/completion/init.lua
Original file line number Diff line number Diff line change
Expand Up @@ -40,8 +40,18 @@ local function include(a, b)
return false
end
if tp1 == 'table' then
for k in pairs(a) do
if not eq(a[k], b[k]) then
-- a / b are array of completion results
-- when checking `include`, the array index order is not important
-- thus need to check every results in b
for _, v1 in ipairs(a) do
local ok = false
for _, v2 in ipairs(b) do
if eq(v1, v2) then
ok = true
break
end
end
if not ok then
return false
end
end
Expand Down