summaryrefslogtreecommitdiff
path: root/script/vm/eachField.lua
diff options
context:
space:
mode:
author最萌小汐 <sumneko@hotmail.com>2020-11-20 21:57:09 +0800
committer最萌小汐 <sumneko@hotmail.com>2020-11-20 21:57:09 +0800
commit4ca61ec457822dd14966afa0752340ae8ce180a1 (patch)
treeae8adb1ad82c717868e551e699fd3cf3bb290089 /script/vm/eachField.lua
parentc63b2e404d8d2bb984afe3678a5ba2b2836380cc (diff)
downloadlua-language-server-4ca61ec457822dd14966afa0752340ae8ce180a1.zip
no longer beta
Diffstat (limited to 'script/vm/eachField.lua')
-rw-r--r--script/vm/eachField.lua45
1 files changed, 45 insertions, 0 deletions
diff --git a/script/vm/eachField.lua b/script/vm/eachField.lua
new file mode 100644
index 00000000..ce0e3928
--- /dev/null
+++ b/script/vm/eachField.lua
@@ -0,0 +1,45 @@
+local vm = require 'vm.vm'
+local guide = require 'parser.guide'
+local await = require 'await'
+
+local function eachField(source, deep)
+ local unlock = vm.lock('eachField', source)
+ if not unlock then
+ return {}
+ end
+
+ while source.type == 'paren' do
+ source = source.exp
+ if not source then
+ return {}
+ end
+ end
+
+ await.delay()
+ local results = guide.requestFields(source, vm.interface, deep)
+
+ unlock()
+ return results
+end
+
+function vm.getFields(source, deep)
+ if source.special == '_G' then
+ return vm.getGlobals '*'
+ end
+ if guide.isGlobal(source) then
+ local name = guide.getKeyName(source)
+ local cache = vm.getCache('eachFieldOfGlobal')[name]
+ or vm.getCache('eachField')[source]
+ or eachField(source, 'deep')
+ vm.getCache('eachFieldOfGlobal')[name] = cache
+ vm.getCache('eachField')[source] = cache
+ return cache
+ else
+ local cache = vm.getCache('eachField')[source]
+ or eachField(source, deep)
+ if deep then
+ vm.getCache('eachField')[source] = cache
+ end
+ return cache
+ end
+end