Skip to content

Commit 4c96b6a

Browse files
committed
fix #1033
1 parent 23ef4b1 commit 4c96b6a

File tree

4 files changed

+50
-11
lines changed

4 files changed

+50
-11
lines changed

changelog.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,8 @@
11
# changelog
22

3+
## 3.0.1
4+
* `FIX` [#1033](https://github.com/sumneko/lua-language-server/issues/1033)
5+
36
## 3.0.0
47
`2022-4-10`
58
* `CHG` [break changes](https://github.com/sumneko/lua-language-server/issues/980)

script/vm/compiler.lua

Lines changed: 23 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -144,47 +144,59 @@ local searchFieldSwitch = util.switch()
144144
function m.getClassFields(suri, node, key, pushResult)
145145
local mark = {}
146146

147-
local function searchClass(class)
147+
local function searchClass(class, searchedFields)
148148
local name = class.name
149149
if mark[name] then
150150
return
151151
end
152152
mark[name] = true
153+
searchedFields = searchedFields or {}
153154
for _, set in ipairs(class:getSets(suri)) do
154155
if set.type == 'doc.class' then
155156
-- check ---@field
156-
local hasFounded
157+
local hasFounded = {}
157158
for _, field in ipairs(set.fields) do
159+
local fieldKey = guide.getKeyName(field)
158160
if key == nil
159-
or guide.getKeyName(field) == key then
160-
hasFounded = true
161-
pushResult(field)
161+
or fieldKey == key then
162+
if not searchedFields[fieldKey] then
163+
pushResult(field)
164+
hasFounded[fieldKey] = true
165+
end
162166
end
163167
end
164168
-- check local field and global field
165169
if set.bindSources then
166170
for _, src in ipairs(set.bindSources) do
167171
searchFieldSwitch(src.type, suri, src, key, function (field)
168-
if guide.isSet(field) then
169-
hasFounded = true
172+
local fieldKey = guide.getKeyName(field)
173+
if not searchedFields[fieldKey]
174+
and guide.isSet(field) then
175+
hasFounded[fieldKey] = true
170176
pushResult(field)
171177
end
172178
end)
173179
if src._globalNode then
174180
searchFieldSwitch('global', suri, src._globalNode, key, function (field)
175-
hasFounded = true
176-
pushResult(field)
181+
local fieldKey = field:getKeyName()
182+
if not searchedFields[fieldKey] then
183+
hasFounded[fieldKey] = true
184+
pushResult(field)
185+
end
177186
end)
178187
end
179188
end
180189
end
181190
-- look into extends(if field not found)
182-
if not hasFounded and set.extends then
191+
if not hasFounded[key] and set.extends then
192+
for fieldKey in pairs(hasFounded) do
193+
searchedFields[fieldKey] = true
194+
end
183195
for _, extend in ipairs(set.extends) do
184196
if extend.type == 'doc.extends.name' then
185197
local extendType = globalMgr.getGlobal('type', extend[1])
186198
if extendType then
187-
searchClass(extendType)
199+
searchClass(extendType, searchedFields)
188200
end
189201
end
190202
end

script/vm/global.lua

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,8 @@ mt.__index = mt
1515
mt.type = 'global'
1616
mt.name = ''
1717

18+
local ID_SPLITE = '\x1F'
19+
1820
---@param uri uri
1921
---@param source parser.object
2022
function mt:addSet(uri, source)
@@ -93,6 +95,11 @@ function mt:getName()
9395
return self.name
9496
end
9597

98+
---@return string
99+
function mt:getKeyName()
100+
return self.name:match('[^' .. ID_SPLITE .. ']+$')
101+
end
102+
96103
---@return boolean
97104
function mt:isAlive()
98105
return next(self.links) ~= nil

test/hover/init.lua

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1849,3 +1849,20 @@ local x: {
18491849
[1]: integer = 10,
18501850
}
18511851
]]
1852+
1853+
TEST [[
1854+
---@class A
1855+
---@field x string
1856+
1857+
---@class B: A
1858+
---@field y string
1859+
1860+
---@type B
1861+
local <?t?>
1862+
]]
1863+
[[
1864+
local t: B {
1865+
x: string,
1866+
y: string,
1867+
}
1868+
]]

0 commit comments

Comments
 (0)