Skip to content

Commit

Permalink
Merge pull request #3055 from tomlau10/fix/generic_class_completion
Browse files Browse the repository at this point in the history
fix: missing field completion for generic class object
  • Loading branch information
sumneko authored Feb 6, 2025
2 parents 32b5981 + 3a8203e commit 2a57892
Show file tree
Hide file tree
Showing 4 changed files with 65 additions and 2 deletions.
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

0 comments on commit 2a57892

Please sign in to comment.