diff options
author | 最萌小汐 <sumneko@hotmail.com> | 2021-06-23 20:57:45 +0800 |
---|---|---|
committer | 最萌小汐 <sumneko@hotmail.com> | 2021-06-23 20:57:45 +0800 |
commit | 95513e01806bffd21f5a6f3c753501496eb2533b (patch) | |
tree | abfc03fbbeba08824bf6c7bccfb4706f2b6f3cd2 /script | |
parent | 7fc2e8d182800a48e99744fc88fef24ca578b18f (diff) | |
download | lua-language-server-95513e01806bffd21f5a6f3c753501496eb2533b.zip |
supports literal table in `pairs`
Diffstat (limited to 'script')
-rw-r--r-- | script/core/generic.lua | 6 | ||||
-rw-r--r-- | script/core/noder.lua | 62 | ||||
-rw-r--r-- | script/core/searcher.lua | 26 |
3 files changed, 67 insertions, 27 deletions
diff --git a/script/core/generic.lua b/script/core/generic.lua index 15950974..5f9ad5a4 100644 --- a/script/core/generic.lua +++ b/script/core/generic.lua @@ -99,7 +99,7 @@ local function createValue(closure, proto, callback, road) end if proto.type == 'doc.type.array' then if road then - road[#road+1] = noder.ANY_FIELD + road[#road+1] = noder.WEAK_ANY_FIELD end local node = createValue(closure, proto.node, callback, road) if road then @@ -113,11 +113,11 @@ local function createValue(closure, proto, callback, road) return value end if proto.type == 'doc.type.table' then - road[#road+1] = noder.TABLE_KEY + road[#road+1] = noder.WEAK_TABLE_KEY local tkey = createValue(closure, proto.tkey, callback, road) road[#road] = nil - road[#road+1] = noder.ANY_FIELD + road[#road+1] = noder.WEAK_ANY_FIELD local tvalue = createValue(closure, proto.tvalue, callback, road) road[#road] = nil diff --git a/script/core/noder.lua b/script/core/noder.lua index 9fcdd7d9..c7963618 100644 --- a/script/core/noder.lua +++ b/script/core/noder.lua @@ -11,8 +11,10 @@ local INDEX_CHAR = '[' local RETURN_INDEX = SPLIT_CHAR .. '#' local PARAM_INDEX = SPLIT_CHAR .. '&' local TABLE_KEY = SPLIT_CHAR .. '<' +local WEAK_TABLE_KEY = SPLIT_CHAR .. '<<' local INDEX_FIELD = SPLIT_CHAR .. INDEX_CHAR local ANY_FIELD = SPLIT_CHAR .. ANY_FIELD_CHAR +local WEAK_ANY_FIELD = SPLIT_CHAR .. ANY_FIELD_CHAR .. ANY_FIELD_CHAR local URI_CHAR = '@' local URI_REGEX = URI_CHAR .. '([^' .. URI_CHAR .. ']*)' .. URI_CHAR .. '(.*)' @@ -101,6 +103,7 @@ local function getKey(source) end if index.type == 'string' or index.type == 'boolean' + or index.type == 'integer' or index.type == 'number' then return ('%q'):format(index[1] or ''), source.parent elseif index.type ~= 'function' @@ -402,15 +405,18 @@ local function pushBackward(noders, id, backwardID, tag) node.backwards[#node.backwards+1] = backwardID end +---@class noder local m = {} -m.SPLIT_CHAR = SPLIT_CHAR -m.RETURN_INDEX = RETURN_INDEX -m.PARAM_INDEX = PARAM_INDEX -m.TABLE_KEY = TABLE_KEY -m.ANY_FIELD = ANY_FIELD -m.URI_CHAR = URI_CHAR -m.INDEX_FIELD = INDEX_FIELD +m.SPLIT_CHAR = SPLIT_CHAR +m.RETURN_INDEX = RETURN_INDEX +m.PARAM_INDEX = PARAM_INDEX +m.TABLE_KEY = TABLE_KEY +m.ANY_FIELD = ANY_FIELD +m.URI_CHAR = URI_CHAR +m.INDEX_FIELD = INDEX_FIELD +m.WEAK_TABLE_KEY = WEAK_TABLE_KEY +m.WEAK_ANY_FIELD = WEAK_ANY_FIELD --- 寻找doc的主体 ---@param obj parser.guide.object @@ -892,29 +898,39 @@ function m.compileNode(noders, source) end end if source.type == 'table' then - local keyID = ('%s%s'):format( - id, - TABLE_KEY - ) - local valueID = ('%s%s'):format( - id, - ANY_FIELD - ) local firstField = source[1] if firstField then if firstField.type == 'varargs' then + local keyID = ('%s%s'):format( + id, + TABLE_KEY + ) + local valueID = ('%s%s'):format( + id, + ANY_FIELD + ) source.array = firstField pushForward(noders, keyID, 'dn:integer') pushForward(noders, valueID, getID(firstField)) - elseif firstField.type == 'tablefield' then - pushForward(noders, keyID, 'dn:string') - pushForward(noders, valueID, getID(firstField.value)) - elseif firstField.type == 'tableindex' then - pushForward(noders, keyID, getID(firstField.index)) - pushForward(noders, valueID, getID(firstField.value)) else - pushForward(noders, keyID, 'dn:integer') - pushForward(noders, valueID, getID(firstField)) + local keyID = ('%s%s'):format( + id, + WEAK_TABLE_KEY + ) + local valueID = ('%s%s'):format( + id, + WEAK_ANY_FIELD + ) + if firstField.type == 'tablefield' then + pushForward(noders, keyID, 'dn:string') + pushForward(noders, valueID, getID(firstField.value)) + elseif firstField.type == 'tableindex' then + pushForward(noders, keyID, getID(firstField.index)) + pushForward(noders, valueID, getID(firstField.value)) + else + pushForward(noders, keyID, 'dn:integer') + pushForward(noders, valueID, getID(firstField)) + end end end end diff --git a/script/core/searcher.lua b/script/core/searcher.lua index 8e984843..83682743 100644 --- a/script/core/searcher.lua +++ b/script/core/searcher.lua @@ -613,7 +613,8 @@ function m.searchRefsByID(status, uri, expect, mode) return end local originField = id:sub(#lastID + 1) - if originField == noder.TABLE_KEY then + if originField == noder.TABLE_KEY + or originField == noder.WEAK_TABLE_KEY then return end local anyFieldID = lastID .. noder.ANY_FIELD @@ -623,6 +624,28 @@ function m.searchRefsByID(status, uri, expect, mode) end end + local function checkWeak(id, field) + local lastID = noder.getLastID(id) + if not lastID then + return + end + local originField = id:sub(#lastID + 1) + if originField == noder.WEAK_TABLE_KEY then + local newID = lastID .. noder.TABLE_KEY + local newNode = noder.getNodeByID(root, newID) + if newNode then + searchNode(newID, newNode, field) + end + end + if originField == noder.WEAK_ANY_FIELD then + local newID = lastID .. noder.ANY_FIELD + local newNode = noder.getNodeByID(root, newID) + if newNode then + searchNode(newID, newNode, field) + end + end + end + local stepCount = 0 local stepMaxCount = 1e3 local statusMaxCount = 1e5 @@ -665,6 +688,7 @@ function m.searchRefsByID(status, uri, expect, mode) checkClass(id, node, field) checkLastID(id, field) checkAnyField(id, field) + checkWeak(id, field) end search(expect) |