summaryrefslogtreecommitdiff
path: root/script-beta/vm/getGlobals.lua
blob: 4fe95ee931e00ed434ef97aebaf4b8f0c919243f (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
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
local guide   = require 'parser.guide'
local vm      = require 'vm.vm'
local files   = require 'files'
local library = require 'library'
local util    = require 'utility'
local config  = require 'config'

local function getGlobalsOfFile(uri)
    local cache = files.getCache(uri)
    if cache.globals then
        return cache.globals
    end
    local globals = {}
    cache.globals = globals
    local ast = files.getAst(uri)
    if not ast then
        return globals
    end
    local results = guide.findGlobals(ast.ast)
    local mark = {}
    for _, res in ipairs(results) do
        if mark[res] then
            goto CONTINUE
        end
        mark[res] = true
        local name = guide.getSimpleName(res)
        if name then
            if not globals[name] then
                globals[name] = {}
            end
            globals[name][#globals[name]+1] = res
        end
        ::CONTINUE::
    end
    return globals
end

local function getGlobalSetsOfFile(uri)
    local cache = files.getCache(uri)
    if cache.globalSets then
        return cache.globalSets
    end
    local globals = {}
    cache.globalSets = globals
    local ast = files.getAst(uri)
    if not ast then
        return globals
    end
    local results = guide.findGlobals(ast.ast)
    local mark = {}
    for _, res in ipairs(results) do
        if mark[res] then
            goto CONTINUE
        end
        mark[res] = true
        if vm.isSet(res) then
            local name = guide.getSimpleName(res)
            if name then
                if not globals[name] then
                    globals[name] = {}
                end
                globals[name][#globals[name]+1] = res
            end
        end
        ::CONTINUE::
    end
    return globals
end

local function insertLibrary(results, name)
    if name:sub(1, 2) == 's|' then
        local libname = name:sub(3)
        results[#results+1] = library.global[libname]
        local asName = config.config.runtime.special[libname]
        results[#results+1] = library.global[asName]
    elseif name == '*' then
        for _, lib in pairs(library.global) do
            results[#results+1] = lib
        end
    end
end

local function getGlobals(name)
    local results = {}
    for uri in files.eachFile() do
        local globals = getGlobalsOfFile(uri)
        if name == '*' then
            for _, sources in util.sortPairs(globals) do
                for _, source in ipairs(sources) do
                    results[#results+1] = source
                end
            end
        else
            if globals[name] then
                for _, source in ipairs(globals[name]) do
                    results[#results+1] = source
                end
            end
        end
    end
    insertLibrary(results, name)
    return results
end

local function getGlobalSets(name)
    local results = {}
    for uri in files.eachFile() do
        local globals = getGlobalSetsOfFile(uri)
        if name == '*' then
            for _, sources in util.sortPairs(globals) do
                for _, source in ipairs(sources) do
                    results[#results+1] = source
                end
            end
        else
            if globals[name] then
                for _, source in ipairs(globals[name]) do
                    results[#results+1] = source
                end
            end
        end
    end
    insertLibrary(results, name)
    return results
end

local function fastGetAnyGlobals()
    local results = {}
    local mark = {}
    for uri in files.eachFile() do
        --local globalSets = getGlobalsOfFile(uri)
        --for destName, sources in util.sortPairs(globalSets) do
        --    if not mark[destName] then
        --        mark[destName] = true
        --        results[#results+1] = sources[1]
        --    end
        --end
        local globals = getGlobalsOfFile(uri)
        for destName, sources in util.sortPairs(globals) do
            if not mark[destName] then
                mark[destName] = true
                results[#results+1] = sources[1]
            end
        end
    end
    insertLibrary(results, '*')
    return results
end

local function fastGetAnyGlobalSets()
    local results = {}
    local mark = {}
    for uri in files.eachFile() do
        local globals = getGlobalSetsOfFile(uri)
        for destName, sources in util.sortPairs(globals) do
            if not mark[destName] then
                mark[destName] = true
                results[#results+1] = sources[1]
            end
        end
    end
    insertLibrary(results, '*')
    return results
end

function vm.getGlobals(key)
    if key == '*' and config.config.intelliSense.fastGlobal then
        local cache = vm.getCache('fastGetAnyGlobals')[key]
        if cache ~= nil then
            return cache
        end
        cache = fastGetAnyGlobals()
        vm.getCache('fastGetAnyGlobals')[key] = cache
        return cache
    else
        local cache = vm.getCache('getGlobals')[key]
        if cache ~= nil then
            return cache
        end
        cache = getGlobals(key)
        vm.getCache('getGlobals')[key] = cache
        return cache
    end
end

function vm.getGlobalSets(key)
    if key == '*' and config.config.intelliSense.fastGlobal then
        local cache = vm.getCache('fastGetAnyGlobalSets')[key]
        if cache ~= nil then
            return cache
        end
        cache = fastGetAnyGlobalSets()
        vm.getCache('fastGetAnyGlobalSets')[key] = cache
        return cache
    end
    local cache = vm.getCache('getGlobalSets')[key]
    if cache ~= nil then
        return cache
    end
    cache = getGlobalSets(key)
    vm.getCache('getGlobalSets')[key] = cache
    return cache
end

files.watch(function (ev, uri)
    if ev == 'update' then
        getGlobalsOfFile(uri)
        getGlobalSetsOfFile(uri)
    end
end)