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
|
local function isContainPos(obj, pos)
return obj.start <= pos and obj.finish + 1 >= pos
end
local function findResult(results, pos)
for _, var in ipairs(results.vars) do
for _, info in ipairs(var) do
if isContainPos(info.source, pos) then
return {
type = 'var',
var = var,
}
end
end
end
for _, dots in ipairs(results.dots) do
for _, info in ipairs(dots) do
if isContainPos(info.source, pos) then
return {
type = 'dots',
dots = dots,
}
end
end
end
for _, label in ipairs(results.labels) do
for _, info in ipairs(label) do
if isContainPos(info.source, pos) then
return {
type = 'label',
label = label,
}
end
end
end
return nil
end
local function tryMeta(var)
local keys = {}
repeat
if var.childs.meta then
local metavar = var.childs.meta
for i = #keys, 1, -1 do
local key = keys[i]
metavar = metavar.childs[key]
if not metavar then
return nil
end
end
return metavar
end
keys[#keys+1] = var.key
var = var.parent
until not var
return nil
end
local function parseResult(result)
local positions = {}
local tp = result.type
if tp == 'var' then
local var = result.var
if var.type == 'local' then
local source = var.source
if not source then
return false
end
positions[1] = {source.start, source.finish}
elseif var.type == 'field' then
for _, info in ipairs(var) do
if info.type == 'set' then
positions[#positions+1] = {info.source.start, info.source.finish}
end
end
local metavar = tryMeta(var)
if metavar then
for _, info in ipairs(metavar) do
if info.type == 'set' then
positions[#positions+1] = {info.source.start, info.source.finish}
end
end
end
else
error('unknow var.type:' .. var.type)
end
elseif tp == 'dots' then
local dots = result.dots
for _, info in ipairs(dots) do
if info.type == 'local' then
positions[#positions+1] = {info.source.start, info.source.finish}
end
end
elseif tp == 'label' then
local label = result.label
for _, info in ipairs(label) do
if info.type == 'set' then
positions[#positions+1] = {info.source.start, info.source.finish}
end
end
else
error('unknow result.type:' .. result.type)
end
return positions
end
return function (results, pos)
local result = findResult(results, pos)
if not result then
return nil
end
local positions = parseResult(result)
return positions
end
|