summaryrefslogtreecommitdiff
path: root/script/core/document-symbol.lua
blob: 2c9caafbe19968ead939f898a090de938eaf742d (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
local await    = require 'await'
local files    = require 'files'
local guide    = require 'parser.guide'
local define   = require 'proto.define'
local util     = require 'utility'
local vm       = require 'vm'

local function buildName(source, text)
    local uri          = guide.getUri(source)
    local state        = files.getState(uri)
    local startOffset  = guide.positionToOffset(state, source.start)
    if source.type == 'setmethod'
    or source.type == 'getmethod' then
        if source.method then
            local finishOffset = guide.positionToOffset(state, source.method.finish)
            return text:sub(startOffset + 1, finishOffset)
        end
    end
    if source.type == 'setfield'
    or source.type == 'tablefield'
    or source.type == 'getfield' then
        if source.field then
            local finishOffset = guide.positionToOffset(state, source.field.finish)
            return text:sub(startOffset + 1, finishOffset)
        end
    end
    if source.type == 'tableindex' then
        if source.index then
            local finishOffset = guide.positionToOffset(state, source.finish)
            return text:sub(startOffset + 1, finishOffset)
        end
    end
    if source.type == 'tableexp' then
        return ('[%d]'):format(source.tindex)
    end
    local finishOffset = guide.positionToOffset(state, source.finish)
    return text:sub(startOffset + 1, finishOffset)
end

local function buildFunctionParams(func)
    if not func.args then
        return ''
    end
    local params = {}
    for _, arg in ipairs(func.args) do
        if arg.type == 'self' then
            goto CONTINUE
        end
        if arg.type == '...' then
            params[#params+1] = '...'
        else
            params[#params+1] = arg[1] or ''
        end
        ::CONTINUE::
    end
    return table.concat(params, ', ')
end

local function buildTable(tbl)
    local uri = guide.getUri(tbl)
    local buf = {}
    for i = 1, 5 do
        local field = tbl[i]
        if not field then
            break
        end
        if  field.type == 'tablefield'
        and field.field then
            buf[#buf+1] = ('%s'):format(field.field[1])
        elseif field.type == 'tableindex'
        and    field.index then
            local view =   vm.getInfer(field.index):viewLiterals()
                        or vm.getInfer(field.index):view(uri)
            buf[#buf+1] = ('[%s]'):format(view)
        elseif field.type == 'tableexp' then
            buf[#buf+1] = ('[%s]'):format(field.tindex)
        end
    end
    if #tbl > 5 then
        buf[#buf+1] = ('...(+%d)'):format(#tbl - 5)
    end
    return table.concat(buf, ', ')
end

local function buildArray(tbl)
    local uri = guide.getUri(tbl)
    local buf = {}
    for i = 1, 5 do
        local field = tbl[i]
        if not field then
            break
        end
        buf[#buf+1] =  vm.getInfer(field):viewLiterals()
                    or vm.getInfer(field):view(uri)
    end
    if #tbl > 5 then
        buf[#buf+1] = ('...(+%d)'):format(#tbl - 5)
    end
    return table.concat(buf, ', ')
end

local function buildValue(source, text, used, symbols)
    local name = buildName(source, text)
    local range, sRange, valueRange, kind
    local details = {}
    if source.type == 'local' then
        if source.parent.type == 'funcargs' then
            kind = define.SymbolKind.Constant
        else
            kind = define.SymbolKind.Variable
        end
        range      = { source.start, source.finish }
        sRange     = { source.start, source.finish }
    elseif source.type == 'setlocal' then
        range      = { source.start, source.finish }
        sRange     = { source.start, source.finish }
    elseif source.type == 'setglobal' then
        range      = { source.start, source.finish }
        sRange     = { source.start, source.finish }
    elseif source.type == 'tablefield' then
        if not source.field then
            return
        end
        range      = { source.field.start, source.field.finish }
        sRange     = { source.field.start, source.field.finish }
    elseif source.type == 'tableindex' then
        if not source.index then
            return
        end
        range      = { source.index.start, source.index.finish }
        sRange     = { source.index.start, source.index.finish }
    elseif source.type == 'tableexp' then
        range      = { source.value.start, source.value.finish }
        sRange     = { source.value.start, source.value.finish }
    elseif source.type == 'setfield' then
        if not source.field then
            return
        end
        range      = { source.field.start, source.field.finish }
        sRange     = { source.field.start, source.field.finish }
    elseif source.type == 'setmethod' then
        if not source.method then
            return
        end
        range      = { source.method.start, source.method.finish }
        sRange     = { source.start, source.finish }
    else
        return
    end
    if source.value then
        used[source.value] = true
        local literal = source.value[1]
        if     source.value.type == 'boolean' then
            kind = define.SymbolKind.Boolean
            if literal ~= nil then
                details[#details+1] = util.viewLiteral(source.value[1])
            end
        elseif source.value.type == 'string' then
            kind = define.SymbolKind.String
            if literal ~= nil then
                details[#details+1] = util.viewLiteral(source.value[1])
            end
        elseif source.value.type == 'number'
        or     source.value.type == 'integer' then
            kind = define.SymbolKind.Number
            if literal ~= nil then
                details[#details+1] = util.viewLiteral(source.value[1])
            end
        elseif source.value.type == 'table' then
            kind = define.SymbolKind.Object
            local lastField = source.value[#source.value]
            if #source.value > 0 then
                if  lastField.type == 'tableexp'
                and lastField.tindex == #source.value then
                    -- Array
                    kind = define.SymbolKind.Array
                    details[#details+1] = '['
                    details[#details+1] = buildArray(source.value)
                    details[#details+1] = ']'
                else
                    -- Object
                    details[#details+1] = '{'
                    details[#details+1] = buildTable(source.value)
                    details[#details+1] = '}'
                end
            end
            valueRange = { source.value.start, source.value.finish }
        elseif source.value.type == 'select' then
            if source.value.vararg and source.value.vararg.type == 'call' then
                valueRange = { source.value.start, source.value.finish }
            end
        elseif source.value.type == 'function' then
            details[#details+1] = ('function (%s)'):format(buildFunctionParams(source.value))
            if source.type == 'setmethod' then
                kind = define.SymbolKind.Method
            else
                kind = define.SymbolKind.Function
            end
            valueRange = { source.value.start, source.value.finish }
            range[1]   = math.min(source.value.start, source.start)
        end
        range      = { range[1], source.value.finish }
    end
    symbols[#symbols+1] = {
        name           = name,
        detail         = table.concat(details),
        kind           = kind or define.SymbolKind.Variable,
        range          = range,
        selectionRange = sRange,
        valueRange     = valueRange,
    }
end

local function buildAnonymousFunction(source, text, used, symbols)
    if used[source] then
        return
    end
    used[source] = true
    local head = ''
    local parent = source.parent
    if     parent.type == 'return' then
        head = 'return '
    elseif parent.type == 'callargs' then
        local call = parent.parent
        local node = call.node
        head = buildName(node, text) .. ' -> '
    end
    symbols[#symbols+1] = {
        name           = '',
        detail         = ('%sfunction (%s)'):format(head, buildFunctionParams(source)),
        kind           = define.SymbolKind.Function,
        range          = { source.start, source.finish },
        selectionRange = { source.keyword[1], source.keyword[2] },
        valueRange     = { source.start, source.finish },
    }
end

local function buildSource(source, text, used, symbols)
    if     source.type == 'local'
    or     source.type == 'setlocal'
    or     source.type == 'setglobal'
    or     source.type == 'setfield'
    or     source.type == 'setmethod'
    or     source.type == 'tablefield'
    or     source.type == 'tableexp'
    or     source.type == 'tableindex' then
        buildValue(source, text, used, symbols)
    elseif source.type == 'function' then
        buildAnonymousFunction(source, text, used, symbols)
    end
end

---@async
local function makeSymbol(uri)
    local ast = files.getState(uri)
    local text = files.getText(uri)
    if not ast or not text then
        return nil
    end

    local symbols = {}
    local used = {}
    local i = 0
    ---@async
    guide.eachSource(ast.ast, function (source)
        buildSource(source, text, used, symbols)
        i = i + 1
        if i % 1000 == 0 then
            await.delay()
        end
    end)

    return symbols
end

local function packChild(symbols)
    local index = 1
    local function insertChilds(min, max)
        local list
        while true do
            local symbol = symbols[index]
            if not symbol then
                break
            end
            if symbol.selectionRange[1] < min
            or symbol.selectionRange[2] > max then
                break
            end
            if not list then
                list = {}
            end
            list[#list+1] = symbol
            index = index + 1
            if symbol.valueRange then
                symbol.children = insertChilds(symbol.valueRange[1], symbol.valueRange[2])
            end
        end
        return list
    end

    local root = insertChilds(0, math.maxinteger)
    return root
end

---@async
local function packSymbols(symbols)
    await.delay()
    table.sort(symbols, function (a, b)
        local o1 = a.valueRange and a.valueRange[1] or a.selectionRange[1]
        local o2 = b.valueRange and b.valueRange[1] or b.selectionRange[1]
        return o1 < o2
    end)
    await.delay()
    -- 处理嵌套
    return packChild(symbols)
end

---@async
return function (uri)
    local symbols = makeSymbol(uri)
    if not symbols then
        return nil
    end

    local packedSymbols = packSymbols(symbols)

    return packedSymbols
end