summaryrefslogtreecommitdiff
path: root/script/vm/eachField.lua
blob: 7718f41e21feec30abe83b26de4ebe8ea22a3171 (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
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
---@type vm
local vm      = require 'vm.vm'
local searcher= require 'core.searcher'
local await   = require 'await'
local config  = require 'config'

local function getFields(source, deep, filterKey)
    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 = searcher.requestFields(source, filterKey)

    unlock()
    return results
end

local function getDefFields(source, deep, filterKey)
    local unlock = vm.lock('eachDefField', 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 = searcher.requestDefFields(source, vm.interface, deep, filterKey)

    unlock()
    return results
end

local function getFieldsBySource(source, deep, filterKey)
    deep = deep or -999
    local cache = vm.getCache('eachField')[source]
    if not cache or cache.deep < deep then
        cache = getFields(source, deep, filterKey)
        cache.deep = deep
        if not filterKey then
            vm.getCache('eachField')[source] = cache
        end
    end
    return cache
end

local function getDefFieldsBySource(source, deep, filterKey)
    deep = deep or -999
    local cache = vm.getCache('eachDefField')[source]
    if not cache or cache.deep < deep then
        cache = getDefFields(source, deep, filterKey)
        cache.deep = deep
        if not filterKey then
            vm.getCache('eachDefField')[source] = cache
        end
    end
    return cache
end

function vm.getFields(source, deep)
    if source.special == '_G' then
        return vm.getGlobals '*'
    end
    if searcher.isGlobal(source) then
        local name = searcher.getKeyName(source)
        if not name then
            return {}
        end
        local cache =  vm.getCache('eachFieldOfGlobal')[name]
                    or getFieldsBySource(source, deep)
        vm.getCache('eachFieldOfGlobal')[name] = cache
        return cache
    else
        return getFieldsBySource(source, deep)
    end
end

function vm.getDefFields(source, deep)
    if source.special == '_G' then
        return vm.getGlobalSets '*'
    end
    if searcher.isGlobal(source) then
        local name = searcher.getKeyName(source)
        if not name then
            return {}
        end
        local cache =  vm.getCache('eachDefFieldOfGlobal')[name]
                    or getDefFieldsBySource(source, deep)
        vm.getCache('eachDefFieldOfGlobal')[name] = cache
        return cache
    else
        return getDefFieldsBySource(source, deep)
    end
end