summaryrefslogtreecommitdiff
path: root/script/parser
diff options
context:
space:
mode:
author最萌小汐 <sumneko@hotmail.com>2023-01-04 21:05:09 +0800
committer最萌小汐 <sumneko@hotmail.com>2023-01-04 21:05:09 +0800
commit2d891ce551b2fe4eaf43a5b9ba68e79df2ee68fa (patch)
tree788b304f1d15f559e7afbe3591a958e16b4187aa /script/parser
parent1befa0b9aa893ad11acb858df5e3c636b05435c0 (diff)
downloadlua-language-server-2d891ce551b2fe4eaf43a5b9ba68e79df2ee68fa.zip
cleanup
Diffstat (limited to 'script/parser')
-rw-r--r--script/parser/guide.lua108
1 files changed, 15 insertions, 93 deletions
diff --git a/script/parser/guide.lua b/script/parser/guide.lua
index 17be1e96..800fbfe7 100644
--- a/script/parser/guide.lua
+++ b/script/parser/guide.lua
@@ -1087,99 +1087,6 @@ function m.getKeyType(obj)
return m.getKeyTypeOfLiteral(obj)
end
---- 测试 a 到 b 的路径(不经过函数,不考虑 goto),
---- 每个路径是一个 block 。
----
---- 如果 a 在 b 的前面,返回 `"before"` 加上 2个`list<block>`
----
---- 如果 a 在 b 的后面,返回 `"after"` 加上 2个`list<block>`
----
---- 否则返回 `false`
----
---- 返回的2个 `list` 分别为基准block到达 a 与 b 的路径。
----@param a table
----@param b table
----@return string|false mode
----@return table? pathA
----@return table? pathB
-function m.getPath(a, b, sameFunction)
- --- 首先测试双方在同一个函数内
- if sameFunction and m.getParentFunction(a) ~= m.getParentFunction(b) then
- return false
- end
- local mode
- local objA
- local objB
- if a.finish < b.start then
- mode = 'before'
- objA = a
- objB = b
- elseif a.start > b.finish then
- mode = 'after'
- objA = b
- objB = a
- else
- return 'equal', {}, {}
- end
- local pathA = {}
- local pathB = {}
- for _ = 1, 1000 do
- objA = m.getParentBlock(objA)
- if objA then
- pathA[#pathA+1] = objA
- if (not sameFunction and objA.type == 'function') or objA.type == 'main' then
- break
- end
- end
- end
- for _ = 1, 1000 do
- objB = m.getParentBlock(objB)
- if objB then
- pathB[#pathB+1] = objB
- if (not sameFunction and objB.type == 'function') or objB.type == 'main' then
- break
- end
- end
- end
- -- pathA: {1, 2, 3, 4, 5}
- -- pathB: {5, 6, 2, 3}
- local top = #pathB
- local start
- for i = #pathA, 1, -1 do
- local currentBlock = pathA[i]
- if currentBlock == pathB[top] then
- start = i
- break
- end
- end
- if not start then
- return false
- end
- -- pathA: { 1, 2, 3}
- -- pathB: {5, 6, 2, 3}
- local extra = 0
- local align = top - start
- for i = start, 1, -1 do
- local currentA = pathA[i]
- local currentB = pathB[i+align]
- if currentA ~= currentB then
- extra = i
- break
- end
- end
- -- pathA: {1}
- local resultA = {}
- for i = extra, 1, -1 do
- resultA[#resultA+1] = pathA[i]
- end
- -- pathB: {5, 6}
- local resultB = {}
- for i = extra + align, 1, -1 do
- resultB[#resultB+1] = pathB[i]
- end
- return mode, resultA, resultB
-end
-
---是否是全局变量(包括 _G.XXX 形式)
---@param source parser.object
---@return boolean
@@ -1338,4 +1245,19 @@ function m.getFunctionSelfNode(func)
return nil
end
+---@param source parser.object
+---@return parser.object?
+function m.getTopBlock(source)
+ for _ = 1, 1000 do
+ local block = source.parent
+ if not m.isBlockType(block) then
+ return nil
+ end
+ if block.type ~= 'do' then
+ return block
+ end
+ end
+ return nil
+end
+
return m