Skip to content

Commit 9451329

Browse files
committed
update
1 parent 57a164b commit 9451329

7 files changed

Lines changed: 96 additions & 24 deletions

File tree

script/core/definition.lua

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -152,7 +152,7 @@ return function (uri, offset)
152152
goto CONTINUE
153153
end
154154
else
155-
if guide.isLiteral(src) then
155+
if guide.isLiteral(src) and src.type ~= 'function' then
156156
goto CONTINUE
157157
end
158158
end

script/vm/compiler.lua

Lines changed: 70 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -57,30 +57,80 @@ local searchFieldMap = util.switch()
5757
if field.type == 'tablefield'
5858
or field.type == 'tableindex' then
5959
if guide.getKeyName(field) == key then
60-
pushResult(m.compileNode(field))
60+
pushResult(field)
6161
end
6262
end
6363
end
6464
end)
6565
: case 'global'
6666
---@param node vm.node.global
6767
: call(function (node, key, pushResult)
68-
local global = globalMgr.getGlobal('variable', node.name, key)
69-
if global then
70-
pushResult(global)
68+
if node.cate == 'variable' then
69+
local global = globalMgr.getGlobal('variable', node.name, key)
70+
if global then
71+
pushResult(global)
72+
end
73+
end
74+
if node.cate == 'type' then
75+
m.getClassFields(node, key, pushResult)
7176
end
7277
end)
7378
: case 'local'
7479
: call(function (node, key, pushResult)
7580
local sources = localID.getSources(node, key)
7681
if sources then
7782
for _, src in ipairs(sources) do
78-
pushResult(m.compileNode(src))
83+
pushResult(src)
7984
end
8085
end
8186
end)
8287
: getMap()
8388

