diff options
Diffstat (limited to 'script/plugins/ffi/searchCode.lua')
-rw-r--r-- | script/plugins/ffi/searchCode.lua | 66 |
1 files changed, 66 insertions, 0 deletions
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 |