summaryrefslogtreecommitdiff
path: root/script-beta/vm/eachDef.lua
diff options
context:
space:
mode:
author最萌小汐 <sumneko@hotmail.com>2019-12-16 21:31:58 +0800
committer最萌小汐 <sumneko@hotmail.com>2019-12-16 21:31:58 +0800
commit70ae6b13b620148e954dbdf9b5563fe04d4e52b2 (patch)
tree08c13b758b2e8aaee3a32f7a61fc7ca7103f5338 /script-beta/vm/eachDef.lua
parentd65bc946566756b7627898118e467ae8bd234341 (diff)
downloadlua-language-server-70ae6b13b620148e954dbdf9b5563fe04d4e52b2.zip
整理代码
Diffstat (limited to 'script-beta/vm/eachDef.lua')
-rw-r--r--script-beta/vm/eachDef.lua147
1 files changed, 59 insertions, 88 deletions
diff --git a/script-beta/vm/eachDef.lua b/script-beta/vm/eachDef.lua
index cf865384..66420d5b 100644
--- a/script-beta/vm/eachDef.lua
+++ b/script-beta/vm/eachDef.lua
@@ -1,102 +1,73 @@
-local vm = require 'vm.vm'
-local guide = require 'parser.guide'
-local files = require 'files'
+local guide = require 'parser.guide'
+local files = require 'files'
+local vm = require 'vm.vm'
+local library = require 'library'
+local await = require 'await'
-local function checkPath(source, info)
- if source.type == 'goto' then
- return true
- end
- local src = info.source
- local mode = guide.getPath(source, src)
- if not mode then
- return true
- end
- if mode == 'before' then
- return false
- end
- if mode == 'equal' then
- if src.type == 'field'
- or src.type == 'method'
- or src.type == 'local'
- or src.type == 'setglobal' then
- return true
- else
- return false
- end
+local function ofLocal(declare, source, callback)
+
+end
+
+local function eachDef(source, callback)
+ local stype = source.type
+ if stype == 'local' then
+ ofLocal(source, source, callback)
+ elseif stype == 'getlocal'
+ or stype == 'setlocal' then
+ ofLocal(source.node, source, callback)
end
- return true
end
--- TODO
--- 只搜索本文件中的引用
--- 跨文件时,选确定入口(main的return),然后递归搜索本文件中的引用
--- 如果类型为setfield等,要确定tbl相同
-function vm.eachDef(source, callback)
- local results = {}
- local returns = {}
- local infoMap = {}
- local sourceUri = guide.getRoot(source).uri
- vm.eachRef(source, function (info)
- if info.mode == 'declare'
- or info.mode == 'set' then
- results[#results+1] = info
- end
- if info.mode == 'return' then
- results[#results+1] = info
- local root = guide.getParentBlock(info.source)
- if root.type == 'main' then
- returns[root.uri] = info
+--- 获取所有的引用
+function vm.eachDef(source, callback, max)
+ local cache = vm.cache.eachDef[source]
+ if cache then
+ await.delay(function ()
+ return files.globalVersion
+ end)
+ if max then
+ if max > #cache then
+ max = #cache
end
+ else
+ max = #cache
end
- infoMap[info.source] = info
- end)
-
- local function pushDef(info)
- local res = callback(info)
- if res ~= nil then
- return res
- end
- local value = info.source.value
- local vinfo = infoMap[value]
- if vinfo then
- res = callback(vinfo)
+ for i = 1, max do
+ local res = callback(cache[i])
+ if res ~= nil then
+ return res
+ end
end
- return res
+ return
end
-
- local res
- local used = {}
- for _, info in ipairs(results) do
+ local unlock = vm.lock('eachDef', source)
+ if not unlock then
+ return
+ end
+ cache = {}
+ vm.cache.eachDef[source] = cache
+ local mark = {}
+ eachDef(source, function (info)
local src = info.source
- local destUri
- if used[src] then
- goto CONTINUE
+ if mark[src] then
+ return
end
- used[src] = true
- destUri = guide.getRoot(src).uri
- -- 如果是同一个文件,则检查位置关系后放行
- if sourceUri == destUri then
- if checkPath(source, info) then
- res = pushDef(info)
- end
- goto CONTINUE
- end
- -- 如果是global或field,则直接放行(因为无法确定顺序)
- if src.type == 'setindex'
- or src.type == 'setfield'
- or src.type == 'setmethod'
- or src.type == 'tablefield'
- or src.type == 'tableindex'
- or src.type == 'setglobal' then
- res = pushDef(info)
- goto CONTINUE
- end
- -- 如果不是同一个文件,则必须在该文件 return 后才放行
- if returns[destUri] then
- res = pushDef(info)
- goto CONTINUE
+ mark[src] = true
+ cache[#cache+1] = info
+ end)
+ unlock()
+ await.delay(function ()
+ return files.globalVersion
+ end)
+ if max then
+ if max > #cache then
+ max = #cache
end
- ::CONTINUE::
+ else
+ max = #cache
+ end
+ for i = 1, max do
+ local res = callback(cache[i])
if res ~= nil then
return res
end