89+
90+
function m.getClassFields(node, key, pushResult)
91+
local mark = {}
92+
local function searchClass(class)
93+
local name = class.name
94+
if mark[name] then
95+
return
96+
end
97+
mark[name] = true
98+
for _, set in ipairs(class:getSets()) do
99+
if set.type == 'doc.class' then
100+
-- check ---@field
101+
local hasFounded
102+
for _, field in ipairs(set.fields) do
103+
if guide.getKeyName(field) == key then
104+
hasFounded = true
105+
pushResult(field)
106+
end
107+
end
108+
-- check local field and global field
109+
if set.bindSources then
110+
for _, src in ipairs(set.bindSources) do
111+
if searchFieldMap[src.type] then
112+
searchFieldMap[src.type](src, key, function (src)
113+
hasFounded = true
114+
pushResult(src)
115+
end)
116+
end
117+
end
118+
end
119+
-- look into extends(if field not found)
120+
if not hasFounded and set.extends then
121+
for _, extend in ipairs(set.extends) do
122+
local extendType = globalMgr.getGlobal('type', extend[1])
123+
if extendType then
124+
searchClass(extendType)
125+
end
126+
end
127+
end
128+
end
129+
end
130+
end
131+
searchClass(node)
132+
end
133+
84134
local function getReturnOfFunction(func, index)
85135
if not func._returns then
86136
func._returns = util.defaultTable(function ()
@@ -101,8 +151,8 @@ local function getReturnOfSetMetaTable(source, args)
101151
m.setNode(source, m.compileNode(tbl))
102152
end
103153
if mt then
104-
m.compileByParentNode(mt, '__index', function (node)
105-
m.setNode(source, node)
154+
m.compileByParentNode(mt, '__index', function (src)
155+
m.setNode(source, m.compileNode(src))
106156
end)
107157
end
108158
return source._node
@@ -136,7 +186,7 @@ end
136186

137187
---@param source vm.node
138188
---@param key any
139-
---@param pushResult fun(node:vm.node)
189+
---@param pushResult fun(source: parser.object)
140190
function m.compileByParentNode(source, key, pushResult)
141191
local parentNode = m.compileNode(source)
142192
if not parentNode then
@@ -244,8 +294,8 @@ local compilerMap = util.switch()
244294
: case 'getindex'
245295
: call(function (source)
246296
compileByLocalID(source)
247-
m.compileByParentNode(source.node, guide.getKeyName(source), function (node)
248-
m.setNode(source, node)
297+
m.compileByParentNode(source.node, guide.getKeyName(source), function (src)
298+
m.setNode(source, m.compileNode(src))
249299
end)
250300
end)
251301
: case 'tablefield'
@@ -291,6 +341,10 @@ local compilerMap = util.switch()
291341
type:addGet(uri, source)
292342
m.setNode(source, type)
293343
end)
344+
: case 'doc.field'
345+
: call(function (source)
346+
m.setNode(source, m.compileNode(source.extends))
347+
end)
294348
: getMap()
295349

296350
---@param source parser.object
@@ -303,13 +357,18 @@ end
303357

304358
---@param source parser.object
305359
local function compileByGlobal(source)
360+
if source.type == 'global' then
361+
m.setNode(source, source)
362+
return
363+
end
306364
if source._globalNode then
307365
m.setNode(source, source._globalNode)
308366
for _, set in ipairs(source._globalNode:getSets()) do
309367
if set.value then
310368
m.setNode(source, m.compileNode(set.value))
311369
end
312370
end
371+
return
313372
end
314373
end
315374

@@ -320,8 +379,8 @@ function m.compileNode(source)
320379
return source._node
321380
end
322381
source._node = false
323-
compileByNode(source)
324382
compileByGlobal(source)
383+
compileByNode(source)
325384

326385
localMgr.subscribeLocal(source, source._node)
327386

script/vm/getDef.lua

Lines changed: 15 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -85,13 +85,23 @@ local searchFieldMap = util.switch()
8585
end
8686
end)
8787
: case 'global'
88+
---@param node vm.node
89+
---@param key string
8890
: call(function (node, key, pushResult)
89-
local newGlobal = globalMgr.getGlobal('variable', node.name, key)
90-
if not newGlobal then
91-
return
91+
if node.cate == 'variable' then
92+
local newGlobal = globalMgr.getGlobal('variable', node.name, key)
93+
if newGlobal then
94+
for _, set in ipairs(newGlobal:getSets()) do
95+
pushResult(set)
96+
end
97+
end
9298
end
93-
for _, set in ipairs(newGlobal:getSets()) do
94-
pushResult(set)
99+
if node.cate == 'type' then
100+
compiler.getClassFields(node, key, function (field)
101+
if field.type == 'doc.field' then
102+
pushResult(field.field)
103+
end
104+
end)
95105
end
96106
end)
97107
: case 'local'

script/vm/global-manager.lua

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -162,7 +162,7 @@ function m.declareGlobal(cate, name, uri)
162162
local key = cate .. '|' .. name
163163
m.globalSubs[uri][key] = true
164164
if not m.globals[key] then
165-
m.globals[key] = globalBuilder(name)
165+
m.globals[key] = globalBuilder(name, cate)
166166
end
167167
return m.globals[key]
168168
end

script/vm/global.lua

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ local util = require 'utility'
88
---@field links table<uri, vm.node.global.link>
99
---@field setsCache parser.object[]
1010
---@field getsCache parser.object[]
11+
---@field cate vm.global.cate
1112
local mt = {}
1213
mt.__index = mt
1314
mt.type = 'global'
@@ -70,10 +71,12 @@ function mt:isAlive()
7071
return next(self.links) ~= nil
7172
end
7273

74+
---@param cate vm.global.cate
7375
---@return vm.node.global
74-
return function (name)
76+
return function (name, cate)
7577
return setmetatable({
7678
name = name,
79+
cate = cate,
7780
links = util.defaultTable(function ()
7881
return {
7982
sets = {},

test/definition/bug.lua

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -262,7 +262,7 @@ TEST [[
262262
---@type B
263263
local t
264264
265-
local <!<?v?>!> = t.x
265+
t.<?x?>
266266
]]
267267

268268
TEST [[
@@ -274,7 +274,7 @@ TEST [[
274274
---@type B
275275
local t
276276
277-
local <!<?v?>!> = t.x
277+
t.<?x?>
278278
]]
279279

280280
TEST [[
@@ -286,7 +286,7 @@ function A:x() end
286286
---@class B: A
287287
local B
288288
289-
function B:<!x!>() end
289+
<!function B:x() end!>
290290
291291
---@type B
292292
local t
@@ -298,7 +298,7 @@ TEST [[
298298
---@class A
299299
local A
300300
301-
function A:<!x!>() end
301+
<!function A:x() end!>
302302
303303
---@class B: A
304304
local B

test/definition/function.lua

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,6 @@ end
2424
]]
2525

2626
TEST [[
27-
local <!f!> = function () end
27+
local <!f!> = <!function () end!>
2828
<?f?>()
2929
]]

0 commit comments

Comments
 (0)