summaryrefslogtreecommitdiff
path: root/test/document_symbol/init.lua
blob: 157ba3727c37f3ede5785ed8157da31e799b89ac (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
local core   = require 'core.document-symbol'
local files  = require 'files'
local define = require 'proto.define'

rawset(_G, 'TEST', true)

local EXISTS = {}

local function eq(a, b)
    if a == EXISTS and b ~= nil then
        return true
    end
    local tp1, tp2 = type(a), type(b)
    if tp1 ~= tp2 then
        return false
    end
    if tp1 == 'table' then
        local mark = {}
        for k in pairs(a) do
            if not eq(a[k], b[k]) then
                return false
            end
            mark[k] = true
        end
        for k in pairs(b) do
            if not mark[k] then
                return false
            end
        end
        return true
    end
    return a == b
end

local function checkArcoss(symbols)
    local lastFinish = 0
    for _, symbol in ipairs(symbols) do
        assert(symbol.range[1] <= symbol.selectionRange[1])
        assert(symbol.range[2] >= symbol.selectionRange[2])
        assert(symbol.range[2] > lastFinish)
        lastFinish = symbol.range[2]
        if symbol.children then
            checkArcoss(symbol.children)
        end
    end
end

---@diagnostic disable: await-in-sync
function TEST(script)
    return function (expect)
        files.setText(TESTURI, script)
        local result = core(TESTURI)
        assert(eq(expect, result))
        checkArcoss(result)
        files.remove(TESTURI)
    end
end

TEST [[
A = 1
]]
{
    [1] = {
        name = 'A',
        detail = '1',
        kind = define.SymbolKind.Number,
        range = {0, 5},
        selectionRange = {0, 1},
    }
}

TEST [[
local function f()
end
]]
{
    [1] = {
        name = 'f',
        detail = 'function ()',
        kind = define.SymbolKind.Function,
        range = {6, 10003},
        selectionRange = {15, 16},
        valueRange = {6, 10003},
    }
}

TEST [[
function f()
end
]]
{
    [1] = {
        name = 'f',
        detail = 'function ()',
        kind = define.SymbolKind.Function,
        range = {0, 10003},
        selectionRange = {9, 10},
        valueRange = {0, 10003},
    }
}

TEST [[
return function ()
end
]]
{
    [1] = {
        name = 'return',
        detail = 'function ()',
        kind = define.SymbolKind.Function,
        range = {7, 10003},
        selectionRange = {7, 15},
        valueRange = {7, 10003},
    }
}

TEST [[
f = function ()
end
]]
{
    [1] = {
        name = 'f',
        detail = 'function ()',
        kind = define.SymbolKind.Function,
        range = {0, 10003},
        selectionRange = {0, 1},
        valueRange = {4, 10003},
    }
}

TEST [[
local f = function ()
end
]]
{
    [1] = {
        name = 'f',
        detail = 'function ()',
        kind = define.SymbolKind.Function,
        range = {6, 10003},
        selectionRange = {6, 7},
        valueRange = {10, 10003},
    }
}

TEST [[
function mt:add()
end
]]
{
    [1] = {
        name = 'mt:add',
        detail = 'function ()',
        kind = define.SymbolKind.Method,
        range = {0, 10003},
        selectionRange = {9, 15},
        valueRange = {0, 10003},
    }
}

TEST [[
function A()
    function A1()
    end
    function A2()
    end
end
function B()
end
]]
{
    [1] = {
        name = 'A',
        detail = 'function ()',
        kind = define.SymbolKind.Function,
        range = {0, 50003},
        selectionRange = {9, 10},
        valueRange = {0, 50003},
        children = {
            [1] = {
                name = 'A1',
                detail = 'function ()',
                kind = define.SymbolKind.Function,
                range = {10004, 20007},
                selectionRange = {10013, 10015},
                valueRange = {10004, 20007},
            },
            [2] = {
                name = 'A2',
                detail = 'function ()',
                kind = define.SymbolKind.Function,
                range = {30004, 40007},
                selectionRange = {30013, 30015},
                valueRange = {30004, 40007},
            },
        },
    },
    [2] = {
        name = 'B',
        detail = 'function ()',
        kind = define.SymbolKind.Function,
        range = {60000, 70003},
        selectionRange = {60009, 60010},
        valueRange = {60000, 70003},
    },
}

TEST [[
local x = 1
local function f()
    local x = 'x'
    local y = {}
    --= 1
end
local y = true
local z
]]
{
    [1] = {
        name = 'x',
        detail = '1',
        kind = define.SymbolKind.Number,
        range = {6, 11},
        selectionRange = {6, 7},
    },
    [2] = {
        name = 'f',
        detail = 'function ()',
        kind = define.SymbolKind.Function,
        range = {10006, 50003},
        selectionRange = {10015, 10016},
        valueRange = {10006, 50003},
        children = {
            [1] = {
                name = 'x',
                detail = '"x"',
                kind = define.SymbolKind.String,
                range = {20010, 20017},
                selectionRange = {20010, 20011},
            },
            [2] = {
                name = 'y',
                detail = '',
                kind = define.SymbolKind.Object,
                range = {30010, 30016},
                selectionRange = {30010, 30011},
                valueRange = {30014, 30016},
            },
            --[3] = {
            --    name = 'z',
            --    detail = 'global z: number = 1',
            --    kind = define.SymbolKind.Object,
            --    range = {71, 71},
            --    selectionRange = {71, 71},
            --    valueRange = {75, 75},
            --},
        },
    },
    [3] = {
        name = 'y',
        detail = 'true',
        kind = define.SymbolKind.Boolean,
        range = {60006, 60014},
        selectionRange = {60006, 60007},
    },
    [4] = {
        name = 'z',
        detail = '',
        kind = define.SymbolKind.Variable,
        range = {70006, 70007},
        selectionRange = {70006, 70007},
    },
}

TEST [[
local t = {
    a = 1,
    b = 2,
    c = 3,
}
]]
{
    [1] = {
        name = 't',
        detail = '{a, b, c}',
        kind = define.SymbolKind.Object,
        range = {6, 40001},
        selectionRange = {6, 7},
        valueRange = {10, 40001},
        children = {
            [1] = {
                name = 'a',
                detail = '1',
                kind = define.SymbolKind.Number,
                range = {10004, 10009},
                selectionRange = {10004, 10005},
            },
            [2] = {
                name = 'b',
                detail = '2',
                kind = define.SymbolKind.Number,
                range = {20004, 20009},
                selectionRange = {20004, 20005},
            },
            [3] = {
                name = 'c',
                detail = '3',
                kind = define.SymbolKind.Number,
                range = {30004, 30009},
                selectionRange = {30004, 30005},
            },
        }
    }
}

TEST [[
local t = {
    a = 1,
    b = 2,
    c = 3,
    d = 4,
    e = 5,
    f = 6,
}
]]
{
    [1] = {
        name = 't',
        detail = '{a, b, c, d, e, ...(+1)}',
        kind = define.SymbolKind.Object,
        range = {6, 70001},
        selectionRange = {6, 7},
        valueRange = {10, 70001},
        children = EXISTS,
    }
}

TEST [[
local t = {
    a = {
        b = 1,
    }
}
]]
{
    [1] = {
        name = 't',
        detail = '{a}',
        kind = define.SymbolKind.Object,
        range = {6, 40001},
        selectionRange = {6, 7},
        valueRange = {10, 40001},
        children = {
            [1] = {
                name = 'a',
                detail = '{b}',
                kind = define.SymbolKind.Object,
                range = {10004, 30005},
                selectionRange = {10004, 10005},
                valueRange = {10008, 30005},
                children = {
                    [1] = {
                        name = 'b',
                        detail = '1',
                        kind = define.SymbolKind.Number,
                        range = {20008, 20013},
                        selectionRange = {20008, 20009},
                    }
                }
            },
        }
    }
}

TEST[[
local function g()
end

g = 1
]]{
    [1] = {
        name = 'g',
        detail = 'function ()',
        kind = define.SymbolKind.Function,
        range = {6, 10003},
        selectionRange = {15, 16},
        valueRange = {6, 10003},
    },
    [2] = {
        name = 'g',
        detail = '1',
        kind = define.SymbolKind.Number,
        range = {30000, 30005},
        selectionRange = {30000, 30001},
    }
}

TEST[[
function f(a, b, ...)
    local x = ...
    print(x.a)
end
]]{
    [1] = {
        name = 'f',
        detail = 'function (a, b, ...)',
        kind = define.SymbolKind.Function,
        range = {0, 30003},
        selectionRange = {9, 10},
        valueRange = {0, 30003},
        children = {
            [1] = {
                name = 'a',
                detail = '',
                kind = define.SymbolKind.Constant,
                range = {11, 12},
                selectionRange = {11, 12},
            },
            [2] = {
                name = 'b',
                detail = '',
                kind = define.SymbolKind.Constant,
                range = {14, 15},
                selectionRange = {14, 15},
            },
            [3] = {
                name = 'x',
                detail = '',
                kind = define.SymbolKind.Variable,
                range = {10010, 10017},
                selectionRange = {10010, 10011},
            }
        }
    },
}

TEST [[
local t = {
    a = 1,
    b = 2,
}

local v = t
]]{
    [1] = {
        name = 't',
        detail = '{a, b}',
        kind = define.SymbolKind.Object,
        range = {6, 30001},
        selectionRange = {6, 7},
        valueRange = {10, 30001},
        children = EXISTS,
    },
    [2] = {
        name = 'v',
        detail = '',
        kind = define.SymbolKind.Variable,
        range = {50006, 50011},
        selectionRange = {50006, 50007},
    },
}

TEST [[
local x
local function
]]{
    [1] = {
        name = 'x',
        detail = '',
        kind = define.SymbolKind.Variable,
        range = {6, 7},
        selectionRange = {6, 7},
    },
    [2] = {
        name = "",
        detail = "function ()",
        kind = define.SymbolKind.Function,
        range = {10006, 10014},
        selectionRange = {10006, 10014},
        valueRange = {10006, 10014},
    },
}

TEST [[
local a, b = {
    x1 = 1,
    y1 = 1,
    z1 = 1,
}, {
    x2 = 1,
    y2= 1,
    z2 = 1,
}

]]{
    [1] = {
        name = 'a',
        detail = '{x1, y1, z1}',
        kind = define.SymbolKind.Object,
        range = {6, 40001},
        selectionRange = {6, 7},
        valueRange = {13, 40001},
        children = EXISTS,
    },
    [2] = {
        name = 'b',
        detail = '{x2, y2, z2}',
        kind = define.SymbolKind.Object,
        range = {9, 80001},
        selectionRange = {9, 10},
        valueRange = {40003, 80001},
        children = EXISTS,
    }
}

TEST [[
local function x()
end

local function f()
    local c
end
]]
{
    [1] = {
        name = 'x',
        detail = 'function ()',
        kind = define.SymbolKind.Function,
        range = {6, 10003},
        selectionRange = {15, 16},
        valueRange = {6, 10003},
    },
    [2] = {
        name = 'f',
        detail = 'function ()',
        kind = define.SymbolKind.Function,
        range = {30006, 50003},
        selectionRange = {30015, 30016},
        valueRange = {30006, 50003},
        children = {
            [1] = {
                name = 'c',
                detail = '',
                kind = define.SymbolKind.Variable,
                range = {40010, 40011},
                selectionRange = {40010, 40011},
            },
        },
    }
}

TEST [[
local t = f({
    k = 1
})
]]
{
    [1] = {
        name = 't',
        detail = '',
        kind = define.SymbolKind.Variable,
        range = {6, 20002},
        selectionRange = {6, 7},
        valueRange = {10, 20002},
        children = {
            [1] = {
                name = 'f ->',
                detail = '{k}',
                kind = define.SymbolKind.Object,
                range = {12, 20001},
                selectionRange = {12, 20001},
                valueRange = {12, 20001},
                children = {
                    [1] = {
                        name = 'k',
                        detail = '1',
                        kind = define.SymbolKind.Number,
                        range = {10004, 10009},
                        selectionRange = {10004, 10005},
                    }
                }
            }
        }
    }
}

TEST [[
local t = {}

local function f(a, b)
end
]]
{
    [1] = {
        name = 't',
        detail = '',
        kind = define.SymbolKind.Object,
        range = {6, 12},
        selectionRange = {6, 7},
        valueRange = {10, 12},
    },
    [2] = {
        name = 'f',
        detail = 'function (a, b)',
        kind = define.SymbolKind.Function,
        range = {20006, 30003},
        selectionRange = {20015, 20016},
        valueRange = {20006, 30003},
        children = {
            [1] = {
                name = 'a',
                detail = '',
                kind = define.SymbolKind.Constant,
                range = {20017, 20018},
                selectionRange = {20017, 20018},
            },
            [2] = {
                name = 'b',
                detail = '',
                kind = define.SymbolKind.Constant,
                range = {20020, 20021},
                selectionRange = {20020, 20021},
            }
        }
    }
}

TEST [[
local a = f {
    x = function ()
    end
}
]]
{
    [1] = {
        name = 'a',
        detail = '',
        kind = define.SymbolKind.Variable,
        range = {6, 30001},
        selectionRange = {6, 7},
        valueRange = {10, 30001},
        children = {
            [1] = {
                name = 'f ->',
                detail = '{x}',
                kind = define.SymbolKind.Object,
                range = {12, 30001},
                selectionRange = {12, 30001},
                valueRange = {12, 30001},
                children = {
                    [1] = {
                        name = 'x',
                        detail = 'function ()',
                        kind = define.SymbolKind.Function,
                        range = {10004, 20007},
                        selectionRange = {10004, 10005},
                        valueRange = {10008, 20007},
                    }
                }
            }
        }
    }
}

TEST [[
table.sort(t, function (a, b)
    return false
end)
]]
{
    [1] = {
        name = 'table.sort ->',
        detail = 'function (a, b)',
        kind = define.SymbolKind.Function,
        range = {14, 20003},
        selectionRange = {14, 22},
        valueRange = {14, 20003},
        children = EXISTS,
    }
}

TEST [[
local root = {
    inner_function = function ()
        local function function_inside_function()
        end
    end
}
]]
{
    [1] = {
        name = 'root',
        detail = '{inner_function}',
        kind = define.SymbolKind.Object,
        range = {6, 50001},
        selectionRange = {6, 10},
        valueRange = {13, 50001},
        children = {
            [1] = {
                name = 'inner_function',
                detail = 'function ()',
                kind = define.SymbolKind.Function,
                range = {10004, 40007},
                selectionRange = {10004, 10018},
                valueRange = {10021, 40007},
                children = {
                    [1] = {
                        name = 'function_inside_function',
                        detail = 'function ()',
                        kind = define.SymbolKind.Function,
                        range = {20014, 30011},
                        selectionRange = {20023, 20047},
                        valueRange = {20014, 30011},
                    },
                },
            },
        },
    }
}

TEST [[
local t = { 1, 2, 3 }
]]
{
    [1] = {
        name = 't',
        detail = '[1, 2, 3]',
        kind = define.SymbolKind.Array,
        range = {6, 21},
        selectionRange = {6, 7},
        valueRange = {10, 21},
        children = EXISTS
    }
}

TEST [[
local t = { 1, 2, 3, 4, 5, 6 }
]]
{
    [1] = {
        name = 't',
        detail = '[1, 2, 3, 4, 5, ...(+1)]',
        kind = define.SymbolKind.Array,
        range = {6, 30},
        selectionRange = {6, 7},
        valueRange = {10, 30},
        children = EXISTS,
    }
}

TEST [[
local t = { 1, 2, [5] = 3, [true] = 4, x = 5 }
]]
{
    [1] = {
        name = 't',
        detail = '{[1], [2], [5], [true], x}',
        kind = define.SymbolKind.Object,
        range = {6, 46},
        selectionRange = {6, 7},
        valueRange = {10, 46},
        children = EXISTS
    }
}

TEST [[
if 1 then

elseif 2 then

elseif 3 then

else

end
]]
{
    {
        name   = 'if',
        detail = 'if 1 then',
        kind = define.SymbolKind.Package,
        range = {0, 20000},
        selectionRange = {0, 2},
        valueRange = {0, 20000},
    },
    {
        name   = 'elseif',
        detail = 'elseif 2 then',
        kind = define.SymbolKind.Package,
        range = {20000, 40000},
        selectionRange = {20000, 20006},
        valueRange = {20000, 40000},
    },
    {
        name   = 'elseif',
        detail = 'elseif 3 then',
        kind = define.SymbolKind.Package,
        range = {40000, 60000},
        selectionRange = {40000, 40006},
        valueRange = {40000, 60000},
    },
    {
        name   = 'else',
        detail = 'else',
        kind = define.SymbolKind.Package,
        range = {60000, 80000},
        selectionRange = {60000, 60004},
        valueRange = {60000, 80000},
    },
}

TEST [[
while true do

end
]]
{
    {
        name   = 'while',
        detail = 'while true do',
        kind = define.SymbolKind.Package,
        range = {0, 20003},
        selectionRange = {0, 5},
        valueRange = {0, 20003},
    },
}

TEST [[
repeat

until true
]]
{
    {
        name   = 'repeat',
        detail = 'until true',
        kind = define.SymbolKind.Package,
        range = {0, 20010},
        selectionRange = {0, 6},
        valueRange = {0, 20010},
    },
}

TEST [[
for i = 1, 10 do

end
]]
{
    {
        name   = 'for',
        detail = 'for i = 1, 10 do',
        kind = define.SymbolKind.Package,
        range = {0, 20003},
        selectionRange = {0, 3},
        valueRange = {0, 20003},
        children = EXISTS,
    },
}