Skip to content

Commit 2a57892

Browse files
authored
Merge pull request #3055 from tomlau10/fix/generic_class_completion
fix: missing field completion for generic class object
2 parents 32b5981 + 3a8203e commit 2a57892

File tree

4 files changed

+65
-2
lines changed

4 files changed

+65
-2
lines changed

changelog.md

+1
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
* `FIX` Generic return can be optional.
1818
* `FIX` Fixed the comment calculating in docs `---@param a string?Comment` - now its `Comment` instead of `omment`.
1919
* `FIX` Fixed cannot bind variables using tail comment `@class` [#2673](https://github.com/LuaLS/lua-language-server/issues/2673)
20+
* `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)
2021
* `NEW` `---@class` supports attribute `partial`, which will not check missing inherited fields [#3023](https://github.com/LuaLS/lua-language-server/issues/3023)
2122
```lua
2223
---@class Config

script/vm/compiler.lua

+11
Original file line numberDiff line numberDiff line change
@@ -304,6 +304,17 @@ local searchFieldSwitch = util.switch()
304304
end
305305
end
306306
end)
307+
: case 'doc.type.sign'
308+
: call(function (suri, source, key, pushResult)
309+
if not source.node[1] then
310+
return
311+
end
312+
local global = vm.getGlobal('type', source.node[1])
313+
if not global then
314+
return
315+
end
316+
vm.getClassFields(suri, global, key, pushResult)
317+
end)
307318
: case 'global'
308319
: call(function (suri, node, key, pushResult)
309320
if node.cate == 'variable' then

test/completion/common.lua

+41
Original file line numberDiff line numberDiff line change
@@ -4572,3 +4572,44 @@ M.create():optional(<??>):self()
45724572
kind = define.CompletionItemKind.EnumMember,
45734573
},
45744574
}
4575+
4576+
TEST [[
4577+
---@class Array<T>: { [integer]: T }
4578+
---@field length integer
4579+
local Array
4580+
4581+
function Array:push() end
4582+
4583+
---@type Array<string>
4584+
local a
4585+
print(a.<??>)
4586+
]]
4587+
{
4588+
include = true,
4589+
{
4590+
label = 'length',
4591+
kind = define.CompletionItemKind.Field,
4592+
},
4593+
{
4594+
label = 'push(self)',
4595+
kind = define.CompletionItemKind.Method,
4596+
},
4597+
}
4598+
4599+
TEST [[
4600+
---@class Array<T>: { [integer]: T }
4601+
---@field length integer
4602+
local Array
4603+
4604+
function Array:push() end
4605+
4606+
---@type Array<string>
4607+
local a
4608+
print(a:<??>)
4609+
]]
4610+
{
4611+
{
4612+
label = 'push()',
4613+
kind = define.CompletionItemKind.Method,
4614+
},
4615+
}

test/completion/init.lua

+12-2
Original file line numberDiff line numberDiff line change
@@ -40,8 +40,18 @@ local function include(a, b)
4040
return false
4141
end
4242
if tp1 == 'table' then
43-
for k in pairs(a) do
44-
if not eq(a[k], b[k]) then
43+
-- a / b are array of completion results
44+
-- when checking `include`, the array index order is not important
45+
-- thus need to check every results in b
46+
for _, v1 in ipairs(a) do
47+
local ok = false
48+
for _, v2 in ipairs(b) do
49+
if eq(v1, v2) then
50+
ok = true
51+
break
52+
end
53+
end
54+
if not ok then
4555
return false
4656
end
4757
end

0 commit comments

Comments
 (0)