summaryrefslogtreecommitdiff
path: root/script/vm/eachField.lua
blob: fabb11c7d4781d36ec2cbdfb6e72d75f731ce367 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
local vm      = require 'vm.vm'
local guide   = require 'parser.guide'
local await   = require 'await'
local config  = require 'config'

local function getFields(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
    deep = config.config.intelliSense.searchDepth + (deep or 0)

    await.delay()
    local results = guide.requestFields(source, vm.interface, deep)

    unlock()
    return results
end

local function getFieldsBySource(source, deep)
    deep = deep or -999
    local cache = vm.getCache('eachField')[source]
    if not cache or cache.deep < deep then
        cache = getFields(source, deep)
        cache.deep = deep
        vm.getCache('eachField')[source] = cache
    end
    return cache
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 getFieldsBySource(source, deep)
        vm.getCache('eachFieldOfGlobal')[name] = cache
        return cache
    else
        return getFieldsBySource(source, deep)
    end
end