Skip to content

Commit 01d5bb7

Browse files
committed
search definitions by 1st arg of setmetatable
resolve #1575
1 parent 137d40a commit 01d5bb7

File tree

3 files changed

+45
-0
lines changed

3 files changed

+45
-0
lines changed

changelog.md

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,15 @@ server will generate `doc.json` and `doc.md` in `LOGPATH`.
3737
* `return-type-mismatch`
3838
* `CHG` workspace-symbol: supports chain fields based on global variables and types. try `io.open` or `iolib.open`
3939
* `CHG` [#1641] if a function only has varargs and has `---@overload`, the varargs will be ignored
40+
* `CHG` [#1575] search definitions by first argument of `setmetatable`
41+
```lua
42+
---@class Object
43+
local obj = setmetatable({
44+
initValue = 1,
45+
}, mt)
46+
47+
print(obj.initValue) --> `obj.initValue` is integer
48+
```
4049
* `FIX` [#1567]
4150
* `FIX` [#1593]
4251
* `FIX` [#1595]
@@ -53,6 +62,7 @@ server will generate `doc.json` and `doc.md` in `LOGPATH`.
5362
[#1558]: https://github.com/sumneko/lua-language-server/issues/1558
5463
[#1561]: https://github.com/sumneko/lua-language-server/issues/1561
5564
[#1567]: https://github.com/sumneko/lua-language-server/issues/1567
65+
[#1575]: https://github.com/sumneko/lua-language-server/issues/1575
5666
[#1582]: https://github.com/sumneko/lua-language-server/issues/1582
5767
[#1593]: https://github.com/sumneko/lua-language-server/issues/1593
5868
[#1595]: https://github.com/sumneko/lua-language-server/issues/1595

script/vm/compiler.lua

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -368,6 +368,27 @@ function vm.getClassFields(suri, object, key, ref, pushResult)
368368
end
369369
end)
370370
end
371+
if src.value
372+
and src.value.type == 'select'
373+
and src.value.vararg.type == 'call' then
374+
local func = src.value.vararg.node
375+
local args = src.value.vararg.args
376+
if func.special == 'setmetatable'
377+
and args
378+
and args[1]
379+
and args[1].type == 'table' then
380+
searchFieldSwitch('table', suri, args[1], key, ref, function (field)
381+
local fieldKey = guide.getKeyName(field)
382+
if fieldKey then
383+
if not searchedFields[fieldKey]
384+
and guide.isSet(field) then
385+
hasFounded[fieldKey] = true
386+
--pushResult(field, true)
387+
end
388+
end
389+
end)
390+
end
391+
end
371392
copyToSearched()
372393
searchFieldSwitch(src.type, suri, src, key, ref, function (field)
373394
local fieldKey = guide.getKeyName(field)

test/diagnostics/type-check.lua

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -841,6 +841,20 @@ TEST [[
841841
local <!test!> = 4
842842
]]
843843

844+
TEST [[
845+
---@class MyClass
846+
local MyClass = {}
847+
848+
function MyClass:new()
849+
---@class MyClass
850+
local myObject = setmetatable({
851+
initialField = true
852+
}, self)
853+
854+
print(myObject.initialField)
855+
end
856+
]]
857+
844858
config.remove(nil, 'Lua.diagnostics.disable', 'unused-local')
845859
config.remove(nil, 'Lua.diagnostics.disable', 'unused-function')
846860
config.remove(nil, 'Lua.diagnostics.disable', 'undefined-global')

0 commit comments

Comments
 (0)