Skip to content

Commit ec39447

Browse files
committed
#1192 improve performance
`guide.getLocal` is too slow
1 parent 7fa53c5 commit ec39447

File tree

1 file changed

+28
-16
lines changed

1 file changed

+28
-16
lines changed

script/parser/guide.lua

Lines changed: 28 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -466,26 +466,38 @@ end
466466
---@param pos integer # 可见位置
467467
---@return parser.object?
468468
function m.getLocal(source, name, pos)
469-
local root = m.getRoot(source)
470-
local res
471-
m.eachSourceContain(root, pos, function (src)
472-
local locals = src.locals
473-
if not locals then
474-
return
469+
local block = source
470+
-- find nearest source
471+
while block.start > pos or block.finish < pos do
472+
block = block.parent
473+
if not block then
474+
return nil
475475
end
476-
for i = 1, #locals do
477-
local loc = locals[i]
478-
if loc.effect > pos then
479-
break
480-
end
481-
if loc[1] == name then
482-
if not res or res.effect < loc.effect then
483-
res = loc
476+
end
477+
m.eachSourceContain(block, pos, function (src)
478+
if blockTypes[src.type]
479+
and (src.finish - src.start) < (block.finish - src.start) then
480+
block = src
481+
end
482+
end)
483+
while block do
484+
local res
485+
if block.locals then
486+
for _, loc in ipairs(block.locals) do
487+
if loc[1] == name
488+
and loc.effect <= pos then
489+
if not res or res.effect < loc.effect then
490+
res = loc
491+
end
484492
end
485493
end
486494
end
487-
end)
488-
return res
495+
if res then
496+
return res
497+
end
498+
block = block.parent
499+
end
500+
return nil
489501
end
490502

491503
--- 获取指定区块中所有的可见局部变量名称

0 commit comments

Comments
 (0)