summaryrefslogtreecommitdiff
path: root/script/vm/getGlobals.lua
blob: bea192efb7c29c6f1e61890f9246359be23dabda (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
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
local guide   = require 'parser.guide'
local await   = require "await"
local searcher = require "core.searcher"
---@type vm
local vm      = require 'vm.vm'
local files   = require 'files'
local util    = require 'utility'
local config  = require 'config'
local ws      = require 'workspace'

local function getGlobalsOfFile(uri)
    if not files.exists(uri) then
        return {}
    end
    local cache = files.getCache(uri)
    if cache.globals then
        return cache.globals
    end
    local globals = {}
    cache.globals = globals
    tracy.ZoneBeginN 'getGlobalsOfFile'
    local results = searcher.findGlobals(uri)
    local subscribe = ws.getCache 'globalSubscribe'
    subscribe[uri] = {}
    local mark = {}
    if not globals['*'] then
        globals['*'] = {}
    end
    for _, res in ipairs(results) do
        if mark[res] then
            goto CONTINUE
        end
        mark[res] = true
        local name = guide.getKeyName(res)
        if name then
            if not globals[name] then
                globals[name] = {}
                subscribe[uri][#subscribe[uri]+1] = name
            end
            globals[name][#globals[name]+1] = res
            globals['*'][#globals['*']+1] = res
        end
        ::CONTINUE::
    end
    tracy.ZoneEnd()
    return globals
end

local function getGlobalSetsOfFile(uri)
    if not files.exists(uri) then
        return {}
    end
    local cache = files.getCache(uri)
    if cache.globalSets then
        return cache.globalSets
    end
    local globals = {}
    cache.globalSets = globals
    tracy.ZoneBeginN 'getGlobalSetsOfFile'
    local results = searcher.findGlobals(uri, 'def')
    local subscribe = ws.getCache 'globalSetsSubscribe'
    subscribe[uri] = {}
    local mark = {}
    if not globals['*'] then
        globals['*'] = {}
    end
    for _, res in ipairs(results) do
        if mark[res] then
            goto CONTINUE
        end
        mark[res] = true
        local name = guide.getKeyName(res)
        if name then
            if not globals[name] then
                globals[name] = {}
                subscribe[uri][#subscribe[uri]+1] = name
            end
            globals[name][#globals[name]+1] = res
            globals['*'][#globals['*']+1] = res
        end
        ::CONTINUE::
    end
    tracy.ZoneEnd()
    return globals
end

local function getGlobals(name)
    tracy.ZoneBeginN 'getGlobals #2'
    local results = {}
    local n = 0
    local uris = files.getAllUris()
    for i = 1, #uris do
        local globals = getGlobalsOfFile(uris[i])[name]
        if globals then
            for j = 1, #globals do
                n = n + 1
                results[n] = globals[j]
            end
        end
    end
    local dummyCache = vm.getCache 'globalDummy'
    for key in pairs(config.config.diagnostics.globals) do
        if name == '*' or name == key then
            if not dummyCache[key] then
                dummyCache[key] = {
                    type   = 'dummy',
                    start  = 0,
                    finish = 0,
                    [1]    = key
                }
            end
            n = n + 1
            results[n] = dummyCache[key]
        end
    end
    tracy.ZoneEnd()
    return results
end

local function getGlobalSets(name)
    tracy.ZoneBeginN 'getGlobalSets #2'
    local results = {}
    local n = 0
    local uris = files.getAllUris()
    for i = 1, #uris do
        local globals = getGlobalSetsOfFile(uris[i])[name]
        if globals then
            for j = 1, #globals do
                n = n + 1
                results[n] = globals[j]
            end
        end
    end
    local dummyCache = vm.getCache 'globalDummy'
    for key in pairs(config.config.diagnostics.globals) do
        if name == '*' or name == key then
            if not dummyCache[key] then
                dummyCache[key] = {
                    type   = 'dummy',
                    start  = 0,
                    finish = 0,
                    [1]    = key
                }
            end
            n = n + 1
            results[n] = dummyCache[key]
        end
    end
    tracy.ZoneEnd()
    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
    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
    return results
end

local function checkNeedUpdate()
    local getGlobalCache      = ws.getCache 'getGlobals'
    local getGlobalSetsCache  = ws.getCache 'getGlobalSets'
    local needUpdateGlobals   = ws.getCache 'needUpdateGlobals'
    local uris = {}
    for uri in pairs(needUpdateGlobals) do
        uris[#uris+1] = uri
    end
    for _, uri in ipairs(uris) do
        if needUpdateGlobals[uri] then
            needUpdateGlobals[uri] = nil
            if files.exists(uri) then
                for name in pairs(getGlobalsOfFile(uri)) do
                    getGlobalCache[name] = nil
                end
                for name in pairs(getGlobalSetsOfFile(uri)) do
                    getGlobalSetsCache[name] = nil
                end
            end
        end
    end
end

function vm.getGlobals(key)
    checkNeedUpdate()
    local cache = ws.getCache('getGlobals')[key]
    if cache ~= nil then
        return cache
    end
    cache = getGlobals(key)
    ws.getCache('getGlobals')[key] = cache
    return cache
end

function vm.getGlobalSets(key)
    checkNeedUpdate()
    local cache = ws.getCache('getGlobalSets')[key]
    if cache ~= nil then
        return cache
    end
    tracy.ZoneBeginN('getGlobalSets')
    cache = getGlobalSets(key)
    ws.getCache('getGlobalSets')[key] = cache
    tracy.ZoneEnd()
    return cache
end

files.watch(function (ev, uri)
    if ev == 'update' then
        local globalSubscribe     = ws.getCache 'globalSubscribe'
        local globalSetsSubscribe = ws.getCache 'globalSetsSubscribe'
        local getGlobalCache      = ws.getCache 'getGlobals'
        local getGlobalSetsCache  = ws.getCache 'getGlobalSets'
        local needUpdateGlobals   = ws.getCache 'needUpdateGlobals'
        uri = files.asKey(uri)
        if globalSubscribe[uri] then
            for _, name in ipairs(globalSubscribe[uri]) do
                getGlobalCache[name] = nil
                getGlobalCache['*'] = nil
            end
        end
        if globalSetsSubscribe[uri] then
            for _, name in ipairs(globalSetsSubscribe[uri]) do
                getGlobalSetsCache[name] = nil
                getGlobalSetsCache['*'] = nil
            end
        end
        needUpdateGlobals[uri] = true
    elseif ev == 'create' then
        --getGlobalsOfFile(uri)
        --getGlobalSetsOfFile(uri)
    end
end)