summaryrefslogtreecommitdiff
path: root/script/vm/docs.lua
blob: 95cb412733e50ee67ffc882be768e5ef88f0f7ee (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 files = require 'files'
local await = require 'await'
local noder = require 'core.noder'

local docsMap      = {}
local subscribeMap = {}

local function popDocs(uri)
    if not subscribeMap[uri] then
        return
    end
    for id in pairs(subscribeMap[uri]) do
        if docsMap[id] then
            docsMap[id][uri] = nil
        end
    end
    subscribeMap[uri] = nil
end

local function pushDocs(uri)
    subscribeMap[uri] = {}
    local state = files.getState(uri)
    if not state then
        return
    end
    local nodes = noder.compileNodes(state.ast)
    for id in pairs(nodes) do
        if id:sub(1, 3) == 'dn:' then
            if not docsMap[id] then
                docsMap[id] = {}
            end
            docsMap[id][uri] = true
            subscribeMap[uri][id] = true
        end
    end
end

files.watch(function (ev, uri)
    if ev == 'update' then
        popDocs(uri)
        pushDocs(uri)
    end
end)

local m = {}

function m.getUrisByID(id)
    return docsMap[id]
end

return m