From 86dcb1a7e5eed8cf2e1b7109b4c0debf6967c485 Mon Sep 17 00:00:00 2001 From: fesily Date: Fri, 12 May 2023 15:19:09 +0800 Subject: link server by plugin --- script/plugins/ffi/searchCode.lua | 66 +++++++++++++++++++++++++++++++++++++++ 1 file changed, 66 insertions(+) create mode 100644 script/plugins/ffi/searchCode.lua (limited to 'script/plugins/ffi/searchCode.lua') diff --git a/script/plugins/ffi/searchCode.lua b/script/plugins/ffi/searchCode.lua new file mode 100644 index 00000000..9e74e7c0 --- /dev/null +++ b/script/plugins/ffi/searchCode.lua @@ -0,0 +1,66 @@ +local vm = require 'vm' + +local function getLiterals(arg) + local literals = vm.getLiterals(arg) + local res = {} + for k, v in pairs(literals) do + if type(k) == 'string' then + res[#res+1] = k + end + end + return res +end + +---@return string[]? +local function getCode(CdefReference) + local target = CdefReference.target + if not (target.type == 'field' and target.parent.type == 'getfield') then + return + end + target = target.parent.parent + if target.type == 'call' then + return getLiterals(target.args and target.args[1]) + elseif target.type == 'local' then + local res = {} + for _, o in ipairs(target.ref) do + if o.parent.type ~= 'call' then + goto CONTINUE + end + local target = o.parent + local literals = vm.getLiterals(target.args and target.args[1]) + if not literals then + goto CONTINUE + end + for k, v in pairs(literals) do + if type(k) == 'string' then + res[#res+1] = k + end + end + ::CONTINUE:: + end + return res + end +end + +---@async +return function (CdefReference, target_uri) + if not CdefReference then + return nil + end + local codeResults + for i, v in ipairs(CdefReference) do + if v.uri ~= target_uri then + goto continue + end + local codes = getCode(v) + if not codes then + goto continue + end + for i, v in ipairs(codes) do + codeResults = codeResults or {} + codeResults[#codeResults+1] = v + end + ::continue:: + end + return codeResults +end -- cgit v1.2.3