blob: 9f998181d7ee78d6b6c3a61b2eea7ce47395b2a9 (
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
|
local union = require 'vm.union'
local files = require 'files'
---@alias vm.node vm.union
---@alias vm.object parser.object | vm.global | vm.generic
---@class vm.node-manager
local m = {}
local DUMMY_FUNCTION = function () end
---@type table<vm.object, vm.node>
m.nodeCache = {}
---@param a vm.node
---@param b vm.node
function m.mergeNode(a, b)
if not b then
return a
end
if not a then
return b
end
return union(a, b)
end
---@param source vm.object
---@param node vm.node | vm.object
---@param cover? boolean
function m.setNode(source, node, cover)
if cover then
m.nodeCache[source] = node
return
end
if not node then
return
end
local me = m.nodeCache[source]
if not me then
if node.type == 'vm.union' then
m.nodeCache[source] = node
else
m.nodeCache[source] = union(node)
end
return
end
m.nodeCache[source] = union(me, node)
end
---@return vm.node?
function m.getNode(source)
return m.nodeCache[source]
end
---@param node vm.node?
---@return vm.node
function m.addOptional(node)
if not node or node.type ~= 'vm.union' then
node = union(node)
end
node = node:addOptional()
return node
end
---@param node vm.node?
---@return vm.union?
function m.removeOptional(node)
if not node then
return node
end
if node.type ~= 'vm.union' then
node = union(node)
end
node = node:removeOptional()
return node
end
---@return fun():vm.object
function m.eachObject(node)
if not node then
return DUMMY_FUNCTION
end
if node.type == 'vm.union' then
return node:eachNode()
end
local first = true
return function ()
if first then
first = false
return node
end
return nil
end
end
function m.clearNodeCache()
m.nodeCache = {}
end
files.watch(function (ev, uri)
if ev == 'version' then
m.clearNodeCache()
end
end)
return m
|