summaryrefslogtreecommitdiff
path: root/server/src/core/completion.lua
blob: ddbe72719eafc8f34f08dc7afbc49037210bb377 (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
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
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
local findSource = require 'core.find_source'
local getFunctionHover = require 'core.hover.function'
local getFunctionHoverAsLib = require 'core.hover.lib_function'
local getFunctionHoverAsEmmy = require 'core.hover.emmy_function'
local sourceMgr = require 'vm.source'
local config = require 'config'
local matchKey = require 'core.matchKey'
local parser = require 'parser'
local lang = require 'language'
local State

local CompletionItemKind = {
    Text = 1,
    Method = 2,
    Function = 3,
    Constructor = 4,
    Field = 5,
    Variable = 6,
    Class = 7,
    Interface = 8,
    Module = 9,
    Property = 10,
    Unit = 11,
    Value = 12,
    Enum = 13,
    Keyword = 14,
    Snippet = 15,
    Color = 16,
    File = 17,
    Reference = 18,
    Folder = 19,
    EnumMember = 20,
    Constant = 21,
    Struct = 22,
    Event = 23,
    Operator = 24,
    TypeParameter = 25,
}

local KEYS = {'and', 'break', 'do', 'else', 'elseif', 'end', 'false', 'for', 'function', 'goto', 'if', 'in', 'local', 'nil', 'not', 'or', 'repeat', 'return', 'then', 'true', 'until', 'while'}
local KEYMAP = {}
for _, k in ipairs(KEYS) do
    KEYMAP[k] = true
end

local EMMY_KEYWORD = {'class', 'type', 'alias', 'param', 'return', 'field', 'generic', 'vararg', 'language', 'see', 'overload'}

local function getDucumentation(name, value)
    if value:getType() == 'function' then
        local lib = value:getLib()
        local hover
        if lib then
            hover = getFunctionHoverAsLib(name, lib)
        else
            local emmy = value:getEmmy()
            if emmy and emmy.type == 'emmy.functionType' then
                hover = getFunctionHoverAsEmmy(name, emmy)
            else
                hover = getFunctionHover(name, value:getFunction())
            end
        end
        if not hover then
            return nil
        end
        local text = ([[
```lua
%s
```
%s
```lua
%s
```
%s
]]):format(hover.label or '', hover.description or '', hover.enum or '', hover.doc or '')
        return {
            kind = 'markdown',
            value = text,
        }
    end
    local lib = value:getLib()
    if lib then
        return {
            kind = 'markdown',
            value = lib.description,
        }
    end
    local comment = value:getComment()
    if comment then
        return {
            kind = 'markdown',
            value = comment,
        }
    end
    return nil
end

local function getDetail(value)
    local literal = value:getLiteral()
    local tp = type(literal)
    local detals = {}
    if value:getType() ~= 'any' then
        detals[#detals+1] = ('(%s)'):format(value:getType())
    end
    if tp == 'boolean' then
        detals[#detals+1] = (' = %q'):format(literal)
    elseif tp == 'string' then
        detals[#detals+1] =  (' = %q'):format(literal)
    elseif tp == 'number' then
        if math.type(literal) == 'integer' then
            detals[#detals+1] =  (' = %q'):format(literal)
        else
            local str = (' = %.16f'):format(literal)
            local dot = str:find('.', 1, true)
            local suffix = str:find('[0]+$', dot + 2)
            if suffix then
                detals[#detals+1] =  str:sub(1, suffix - 1)
            else
                detals[#detals+1] =  str
            end
        end
    end
    if value:getType() == 'function' then
        ---@type emmyFunction
        local func = value:getFunction()
        local overLoads = func and func:getEmmyOverLoads()
        if overLoads then
            detals[#detals+1] = lang.script('HOVER_MULTI_PROTOTYPE', #overLoads + 1)
        end
    end
    if #detals == 0 then
        return nil
    end
    return table.concat(detals)
end

local function getKind(cata, value)
    if value:getType() == 'function' then
        local func = value:getFunction()
        if func and func:getObject() then
            return CompletionItemKind.Method
        else
            return CompletionItemKind.Function
        end
    end
    if cata == 'field' then
        local literal = value:getLiteral()
        local tp = type(literal)
        if tp == 'number' or tp == 'integer' or tp == 'string' then
            return CompletionItemKind.Enum
        end
    end
    return nil
end

local function getValueData(cata, name, value, pos, source)
    local data = {
        documentation = getDucumentation(name, value),
        detail = getDetail(value),
        kind = getKind(cata, value),
    }
    if cata == 'field' then
        if not parser:grammar(name, 'Name') then
            if source:get 'simple' and source:get 'simple' [1] ~= source then
                data.textEdit = {
                    start = pos + 1,
                    finish = pos,
                    newText = ('[%q]'):format(name),
                }
                data.additionalTextEdits = {
                    {
                        start = pos,
                        finish = pos,
                        newText = '',
                    }
                }
            else
                data.textEdit = {
                    start = pos + 1,
                    finish = pos,
                    newText = ('_ENV[%q]'):format(name),
                }
                data.additionalTextEdits = {
                    {
                        start = pos,
                        finish = pos,
                        newText = '',
                    }
                }
            end
        end
    end
    return data
end

local function searchLocals(vm, source, word, callback, pos)
    vm:eachSource(function (src)
        local loc = src:bindLocal()
        if not loc then
            return
        end

        if      src.start <= source.start
            and loc:close() >= source.finish
            and matchKey(word, loc:getName())
        then
            callback(loc:getName(), src, CompletionItemKind.Variable, getValueData('local', loc:getName(), loc:getValue(), pos, source))
        end
    end)
end

local function sortPairs(t)
    local keys = {}
    for k in pairs(t) do
        keys[#keys+1] = k
    end
    table.sort(keys)
    local i = 0
    return function ()
        i = i + 1
        local k = keys[i]
        return k, t[k]
    end
end

local function searchFieldsByInfo(parent, word, source, map)
    parent:eachInfo(function (info, src)
        local k = info[1]
        if src == source then
            return
        end
        if map[k] then
            return
        end
        if KEYMAP[k] then
            return
        end
        if info.type ~= 'set child' and info.type ~= 'get child' then
            return
        end
        if type(k) ~= 'string' then
            return
        end
        local v = parent:getChild(k)
        if not v then
            return
        end
        if source:get 'object' and v:getType() ~= 'function' then
            return
        end
        if matchKey(word, k) then
            map[k] = v
        end
    end)
end

local function searchFieldsByChild(parent, word, source, map)
    parent:eachChild(function (k, v)
        if map[k] then
            return
        end
        if KEYMAP[k] then
            return
        end
        if not v:getLib() then
            return
        end
        if type(k) ~= 'string' then
            return
        end
        if source:get 'object' and v:getType() ~= 'function' then
            return
        end
        if matchKey(word, k) then
            map[k] = v
        end
    end)
end

---@param vm VM
local function searchFields(vm, source, word, callback, pos)
    local parent = source:get 'parent' or vm.env:getValue()
    if not parent then
        return
    end
    local map = {}
    local current = parent
    for _ = 1, 3 do
        searchFieldsByInfo(current, word, source, map)
        current = current:getMetaMethod('__index')
        if not current then
            break
        end
    end
    searchFieldsByChild(parent, word, source, map)
    for k, v in sortPairs(map) do
        callback(k, nil, CompletionItemKind.Field, getValueData('field', k, v, pos, source))
    end
end

local function searchIndex(vm, source, word, callback)
    vm:eachSource(function (src)
        if src:get 'table index' then
            if matchKey(word, src[1]) then
                callback(src[1], src, CompletionItemKind.Property)
            end
        end
    end)
end

local function searchCloseGlobal(vm, start, finish, word, callback)
    vm:eachSource(function (src)
        if      (src:get 'global' or src:bindLocal())
            and src.start >= start
            and src.finish <= finish
        then
            if matchKey(word, src[1]) then
                callback(src[1], src, CompletionItemKind.Variable)
            end
        end
    end)
end

local function searchParams(vm, source, func, word, callback)
    if not func then
        return
    end
    ---@type emmyFunction
    local emmyParams = func:getEmmyParams()
    if not emmyParams then
        return
    end
    if #emmyParams > 1 then
        if not func.args
        or not func.args[1]
        or func.args[1]:getSource() == source then
            if matchKey(word, source and source[1] or '') then
                local names = {}
                for _, param in ipairs(emmyParams) do
                    local name = param:getName()
                    names[#names+1] = name
                end
                callback(table.concat(names, ', '), nil, CompletionItemKind.Snippet)
            end
        end
    end
    for _, param in ipairs(emmyParams) do
        local name = param:getName()
        if matchKey(word, name) then
            callback(name, param:getSource(), CompletionItemKind.Interface)
        end
    end
end

local function searchKeyWords(vm, source, word, callback)
    for _, key in ipairs(KEYS) do
        if matchKey(word, key) then
            callback(key, nil, CompletionItemKind.Keyword)
        end
    end
end

local function searchGlobals(vm, source, word, callback, pos)
    local global = vm.env:getValue()
    local map = {}
    local current = global
    for _ = 1, 3 do
        searchFieldsByInfo(current, word, source, map)
        current = current:getMetaMethod('__index')
        if not current then
            break
        end
    end
    searchFieldsByChild(global, word, source, map)
    for k, v in sortPairs(map) do
        callback(k, nil, CompletionItemKind.Field, getValueData('field', k, v, pos, source))
    end
end

local function searchAsGlobal(vm, source, word, callback, pos)
    if word == '' then
        return
    end
    searchLocals(vm, source, word, callback, pos)
    searchFields(vm, source, word, callback, pos)
    searchKeyWords(vm, source, word, callback)
end

local function searchAsKeyowrd(vm, source, word, callback, pos)
    searchLocals(vm, source, word, callback, pos)
    searchGlobals(vm, source, word, callback, pos)
    searchKeyWords(vm, source, word, callback)
end

local function searchAsSuffix(vm, source, word, callback, pos)
    searchFields(vm, source, word, callback, pos)
end

local function searchAsIndex(vm, source, word, callback, pos)
    searchLocals(vm, source, word, callback, pos)
    searchIndex(vm, source, word, callback)
    searchFields(vm, source, word, callback, pos)
end

local function searchAsLocal(vm, source, word, callback)
    local loc = source:bindLocal()
    if not loc then
        return
    end
    local close = loc:close()
    -- 因为闭包的关系落在局部变量finish到close范围内的全局变量一定能访问到该局部变量
    searchCloseGlobal(vm, source.finish, close, word, callback)
    -- 特殊支持 local function
    if matchKey(word, 'function') then
        callback('function', nil, CompletionItemKind.Keyword)
    end
end

local function searchAsArg(vm, source, word, callback)
    searchParams(vm, source, source:get 'arg', word, callback)

    local loc = source:bindLocal()
    if loc then
        local close = loc:close()
        -- 因为闭包的关系落在局部变量finish到close范围内的全局变量一定能访问到该局部变量
        searchCloseGlobal(vm, source.finish, close, word, callback)
        return
    end
end

local function searchFunction(vm, source, word, pos, callback)
    if pos >= source.argStart and pos <= source.argFinish then
        searchParams(vm, nil, source:bindFunction():getFunction(), word, callback)
        searchCloseGlobal(vm, source.argFinish, source.finish, word, callback)
    end
end

local function searchEmmyKeyword(vm, source, word, callback)
    for _, kw in ipairs(EMMY_KEYWORD) do
        if matchKey(word, kw) then
            callback(kw, nil, CompletionItemKind.Keyword)
        end
    end
end

local function searchEmmyClass(vm, source, word, callback)
    local classes = {}
    vm.emmyMgr:eachClass(function (class)
        if class.type == 'emmy.class' or class.type == 'emmy.alias' then
            if matchKey(word, class:getName()) then
                classes[#classes+1] = class
            end
        end
    end)
    table.sort(classes, function (a, b)
        return a:getName() < b:getName()
    end)
    for _, class in ipairs(classes) do
        callback(class:getName(), class:getSource(), CompletionItemKind.Class)
    end
end

local function searchEmmyFunctionParam(vm, source, word, callback)
    local func = source:get 'emmy function'
    if not func.args then
        return
    end
    if #func.args > 1 and matchKey(word, func.args[1].name) then
        local list = {}
        local args = {}
        for i, arg in ipairs(func.args) do
            if func:getObject() and i == 1 then
                goto NEXT
            end
            args[#args+1] = arg.name
            if #list == 0 then
                list[#list+1] = ('%s any'):format(arg.name)
            else
                list[#list+1] = ('---@param %s any'):format(arg.name)
            end
            :: NEXT ::
        end
        callback(('%s'):format(table.concat(args, ', ')), nil, CompletionItemKind.Snippet, {
            insertText = table.concat(list, '\n')
        })
    end
    for i, arg in ipairs(func.args) do
        if func:getObject() and i == 1 then
            goto NEXT
        end
        if matchKey(word, arg.name) then
            callback(arg.name, nil, CompletionItemKind.Interface)
        end
        :: NEXT ::
    end
end

local function searchSource(vm, source, word, callback, pos)
    if source.type == 'keyword' then
        searchAsKeyowrd(vm, source, word, callback, pos)
        return
    end
    if source:get 'table index' then
        searchAsIndex(vm, source, word, callback, pos)
        return
    end
    if source:get 'arg' then
        searchAsArg(vm, source, word, callback)
        return
    end
    if source:get 'global' then
        searchAsGlobal(vm, source, word, callback, pos)
        return
    end
    if source:action() == 'local' then
        searchAsLocal(vm, source, word, callback)
        return
    end
    if source:bindLocal() then
        searchAsGlobal(vm, source, word, callback, pos)
        return
    end
    if source:get 'simple'
    and (source.type == 'name' or source.type == '.' or source.type == ':') then
        searchAsSuffix(vm, source, word, callback, pos)
        return
    end
    if source:bindFunction() then
        searchFunction(vm, source, word, pos, callback)
        return
    end
    if source.type == 'emmyIncomplete' then
        searchEmmyKeyword(vm, source, word, callback)
        State.ignoreText = true
        return
    end
    if source:get 'emmy class' then
        searchEmmyClass(vm, source, word, callback)
        State.ignoreText = true
        return
    end
    if source:get 'emmy function' then
        searchEmmyFunctionParam(vm, source, word, callback)
        State.ignoreText = true
        return
    end
end

local function buildTextEdit(start, finish, str, quo)
    local text, lquo, rquo, label, filterText
    if quo == nil then
        local text = str:gsub('\r', '\\r'):gsub('\n', '\\n'):gsub('"', '\\"')
        return {
            label = '"' .. text .. '"'
        }
    end
    if quo == '"' then
        label = str
        filterText = str
        text = str:gsub('\r', '\\r'):gsub('\n', '\\n'):gsub('"', '\\"')
        lquo = quo
        rquo = quo
    elseif quo == "'" then
        label = str
        filterText = str
        text = str:gsub('\r', '\\r'):gsub('\n', '\\n'):gsub("'", "\\'")
        lquo = quo
        rquo = quo
    else
        label = str
        filterText = str
        lquo = quo
        rquo = ']' .. lquo:sub(2, -2) .. ']'
        while str:find(rquo, 1, true) do
            lquo = '[=' .. quo:sub(2)
            rquo = ']' .. lquo:sub(2, -2) .. ']'
        end
        text = str
    end
    return {
        label = label,
        filterText = filterText,
        textEdit = {
            start = start + #quo,
            finish = finish - #quo,
            newText = text,
        },
        additionalTextEdits = {
            {
                start = start,
                finish = start + #quo - 1,
                newText = lquo,
            },
            {
                start = finish - #quo + 1,
                finish = finish,
                newText = rquo,
            },
        }
    }
end

local function searchInRequire(vm, select, source, callback)
    if select ~= 1 then
        return
    end
    if not vm.lsp or not vm.lsp.workspace then
        return
    end
    if source.type ~= 'string' then
        return
    end
    local list, map = vm.lsp.workspace:matchPath(vm.uri, source[1])
    if not list then
        return
    end
    for _, str in ipairs(list) do
        local data = buildTextEdit(source.start, source.finish, str, source[2])
        data.documentation = map[str]
        callback(str, nil, CompletionItemKind.Reference, data)
    end
end

local function searchEnumAsLib(vm, source, word, callback, pos, args, lib)
    local select = #args + 1
    for i, arg in ipairs(args) do
        if arg.start <= pos and arg.finish >= pos then
            select = i
            break
        end
    end

    -- 根据参数位置找枚举值
    if lib.args and lib.enums then
        local arg = lib.args[select]
        local name = arg and arg.name
        for _, enum in ipairs(lib.enums) do
            if enum.name and enum.name == name and enum.enum then
                if matchKey(word, enum.enum) then
                    local strSource = parser:ast(tostring(enum.enum), 'String')
                    if strSource then
                        if source.type == 'string' then
                            local data = buildTextEdit(source.start, source.finish, strSource[1], source[2])
                            data.documentation = enum.description
                            callback(enum.enum, nil, CompletionItemKind.EnumMember, data)
                        else
                            callback(enum.enum, nil, CompletionItemKind.EnumMember, {
                                documentation = enum.description
                            })
                        end
                    end
                else
                    callback(enum.enum, nil, CompletionItemKind.EnumMember, {
                        documentation = enum.description
                    })
                end
            end
        end
    end

    -- 搜索特殊函数
    if lib.special == 'require' then
        searchInRequire(vm, select, source, callback)
    end
end

local function buildEmmyEnumComment(option, data)
    if not option or not option.comment then
        return data
    end
    if not data then
        data = {}
    end
    data.documentation = tostring(option.comment)
    return data
end

local function searchEnumAsEmmyParams(vm, source, word, callback, pos, args, func)
    local select = #args + 1
    for i, arg in ipairs(args) do
        if arg.start <= pos and arg.finish >= pos then
            select = i
            break
        end
    end

    local param = func:findEmmyParamByIndex(select)
    if not param then
        return
    end

    param:eachEnum(function (str, option)
        if matchKey(word, str) then
            local strSource = parser:ast(tostring(str), 'String')
            if strSource then
                if source.type == 'string' then
                    local data = buildTextEdit(source.start, source.finish, strSource[1], source[2])
                    callback(str, nil, CompletionItemKind.EnumMember, buildEmmyEnumComment(option, data))
                else
                    callback(str, nil, CompletionItemKind.EnumMember, buildEmmyEnumComment(option))
                end
            else
                callback(str, nil, CompletionItemKind.EnumMember, buildEmmyEnumComment(option))
            end
        end
    end)
end

local function getSelect(args, pos)
    if not args then
        return 1
    end
    for i, arg in ipairs(args) do
        if arg.start <= pos and arg.finish >= pos - 1 then
            return i
        end
    end
    return #args + 1
end

local function isInFunctionOrTable(call, pos)
    local args = call:bindCall()
    if not args then
        return false
    end
    local select = getSelect(args, pos)
    local arg = args[select]
    if not arg then
        return false
    end
    if arg.type == 'function' or arg.type == 'table' then
        return true
    end
    return false
end

local function searchCallArg(vm, source, word, callback, pos)
    local results = {}
    vm:eachSource(function (src)
        if      src.type == 'call'
            and src.start <= pos
            and src.finish >= pos
        then
            results[#results+1] = src
        end
    end)
    if #results == 0 then
        return nil
    end
    -- 可能处于 'func1(func2(' 的嵌套中,将最近的call放到最前面
    table.sort(results, function (a, b)
        return a.start > b.start
    end)
    local call = results[1]
    if isInFunctionOrTable(call, pos) then
        return
    end

    local args = call:bindCall()
    if not args then
        return
    end

    local value = call:findCallFunction()
    if not value then
        return
    end

    local lib = value:getLib()
    if lib then
        searchEnumAsLib(vm, source, word, callback, pos, args, lib)
        return
    end

    ---@type emmyFunction
    local func = value:getFunction()
    if func then
        searchEnumAsEmmyParams(vm, source, word, callback, pos, args, func)
        return
    end
end

local function searchAllWords(vm, source, word, callback, pos)
    if word == '' then
        return
    end
    if source.type == 'string' then
        return
    end
    vm:eachSource(function (src)
        if      src.type == 'name'
            and not (src.start <= pos and src.finish >= pos)
            and matchKey(word, src[1])
        then
            callback(src[1], src, CompletionItemKind.Text)
        end
    end)
end

local function searchSpecialHashSign(vm, pos, text, callback)
    -- 尝试 XXX[#XXX+1]
    -- 1. 搜索 []
    local index
    vm:eachSource(function (src)
        if      src.type == 'index'
            and src.start <= pos
            and src.finish >= pos
        then
            index = src
            return true
        end
    end)
    if not index then
        return nil
    end
    -- 2. [] 内部只能有一个 #
    local inside = index[1]
    if not inside then
        return nil
    end
    if inside.op ~= '#' then
        return nil
    end
    -- 3. [] 左侧必须是 simple ,且index 是 simple 的最后一项
    local simple = index:get 'simple'
    if not simple then
        return nil
    end
    if simple[#simple] ~= index then
        return nil
    end
    local chars = text:sub(simple.start, simple[#simple-1].finish)
    -- 4. 创建代码片段
    if simple:get 'as action' then
        local label = chars .. '+1'
        callback(label, nil, CompletionItemKind.Snippet, {
            textEdit = {
                start = inside.start + 1,
                finish = index.finish,
                newText = ('%s] = '):format(label),
            },
        })
    else
        local label = chars
        callback(label, nil, CompletionItemKind.Snippet, {
            textEdit = {
                start = inside.start + 1,
                finish = index.finish,
                newText = ('%s]'):format(label),
            },
        })
    end
end

local function searchSpecial(vm, source, word, callback, pos, text)
    searchSpecialHashSign(vm, pos, text, callback)
end

local function makeList(source, pos, word)
    local list = {}
    local mark = {}
    return function (name, src, kind, data)
        if src == source then
            return
        end
        if word == name then
            if src and src.start <= pos and src.finish >= pos then
                return
            end
        end
        if mark[name] then
            return
        end
        mark[name] = true
        if not data then
            data = {}
        end
        if not data.label then
            data.label = name
        end
        if not data.kind then
            data.kind = kind
        end
        list[#list+1] = data
    end, list
end

local function keywordSource(vm, word, pos)
    if not KEYMAP[word] then
        return nil
    end
    return vm:instantSource {
        type   = 'keyword',
        start  = pos,
        finish = pos + #word - 1,
        [1]    = word,
    }
end

local function findStartPos(pos, buf)
    local res = nil
    for i = pos, 1, -1 do
        local c = buf:sub(i, i)
        if c:find '[%w_]' then
            res = i
        else
            break
        end
    end
    if not res then
        for i = pos, 1, -1 do
            local c = buf:sub(i, i)
            if c == '.' or c == ':' or c == '|' or c == '(' then
                res = i
                break
            elseif c == '#' or c == '@' then
                res = i + 1
                break
            elseif c:find '[%s%c]' then
            else
                break
            end
        end
    end
    if not res then
        return pos
    end
    return res
end

local function findWord(position, text)
    local word = text
    for i = position, 1, -1 do
        local c = text:sub(i, i)
        if not c:find '[%w_]' then
            word = text:sub(i+1, position)
            break
        end
    end
    return word:match('^([%w_]*)')
end

local function getSource(vm, pos, text, filter)
    local word = findWord(pos, text)
    local source = keywordSource(vm, word, pos)
    if source then
        return source, pos, word
    end
    source = findSource(vm, pos, filter)
    if source then
        return source, pos, word
    end
    pos = findStartPos(pos, text)
    source = findSource(vm, pos, filter) or findSource(vm, pos-1, filter)
    return source, pos, word
end

return function (vm, text, pos, oldText)
    local filter = {
        ['name']           = true,
        ['string']         = true,
        ['.']              = true,
        [':']              = true,
        ['emmyName']       = true,
        ['emmyIncomplete'] = true,
        ['call']           = true,
        ['function']       = true,
        ['localfunction']  = true,
    }
    local source, pos, word = getSource(vm, pos, text, filter)
    if not source then
        return nil
    end
    if oldText then
        local oldWord = oldText:sub(source.start, source.finish)
        if word:sub(1, #oldWord) ~= oldWord then
            return nil
        end
    end
    State = {}
    local callback, list = makeList(source, pos, word)
    searchSpecial(vm, source, word, callback, pos, text)
    searchCallArg(vm, source, word, callback, pos)
    searchSource(vm, source, word, callback, pos)
    if not oldText or #list > 0 then
        if not State.ignoreText then
            searchAllWords(vm, source, word, callback, pos)
        end
    end

    if #list == 0 then
        return nil
    end

    return list
end