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 searcher = require 'core.searcher'
local files = require 'files'
local vm = require 'vm'
local define = require 'proto.define'
local findSource = require 'core.find-source'
local function eachRef(source, callback)
local results = searcher.requestReference(source)
for i = 1, #results do
callback(results[i])
end
end
local function eachField(source, callback)
if not source then
return
end
local isGlobal = searcher.isGlobal(source)
local results = searcher.requestReference(source)
for i = 1, #results do
local res = results[i]
if isGlobal == searcher.isGlobal(res) then
callback(res)
end
end
end
local function eachLocal(source, callback)
callback(source)
if source.ref then
for _, ref in ipairs(source.ref) do
callback(ref)
end
end
end
local function find(source, uri, callback)
if source.type == 'local' then
eachLocal(source, callback)
elseif source.type == 'getlocal'
or source.type == 'setlocal' then
eachLocal(source.node, callback)
elseif source.type == 'field'
or source.type == 'method' then
eachField(source.parent, callback)
elseif source.type == 'getindex'
or source.type == 'setindex'
or source.type == 'tableindex' then
eachField(source, callback)
elseif source.type == 'setglobal'
or source.type == 'getglobal' then
eachField(source, callback)
elseif source.type == 'goto'
or source.type == 'label' then
eachRef(source, callback)
elseif source.type == 'string'
and source.parent
and source.parent.index == source then
eachField(source.parent, callback)
elseif source.type == 'string'
or source.type == 'boolean'
or source.type == 'number'
or source.type == 'nil' then
callback(source)
end
end
local function checkInIf(source, text, offset)
-- 检查 end
local endA = source.finish - #'end' + 1
local endB = source.finish
if offset >= endA
and offset <= endB
and text:sub(endA, endB) == 'end' then
return true
end
-- 检查每个子模块
for _, block in ipairs(source) do
for i = 1, #block.keyword, 2 do
local start = block.keyword[i]
local finish = block.keyword[i+1]
if offset >= start and offset <= finish then
return true
end
end
end
return false
end
local function makeIf(source, text, callback)
-- end
local endA = source.finish - #'end' + 1
local endB = source.finish
if text:sub(endA, endB) == 'end' then
callback(endA, endB)
end
-- 每个子模块
for _, block in ipairs(source) do
for i = 1, #block.keyword, 2 do
local start = block.keyword[i]
local finish = block.keyword[i+1]
callback(start, finish)
end
end
return false
end
local function findKeyWord(ast, text, offset, callback)
searcher.eachSourceContain(ast.ast, offset, function (source)
if source.type == 'do'
or source.type == 'function'
or source.type == 'loop'
or source.type == 'in'
or source.type == 'while'
or source.type == 'repeat' then
local ok
for i = 1, #source.keyword, 2 do
local start = source.keyword[i]
local finish = source.keyword[i+1]
if offset >= start and offset <= finish then
ok = true
break
end
end
if ok then
for i = 1, #source.keyword, 2 do
local start = source.keyword[i]
local finish = source.keyword[i+1]
callback(start, finish)
end
end
elseif source.type == 'if' then
local ok = checkInIf(source, text, offset)
if ok then
makeIf(source, text, callback)
end
end
end)
end
local accept = {
['label'] = true,
['goto'] = true,
['local'] = true,
['setlocal'] = true,
['getlocal'] = true,
['field'] = true,
['method'] = true,
['tablefield'] = true,
['setglobal'] = true,
['getglobal'] = true,
['string'] = true,
['boolean'] = true,
['number'] = true,
['nil'] = true,
}
return function (uri, offset)
local ast = files.getAst(uri)
if not ast then
return nil
end
local text = files.getText(uri)
local results = {}
local mark = {}
local source = findSource(ast, offset, accept)
if source then
find(source, uri, function (target)
if target.dummy then
return
end
local kind
if target.type == 'getfield' then
target = target.field
kind = define.DocumentHighlightKind.Read
elseif target.type == 'setfield'
or target.type == 'tablefield' then
target = target.field
kind = define.DocumentHighlightKind.Write
elseif target.type == 'getmethod' then
target = target.method
kind = define.DocumentHighlightKind.Read
elseif target.type == 'setmethod' then
target = target.method
kind = define.DocumentHighlightKind.Write
elseif target.type == 'getindex' then
target = target.index
kind = define.DocumentHighlightKind.Read
elseif target.type == 'field' then
if target.parent.type == 'getfield' then
kind = define.DocumentHighlightKind.Read
else
kind = define.DocumentHighlightKind.Write
end
elseif target.type == 'method' then
if target.parent.type == 'getmethod' then
kind = define.DocumentHighlightKind.Read
else
kind = define.DocumentHighlightKind.Write
end
elseif target.type == 'index' then
if target.parent.type == 'getindex' then
kind = define.DocumentHighlightKind.Read
else
kind = define.DocumentHighlightKind.Write
end
elseif target.type == 'index' then
if target.parent.type == 'getindex' then
kind = define.DocumentHighlightKind.Read
else
kind = define.DocumentHighlightKind.Write
end
elseif target.type == 'setindex'
or target.type == 'tableindex' then
target = target.index
kind = define.DocumentHighlightKind.Write
elseif target.type == 'getlocal'
or target.type == 'getglobal'
or target.type == 'goto' then
kind = define.DocumentHighlightKind.Read
elseif target.type == 'setlocal'
or target.type == 'local'
or target.type == 'setglobal'
or target.type == 'label' then
kind = define.DocumentHighlightKind.Write
elseif target.type == 'string'
or target.type == 'boolean'
or target.type == 'number'
or target.type == 'nil' then
kind = define.DocumentHighlightKind.Text
else
return
end
if not target then
return
end
if mark[target] then
return
end
mark[target] = true
results[#results+1] = {
start = target.start,
finish = target.finish,
kind = kind,
}
end)
end
findKeyWord(ast, text, offset, function (start, finish)
results[#results+1] = {
start = start,
finish = finish,
kind = define.DocumentHighlightKind.Write
}
end)
if #results == 0 then
return nil
end
return results
end
|