summaryrefslogtreecommitdiff
path: root/script-beta
diff options
context:
space:
mode:
author最萌小汐 <sumneko@hotmail.com>2020-07-23 15:11:03 +0800
committer最萌小汐 <sumneko@hotmail.com>2020-07-23 15:11:03 +0800
commita27677a6d4beca0e898d691e36d94df498ffb1d1 (patch)
tree14e8d23d91fb7f4436b86534ecd0a4449a01fc73 /script-beta
parentd60ad2c5826162c480dba8f1bc810758bb090327 (diff)
downloadlua-language-server-a27677a6d4beca0e898d691e36d94df498ffb1d1.zip
getGlobals
Diffstat (limited to 'script-beta')
-rw-r--r--script-beta/files.lua81
-rw-r--r--script-beta/parser/guide.lua26
-rw-r--r--script-beta/vm/getGlobal.lua6
-rw-r--r--script-beta/vm/getGlobals.lua58
-rw-r--r--script-beta/vm/guideInterface.lua4
-rw-r--r--script-beta/vm/init.lua1
-rw-r--r--script-beta/vm/vm.lua7
7 files changed, 68 insertions, 115 deletions
diff --git a/script-beta/files.lua b/script-beta/files.lua
index 324fb11d..be890bb0 100644
--- a/script-beta/files.lua
+++ b/script-beta/files.lua
@@ -71,8 +71,9 @@ function m.setText(uri, text)
if file.text == text then
return
end
- file.text = text
+ file.text = text
file.lines = nil
+ file.cache = {}
m.globalVersion = m.globalVersion + 1
if not m.needRefreshUri then
m.needRefreshUri = {}
@@ -80,31 +81,6 @@ function m.setText(uri, text)
m.needRefreshUri[file] = true
end
---- 刷新缓存
----|必须在自动完成请求后执行,否则会影响自动完成的响应速度
-function m.refresh()
- local refreshed = m.needRefreshUri
- if not refreshed then
- return
- end
-
- local diagnostic = require 'provider.diagnostic'
- log.debug('Refresh cache.')
- m.needRefreshUri = nil
- local lastFile
- for file in pairs(refreshed) do
- lastFile = file
- file.vm = nil
- file.ast = nil
- file.globals = nil
- file.links = nil
- end
- vm.refreshCache()
-
- diagnostic.refresh(lastFile.uri)
- return true
-end
-
--- 监听编译完成
function m.onCompiled(uri, callback)
if platform.OS == 'Windows' then
@@ -148,10 +124,6 @@ function m.remove(uri)
m.globalVersion = m.globalVersion + 1
vm.refreshCache()
-
- local diagnostic = require 'provider.diagnostic'
- diagnostic.refresh(file.uri)
- diagnostic.clear(file.uri)
end
--- 移除所有文件
@@ -249,53 +221,16 @@ function m.getOriginUri(uri)
return file.uri
end
---- 寻找全局变量
-function m.findGlobals(name)
- local uris = {}
- for uri, file in pairs(m.fileMap) do
- if not file.globals then
- file.globals = {}
- local ast = m.getAst(uri)
- if ast then
- local globals = vm.getGlobals(ast.ast)
- if globals then
- for name in pairs(globals) do
- file.globals[name] = true
- end
- end
- end
- end
- if file.globals[name] then
- uris[#uris+1] = file.uri
- end
- end
- return uris
-end
-
---- 寻找link自己的其他文件
-function m.findLinkTo(uri)
+--- 获取文件的自定义缓存信息(在文件内容更新后自动失效)
+function m.getCache(uri)
if platform.OS == 'Windows' then
uri = uri:lower()
end
- local result = {}
- for _, file in pairs(m.fileMap) do
- if file.links == nil then
- local ast = m.getAst(file.uri)
- if ast then
- file.links = vm.getLinks(ast.ast)
- else
- file.links = false
- end
- end
- if file.links then
- for linkUri in pairs(file.links) do
- if m.eq(uri, linkUri) then
- result[#result+1] = file.uri
- end
- end
- end
+ local file = m.fileMap[uri]
+ if not file then
+ return nil
end
- return result
+ return file.cache
end
--- 判断文件名相等
diff --git a/script-beta/parser/guide.lua b/script-beta/parser/guide.lua
index ee5bb425..f3224e4c 100644
--- a/script-beta/parser/guide.lua
+++ b/script-beta/parser/guide.lua
@@ -1204,7 +1204,7 @@ function m.searchSameFieldsCrossMethod(status, ref, start, queue)
end
end
-function m.checkSameSimpleIncall(status, ref, start, queue)
+function m.checkSameSimpleInCall(status, ref, start, queue)
if not status.interface.call then
return
end
@@ -1224,6 +1224,26 @@ function m.checkSameSimpleIncall(status, ref, start, queue)
end
end
+function m.checkSameSimpleInGlobal(status, ref, start, queue)
+ if not status.interface.global then
+ return
+ end
+ if ref.type ~= 'setglobal' and ref.type ~= 'getglobal' then
+ return
+ end
+ local globalName = ref[1]
+ local objs = status.interface.global(globalName)
+ if objs then
+ for _, obj in ipairs(objs) do
+ queue[#queue+1] = {
+ obj = obj,
+ start = start,
+ force = true,
+ }
+ end
+ end
+end
+
function m.checkSameSimple(status, simple, data, mode, results, queue)
local ref = data.obj
local start = data.start
@@ -1242,7 +1262,9 @@ function m.checkSameSimple(status, simple, data, mode, results, queue)
-- 检查形如 a = {} 的分支情况
m.checkSameSimpleInBranch(status, ref, i, queue)
-- 检查形如 a = f() 的分支情况,需要业务层传入 interface.call
- m.checkSameSimpleIncall(status, ref, i, queue)
+ m.checkSameSimpleInCall(status, ref, i, queue)
+ -- 检查全局变量的分支情况,需要业务层传入 interface.global
+ m.checkSameSimpleInGlobal(status, ref, i, queue)
ref = m.getNextRef(ref)
if not ref then
return
diff --git a/script-beta/vm/getGlobal.lua b/script-beta/vm/getGlobal.lua
deleted file mode 100644
index 373c907e..00000000
--- a/script-beta/vm/getGlobal.lua
+++ /dev/null
@@ -1,6 +0,0 @@
-local vm = require 'vm.vm'
-
-function vm.getGlobal(source)
- vm.getGlobals(source)
- return vm.cache.getGlobal[source]
-end
diff --git a/script-beta/vm/getGlobals.lua b/script-beta/vm/getGlobals.lua
index 0ff92e3a..ec575e8e 100644
--- a/script-beta/vm/getGlobals.lua
+++ b/script-beta/vm/getGlobals.lua
@@ -1,41 +1,45 @@
local guide = require 'parser.guide'
local vm = require 'vm.vm'
+local files = require 'files'
-local function getGlobals(root)
- local env = guide.getENV(root)
- if not env then
- return nil
+local function getGlobalsOfFile(uri)
+ local globals = {}
+ local ast = files.getAst(uri)
+ if not ast then
+ return globals
end
- local cache = {}
- local fields = guide.requestFields(env)
- for _, src in ipairs(fields) do
- local name = vm.getKeyName(src)
- if not name then
- return
+ local env = guide.getENV(ast.ast)
+ local results = guide.requestFields(env)
+ for _, res in ipairs(results) do
+ local name = guide.getName(res)
+ if not globals[name] then
+ globals[name] = {}
end
- if not cache[name] then
- cache[name] = {
- key = name,
- }
+ globals[name][#globals[name]+1] = res
+ end
+ return globals
+end
+
+local function getGlobals(name)
+ local results = {}
+ for uri in files:eachFile() do
+ local cache = files.getCache(uri)
+ cache.globals = cache.globals or getGlobalsOfFile(uri)
+ if cache.globals[name] then
+ for _, source in ipairs(cache.globals[name]) do
+ results[#results+1] = source
+ end
end
- cache[name][#cache[name]+1] = src
- vm.cache.getGlobal[src] = name
end
- return cache
+ return results
end
-function vm.getGlobals(source)
- source = guide.getRoot(source)
- local cache = vm.cache.getGlobals[source]
+function vm.getGlobals(name)
+ local cache = vm.cache.getGlobals[name]
if cache ~= nil then
return cache
end
- local unlock = vm.lock('getGlobals', source)
- if not unlock then
- return nil
- end
- cache = getGlobals(source) or false
- vm.cache.getGlobals[source] = cache
- unlock()
+ cache = getGlobals(name)
+ vm.cache.getGlobals[name] = cache
return cache
end
diff --git a/script-beta/vm/guideInterface.lua b/script-beta/vm/guideInterface.lua
index e9fe4084..454390dc 100644
--- a/script-beta/vm/guideInterface.lua
+++ b/script-beta/vm/guideInterface.lua
@@ -65,3 +65,7 @@ function vm.interface.call(func, args, index)
return m.dofile(args, index)
end
end
+
+function vm.interface.global(name)
+ return vm.getGlobals(name)
+end
diff --git a/script-beta/vm/init.lua b/script-beta/vm/init.lua
index cd01e35c..ce07264d 100644
--- a/script-beta/vm/init.lua
+++ b/script-beta/vm/init.lua
@@ -1,6 +1,5 @@
local vm = require 'vm.vm'
require 'vm.getGlobals'
-require 'vm.getGlobal'
require 'vm.getLibrary'
require 'vm.getValue'
require 'vm.getClass'
diff --git a/script-beta/vm/vm.lua b/script-beta/vm/vm.lua
index c55b46bd..21c37556 100644
--- a/script-beta/vm/vm.lua
+++ b/script-beta/vm/vm.lua
@@ -152,14 +152,9 @@ function m.refreshCache()
eachDef = {},
eachField = {},
eachMeta = {},
- getGlobals = {},
- getLinks = {},
- getGlobal = {},
- specialName = {},
getLibrary = {},
getValue = {},
- getMeta = {},
- specials = nil,
+ getGlobals = {},
}
m.locked = setmetatable({}, { __mode = 'k' })
m.cacheTracker[m.cache] = true