summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--.vscode/settings.json3
-rw-r--r--server-beta/src/core/definition.lua20
-rw-r--r--server-beta/src/parser/guide.lua24
-rw-r--r--server-beta/test/definition/set.lua2
4 files changed, 37 insertions, 12 deletions
diff --git a/.vscode/settings.json b/.vscode/settings.json
index 098fc00a..79492f79 100644
--- a/.vscode/settings.json
+++ b/.vscode/settings.json
@@ -29,6 +29,5 @@
"E:/Github/test" : true,
"server/src/meta" : true
},
- "Lua.plugin.enable": true,
- "Lua.zzzzzz.cat": true
+ "Lua.plugin.enable": true
}
diff --git a/server-beta/src/core/definition.lua b/server-beta/src/core/definition.lua
index 6f6689b4..fd01072e 100644
--- a/server-beta/src/core/definition.lua
+++ b/server-beta/src/core/definition.lua
@@ -37,22 +37,24 @@ function m.aslocal(state, ast, source)
end
end
-function m.assetglobal(state, ast, source)
+function m.globals(state, ast, source)
local name = source[1]
- guide.eachSourceOf(ast.root, 'setglobal', function (src)
- if src[1] == name then
+ guide.eachGloabl(ast.root, function (src, gname)
+ if name ~= gname then
+ return
+ end
+ if src.type == 'setglobal' or src.type == 'setfield' then
state.callback(src, ast.uri)
end
end)
end
+function m.assetglobal(state, ast, source)
+ m.globals(state, ast, source)
+end
+
function m.asgetglobal(state, ast, source)
- local name = source[1]
- guide.eachSourceOf(ast.root, 'setglobal', function (src)
- if src[1] == name then
- state.callback(src, ast.uri)
- end
- end)
+ m.globals(state, ast, source)
end
return function (ast, text, offset)
diff --git a/server-beta/src/parser/guide.lua b/server-beta/src/parser/guide.lua
index bd7f9150..e6be631d 100644
--- a/server-beta/src/parser/guide.lua
+++ b/server-beta/src/parser/guide.lua
@@ -215,6 +215,30 @@ function m.eachSourceOf(root, types, callback)
end
end
+--- 遍历全局变量
+function m.eachGloabl(root, callback)
+ m.eachSourceOf(root, {'setglobal', 'getglobal', 'setfield', 'getfield'}, function (src)
+ if src.type == 'setglobal' or src.type == 'getglobal' then
+ callback(src, src[1])
+ elseif src.type == 'setfield' or src.type == 'getfield' then
+ local node = root[src.node]
+ if m.isGlobal(root, node) then
+ callback(src, src.field[1])
+ end
+ end
+ end)
+end
+
+--- 判断全局变量
+function m.isGlobal(root, obj)
+ if obj.type == 'getglobal' then
+ if obj[1] == '_G' or obj[1] == '_ENV' then
+ return true
+ end
+ end
+ return false
+end
+
--- 获取偏移对应的坐标(row从0开始,col为光标位置)
---@param lines table
---@return integer {name = 'row'}
diff --git a/server-beta/test/definition/set.lua b/server-beta/test/definition/set.lua
index 44277d33..ec582d38 100644
--- a/server-beta/test/definition/set.lua
+++ b/server-beta/test/definition/set.lua
@@ -40,7 +40,7 @@ print(<?x?>)
]]
TEST [[
-_G.<!x!> = 1
+<!_G.x!> = 1
print(<?x?>)
]]