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
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
|
local ckind = require 'define.CompletionItemKind'
local files = require 'files'
local guide = require 'parser.guide'
local matchKey = require 'core.matchKey'
local vm = require 'vm'
local library = require 'library'
local getLabel = require 'core.hover.label'
local getName = require 'core.hover.name'
local getArg = require 'core.hover.arg'
local getDesc = require 'core.hover.description'
local getHover = require 'core.hover'
local config = require 'config'
local util = require 'utility'
local markdown = require 'provider.markdown'
local stackID = 0
local stacks = {}
local function stack(callback)
stackID = stackID + 1
stacks[stackID] = callback
return stackID
end
local function clearStack()
stacks = {}
end
local function resolveStack(id)
local callback = stacks[id]
if not callback then
return nil
end
return callback()
end
local function isSpace(char)
if char == ' '
or char == '\n'
or char == '\r'
or char == '\t' then
return true
end
return false
end
local function skipSpace(text, offset)
for i = offset, 1, -1 do
local char = text:sub(i, i)
if not isSpace(char) then
return i
end
end
return 0
end
local function findWord(text, offset)
for i = offset, 1, -1 do
if not text:sub(i, i):match '[%w_]' then
if i == offset then
return nil
end
return text:sub(i+1, offset), i+1
end
end
return text:sub(1, offset), 1
end
local function findSymbol(text, offset)
for i = offset, 1, -1 do
local char = text:sub(i, i)
if isSpace(char) then
goto CONTINUE
end
if char == '.'
or char == ':'
or char == '#' then
return char, i
else
return nil
end
::CONTINUE::
end
return nil
end
local function findAnyPos(text, offset)
for i = offset, 1, -1 do
if not isSpace(text:sub(i, i)) then
return i
end
end
return nil
end
local function findParent(ast, text, offset)
for i = offset, 1, -1 do
local char = text:sub(i, i)
if isSpace(char) then
goto CONTINUE
end
local oop
if char == '.' then
oop = false
elseif char == ':' then
oop = true
else
return nil, nil
end
local anyPos = findAnyPos(text, i-1)
if not anyPos then
return nil, nil
end
local parent = guide.eachSourceContain(ast.ast, anyPos, function (source)
if source.finish == anyPos then
return source
end
end)
if parent then
return parent, oop
end
::CONTINUE::
end
return nil, nil
end
local function buildFunctionSnip(source)
local name = getName(source):gsub('^.-[$.:]', '')
local args = vm.eachDef(source, function (info)
if info.source.type == 'function' then
local args = getArg(info.source)
if args ~= '' then
return args
end
end
end) or ''
local id = 0
args = args:gsub('[^,]+', function (arg)
id = id + 1
return arg:gsub('^(%s*)(.+)', function (sp, word)
return ('%s${%d:%s}'):format(sp, id, word)
end)
end)
return ('%s(%s)'):format(name, args)
end
local function buildDetail(source)
local types = vm.getType(source)
return types
end
local function buildDesc(source)
local hover = getHover.get(source)
local md = markdown()
md:add('lua', hover.label)
md:add('md', hover.description)
return md:string()
end
local function buildFunction(results, source, oop, data)
local snipType = config.config.completion.callSnippet
if snipType == 'Disable' or snipType == 'Both' then
results[#results+1] = data
end
if snipType == 'Both' or snipType == 'Replace' then
local snipData = util.deepCopy(data)
snipData.kind = ckind.Snippet
snipData.label = snipData.label .. '()'
snipData.insertText = buildFunctionSnip(source)
snipData.insertTextFormat = 2
snipData.id = stack(function ()
return {
detail = buildDetail(source),
description = buildDesc(source),
}
end)
results[#results+1] = snipData
end
end
local function checkLocal(ast, word, offset, results)
local locals = guide.getVisibleLocals(ast.ast, offset)
for name, source in pairs(locals) do
if matchKey(word, name) then
if vm.hasType(source, 'function') then
buildFunction(results, source, false, {
label = name,
kind = ckind.Function,
id = stack(function ()
return {
detail = buildDetail(source),
description = buildDesc(source),
}
end),
})
else
results[#results+1] = {
label = name,
kind = ckind.Variable,
id = stack(function ()
return {
detail = buildDetail(source),
description = buildDesc(source),
}
end),
}
end
end
end
end
local function isSameSource(source, pos)
return source.start <= pos and source.finish >= pos
end
local function checkField(word, start, parent, oop, results)
local used = {}
vm.eachField(parent, function (info)
local key = info.key
if not key or key:sub(1, 1) ~= 's' then
return
end
if isSameSource(info.source, start) then
return
end
local name = key:sub(3)
if used[name] then
return
end
if not matchKey(word, name) then
used[name] = true
return
end
local kind = ckind.Field
if vm.hasType(info.source, 'function') then
if oop then
kind = ckind.Method
else
kind = ckind.Function
end
used[name] = true
buildFunction(results, info.source, oop, {
label = name,
kind = kind,
id = stack(function ()
return {
detail = buildDetail(info.source),
description = buildDesc(info.source),
}
end),
})
else
if oop then
return
end
used[name] = true
local literal = vm.getLiteral(info.source)
if literal ~= nil then
kind = ckind.Enum
end
results[#results+1] = {
label = name,
kind = kind,
id = stack(function ()
return {
detail = buildDetail(info.source),
description = buildDesc(info.source),
}
end)
}
end
end)
return used
end
local function checkCommon(word, text, results)
local used = {}
for _, result in ipairs(results) do
used[result.label] = true
end
for str in text:gmatch '[%a_][%w_]*' do
if not used[str] and str ~= word then
used[str] = true
if matchKey(word, str) then
results[#results+1] = {
label = str,
kind = ckind.Text,
}
end
end
end
end
local function isInString(ast, offset)
return guide.eachSourceContain(ast.ast, offset, function (source)
if source.type == 'string' then
return true
end
end)
end
local keyWordMap = {
{'do', function (hasSpace, results)
if hasSpace then
results[#results+1] = {
label = 'do .. end',
kind = ckind.Snippet,
insertTextFormat = 2,
insertText = [[$0 end]],
}
else
results[#results+1] = {
label = 'do .. end',
kind = ckind.Snippet,
insertTextFormat = 2,
insertText = [[
do
$0
end]],
}
end
end, function (ast, start)
return guide.eachSourceContain(ast.ast, start, function (source)
if source.type == 'while'
or source.type == 'in'
or source.type == 'loop' then
for i = 1, #source.keyword do
if start == source.keyword[i] then
return true
end
end
end
end)
end},
{'and'},
{'break'},
{'else'},
{'elseif', function (hasSpace, results)
if hasSpace then
results[#results+1] = {
label = 'elseif .. then',
kind = ckind.Snippet,
insertTextFormat = 2,
insertText = [[$1 then]],
}
else
results[#results+1] = {
label = 'elseif .. then',
kind = ckind.Snippet,
insertTextFormat = 2,
insertText = [[elseif $1 then]],
}
end
end},
{'end'},
{'false'},
{'for', function (hasSpace, results)
if hasSpace then
results[#results+1] = {
label = 'for .. in',
kind = ckind.Snippet,
insertTextFormat = 2,
insertText = [[
${1:key, value} in ${2:pairs(${3:t})} do
$0
end]]
}
results[#results+1] = {
label = 'for i = ..',
kind = ckind.Snippet,
insertTextFormat = 2,
insertText = [[
${1:i} = ${2:1}, ${3:10, 1} do
$0
end]]
}
else
results[#results+1] = {
label = 'for .. in',
kind = ckind.Snippet,
insertTextFormat = 2,
insertText = [[
for ${1:key, value} in ${2:pairs(${3:t})} do
$0
end]]
}
results[#results+1] = {
label = 'for i = ..',
kind = ckind.Snippet,
insertTextFormat = 2,
insertText = [[
for ${1:i} = ${2:1}, ${3:10, 1} do
$0
end]]
}
end
end},
{'function', function (hasSpace, results)
if hasSpace then
results[#results+1] = {
label = 'function ()',
kind = ckind.Snippet,
insertTextFormat = 2,
insertText = [[
$1($2)
$0
end]]
}
else
results[#results+1] = {
label = 'function ()',
kind = ckind.Snippet,
insertTextFormat = 2,
insertText = [[
function $1($2)
$0
end]]
}
end
end},
{'goto'},
{'if', function (hasSpace, results)
if hasSpace then
results[#results+1] = {
label = 'if .. then',
kind = ckind.Snippet,
insertTextFormat = 2,
insertText = [[
$1 then
$0
end]]
}
else
results[#results+1] = {
label = 'if .. then',
kind = ckind.Snippet,
insertTextFormat = 2,
insertText = [[
if $1 then
$0
end]]
}
end
end},
{'in', function (hasSpace, results)
if hasSpace then
results[#results+1] = {
label = 'in ..',
kind = ckind.Snippet,
insertTextFormat = 2,
insertText = [[
${1:pairs(${2:t})} do
$0
end]]
}
else
results[#results+1] = {
label = 'in ..',
kind = ckind.Snippet,
insertTextFormat = 2,
insertText = [[
in ${1:pairs(${2:t})} do
$0
end]]
}
end
end},
{'local', function (hasSpace, results)
if hasSpace then
results[#results+1] = {
label = 'local function',
kind = ckind.Snippet,
insertTextFormat = 2,
insertText = [[
function $1($2)
$0
end]]
}
else
results[#results+1] = {
label = 'local function',
kind = ckind.Snippet,
insertTextFormat = 2,
insertText = [[
local function $1($2)
$0
end]]
}
end
end},
{'nil'},
{'not'},
{'or'},
{'repeat', function (hasSpace, results)
if hasSpace then
results[#results+1] = {
label = 'repeat .. until',
kind = ckind.Snippet,
insertTextFormat = 2,
insertText = [[$0 until $1]]
}
else
results[#results+1] = {
label = 'repeat .. until',
kind = ckind.Snippet,
insertTextFormat = 2,
insertText = [[
repeat
$0
until $1]]
}
end
end},
{'return', function (hasSpace, results)
if not hasSpace then
results[#results+1] = {
label = 'do return end',
kind = ckind.Snippet,
insertTextFormat = 2,
insertText = [[do return $1end]]
}
end
end},
{'then'},
{'true'},
{'until'},
{'while', function (hasSpace, results)
if hasSpace then
results[#results+1] = {
label = 'while .. do',
kind = ckind.Snippet,
insertTextFormat = 2,
insertText = [[
${1:true} do
$0
end]]
}
else
results[#results+1] = {
label = 'while .. do',
kind = ckind.Snippet,
insertTextFormat = 2,
insertText = [[
while ${1:true} do
$0
end]]
}
end
end},
}
local function checkKeyWord(ast, text, start, word, hasSpace, results)
local snipType = config.config.completion.keywordSnippet
for _, data in ipairs(keyWordMap) do
local key = data[1]
if matchKey(word, key) then
if snipType == 'Both' or snipType == 'Disable' then
if not hasSpace then
results[#results+1] = {
label = key,
kind = ckind.Keyword,
}
end
end
if snipType == 'Both' or snipType == 'Replace' then
local func = data[2]
if func then
func(hasSpace, results)
end
end
local checkStop = data[3]
if checkStop then
local stop = checkStop(ast, start)
if stop then
return true
end
end
end
end
end
local function tryWord(ast, text, offset, results)
local finish = skipSpace(text, offset)
local word, start = findWord(text, finish)
if not word then
return nil
end
local hasSpace = finish ~= offset
if not isInString(ast, offset) then
local parent, oop = findParent(ast, text, start - 1)
if parent then
if not hasSpace then
checkField(word, start, parent, oop, results)
end
else
local stop = checkKeyWord(ast, text, start, word, hasSpace, results)
if stop then
return
end
if not hasSpace then
checkLocal(ast, word, start, results)
local env = guide.getLocal(ast.ast, '_ENV', start)
checkField(word, start, env, false, results)
end
end
end
if not hasSpace then
checkCommon(word, text, results)
end
end
local function trySymbol(ast, text, offset, results)
local symbol, start = findSymbol(text, offset)
if not symbol then
return nil
end
if isInString(ast, offset) then
return nil
end
if symbol == '.'
or symbol == ':' then
local parent, oop = findParent(ast, text, start)
if parent then
checkField('', start, parent, oop, results)
end
end
end
local function completion(uri, offset)
local ast = files.getAst(uri)
if not ast then
return nil
end
clearStack()
local text = files.getText(uri)
local results = {}
tryWord(ast, text, offset, results)
trySymbol(ast, text, offset, results)
if #results == 0 then
return nil
end
return results
end
local function resolve(id)
return resolveStack(id)
end
return {
completion = completion,
resolve = resolve,
}
|