summaryrefslogtreecommitdiff
path: root/script
diff options
context:
space:
mode:
author最萌小汐 <sumneko@hotmail.com>2021-04-20 16:39:02 +0800
committer最萌小汐 <sumneko@hotmail.com>2021-04-20 16:39:02 +0800
commit1e8b20a1ca73f9bdf234c99909827eecbeca48f9 (patch)
tree7ce396a8ae8049519adb478f6ba98470cf03ad28 /script
parent9a1307a9147ef8956db9cdc37e42f057d24c3db1 (diff)
downloadlua-language-server-1e8b20a1ca73f9bdf234c99909827eecbeca48f9.zip
update
Diffstat (limited to 'script')
-rw-r--r--script/core/guide.lua4
-rw-r--r--script/core/linker.lua55
2 files changed, 55 insertions, 4 deletions
diff --git a/script/core/guide.lua b/script/core/guide.lua
index 9e75936a..ff4c2177 100644
--- a/script/core/guide.lua
+++ b/script/core/guide.lua
@@ -19,6 +19,10 @@ m.eachSourceContain = guide.eachSourceContain
m.eachSourceBetween = guide.eachSourceBetween
m.getStartFinish = guide.getStartFinish
m.isLiteral = guide.isLiteral
+m.positionOf = guide.positionOf
+m.offsetOf = guide.offsetOf
+m.lineRange = guide.lineRange
+m.lineData = guide.lineData
---@alias guide.searchmode '"ref"'|'"def"'|'"field"'
diff --git a/script/core/linker.lua b/script/core/linker.lua
index dd5e1ab7..f4f18b84 100644
--- a/script/core/linker.lua
+++ b/script/core/linker.lua
@@ -1,6 +1,29 @@
local util = require 'utility'
local guide = require 'parser.guide'
+---是否是全局变量(包括 _G.XXX 形式)
+---@param source parser.guide.object
+---@return boolean
+local function isGlobal(source)
+ if source.type == 'setglobal'
+ or source.type == 'getglobal' then
+ if source.node and source.node.tag == '_ENV' then
+ return true
+ end
+ end
+ if source.type == 'field' then
+ source = source.parent
+ end
+ if source.type == 'getfield'
+ or source.type == 'setfield' then
+ local node = source.node
+ if node and node.special == '_G' then
+ return true
+ end
+ end
+ return false
+end
+
---获取语法树单元的key
---@param source parser.guide.object
---@return string? key
@@ -61,16 +84,15 @@ local function getKey(source)
end
local function checkMode(source)
- if source.type == 'setglobal'
- or source.type == 'getglobal' then
- return 'g'
- end
if source.type == 'table' then
return 't'
end
if source.type == 'select' then
return 's'
end
+ if isGlobal(source) then
+ return 'g'
+ end
return 'l'
end
@@ -97,13 +119,27 @@ local TempList = {}
---@return parser.guide.object[]
local function checkForward(source)
local list = TempList
+ local parent = source.parent
if source.value then
+ -- x = y : x -> y
list[#list+1] = source.value
elseif source.type == 'table' then
+ -- x = {y = 1} : x -> x.y
for _, keyvalue in ipairs(source) do
list[#list+1] = keyvalue
end
end
+ -- mt:f -> self
+ if parent.type == 'setmethod'
+ and parent.node == source then
+ local func = parent.value
+ if func then
+ local self = func.locals[1]
+ if self.tag == 'self' then
+ list[#list+1] = self
+ end
+ end
+ end
if #list == 0 then
return nil
else
@@ -121,6 +157,14 @@ local function checkBackward(source)
if parent.value == source then
list[#list+1] = parent
end
+ -- self -> mt:xx
+ if source.tag == 'self' then
+ local func = guide.getParentFunction(source)
+ local setmethod = func.parent
+ if setmethod and setmethod.type == 'setmethod' then
+ list[#list+1] = setmethod.node
+ end
+ end
if #list == 0 then
return nil
else
@@ -152,6 +196,9 @@ local function getID(source)
if not node then
break
end
+ if node.special == '_G' then
+ break
+ end
current = node
end
if index == 0 then