diff options
author | 最萌小汐 <sumneko@hotmail.com> | 2020-11-20 21:57:09 +0800 |
---|---|---|
committer | 最萌小汐 <sumneko@hotmail.com> | 2020-11-20 21:57:09 +0800 |
commit | 4ca61ec457822dd14966afa0752340ae8ce180a1 (patch) | |
tree | ae8adb1ad82c717868e551e699fd3cf3bb290089 /script/vm/getClass.lua | |
parent | c63b2e404d8d2bb984afe3678a5ba2b2836380cc (diff) | |
download | lua-language-server-4ca61ec457822dd14966afa0752340ae8ce180a1.zip |
no longer beta
Diffstat (limited to 'script/vm/getClass.lua')
-rw-r--r-- | script/vm/getClass.lua | 62 |
1 files changed, 62 insertions, 0 deletions
diff --git a/script/vm/getClass.lua b/script/vm/getClass.lua new file mode 100644 index 00000000..a8bd7e40 --- /dev/null +++ b/script/vm/getClass.lua @@ -0,0 +1,62 @@ +local vm = require 'vm.vm' +local guide = require 'parser.guide' + +local function lookUpDocClass(source) + local infers = vm.getInfers(source, 'deep') + for _, infer in ipairs(infers) do + if infer.source.type == 'doc.class' + or infer.source.type == 'doc.type' then + return infer.type + end + end +end + +local function getClass(source, classes, depth, deep) + local docClass = lookUpDocClass(source) + if docClass then + classes[#classes+1] = docClass + return + end + if depth > 3 then + return + end + local value = guide.getObjectValue(source) or source + if not deep then + if value and value.type == 'string' then + classes[#classes+1] = value[1] + end + else + for _, src in ipairs(vm.getFields(value)) do + local key = vm.getKeyName(src) + if not key then + goto CONTINUE + end + local lkey = key:lower() + if lkey == 's|type' + or lkey == 's|__name' + or lkey == 's|name' + or lkey == 's|class' then + local value = guide.getObjectValue(src) + if value and value.type == 'string' then + classes[#classes+1] = value[1] + end + end + ::CONTINUE:: + end + end + if #classes ~= 0 then + return + end + vm.eachMeta(source, function (mt) + getClass(mt, classes, depth + 1, deep) + end) +end + +function vm.getClass(source, deep) + local classes = {} + getClass(source, classes, 1, deep) + if #classes == 0 then + return nil + end + return guide.mergeTypes(classes) +end |