summaryrefslogtreecommitdiff
path: root/script-beta/core/hover/description.lua
blob: 39cfb07bb7612e31a8e9f536bf42e783fe11edac (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
local vm       = require 'vm'
local ws       = require 'workspace'
local furi     = require 'file-uri'
local files    = require 'files'
local guide    = require 'parser.guide'
local markdown = require 'provider.markdown'
local config   = require 'config'
local client   = require 'provider.client'
local lang     = require 'language'
local platform = require 'bee.platform'

local function asStringInRequire(source, literal)
    local parent = source.parent
    if parent and parent.type == 'callargs' then
        local result, searchers
        local call = parent.parent
        local func = call.node
        local libName = vm.getLibraryName(func)
        if not libName then
            return
        end
        if     libName == 'require' then
            result, searchers = ws.findUrisByRequirePath(literal)
        elseif libName == 'dofile'
        or     libName == 'loadfile' then
            result = ws.findUrisByFilePath(literal)
        end
        if result and #result > 0 then
            for i, uri in ipairs(result) do
                local searcher = searchers and furi.decode(searchers[uri])
                uri = files.getOriginUri(uri)
                local path = furi.decode(uri)
                if files.eq(path:sub(1, #ws.path), ws.path) then
                    path = path:sub(#ws.path + 1)
                end
                path = path:gsub('^[/\\]*', '')
                if vm.isMetaFile(uri) then
                    result[i] = ('* [[meta]](%s)'):format(uri)
                elseif searcher then
                    searcher = searcher:sub(#ws.path + 1)
                    searcher = ws.normalize(searcher)
                    result[i] = ('* [%s](%s) %s'):format(path, uri, lang.script('HOVER_USE_LUA_PATH', searcher))
                else
                    result[i] = ('* [%s](%s)'):format(path, uri)
                end
            end
            table.sort(result)
            local md = markdown()
            md:add('md', table.concat(result, '\n'))
            return md:string()
        end
    end
end

local function asStringView(source, literal)
    -- 内部包含转义符?
    local rawLen = source.finish - source.start - 2 * #source[2] + 1
    if  config.config.hover.viewString
    and (source[2] == '"' or source[2] == "'")
    and rawLen > #literal then
        local view = literal
        local max = config.config.hover.viewStringMax
        if #view > max then
            view = view:sub(1, max) .. '...'
        end
        local md = markdown()
        md:add('txt', view)
        return md:string()
    end
end

local function asString(source)
    local literal = guide.getLiteral(source)
    if type(literal) ~= 'string' then
        return nil
    end
    return asStringInRequire(source, literal)
        or asStringView(source, literal)
end

local function getDocFormater()
    local version = config.config.runtime.version
    if client.client() == 'vscode' then
        if version == 'Lua 5.1' then
            return 'HOVER_NATIVE_DOCUMENT_LUA51'
        elseif version == 'Lua 5.2' then
            return 'HOVER_NATIVE_DOCUMENT_LUA52'
        elseif version == 'Lua 5.3' then
            return 'HOVER_NATIVE_DOCUMENT_LUA53'
        elseif version == 'Lua 5.4' then
            return 'HOVER_NATIVE_DOCUMENT_LUA54'
        elseif version == 'LuaJIT' then
            return 'HOVER_NATIVE_DOCUMENT_LUAJIT'
        end
    else
        if version == 'Lua 5.1' then
            return 'HOVER_DOCUMENT_LUA51'
        elseif version == 'Lua 5.2' then
            return 'HOVER_DOCUMENT_LUA52'
        elseif version == 'Lua 5.3' then
            return 'HOVER_DOCUMENT_LUA53'
        elseif version == 'Lua 5.4' then
            return 'HOVER_DOCUMENT_LUA54'
        elseif version == 'LuaJIT' then
            return 'HOVER_DOCUMENT_LUAJIT'
        end
    end
end

local function buildLibEnum(enum)
    local line = {}
    if enum.default then
        line[#line+1] = '  ->'
    else
        line[#line+1] = '   |'
    end
    line[#line+1] = enum.enum
    if enum.description then
        line[#line+1] = '--'
        line[#line+1] = enum.description
    end
    return table.concat(line, ' ')
end

local function getArgType(value, name)
    if not value.args then
        return ''
    end
    for _, arg in ipairs(value.args) do
        if arg.name == name then
            if type(arg.type) == 'table' then
                return ' ' .. table.concat(arg.type, '|')
            else
                return arg.type and (' ' .. arg.type) or ''
            end
        end
    end
    return ''
end

local function buildLibEnums(value)
    local results = {}
    local sorts = {}

    for _, enum in ipairs(value.enums) do
        local name = enum.name
        if not results[name] then
            results[name] = { name .. ':' .. getArgType(value, name) }
            sorts[#sorts+1] = name
        end
        results[name][#results[name]+1] = buildLibEnum(enum)
    end

    local lines = {}
    for _, name in ipairs(sorts) do
        lines[#lines+1] = table.concat(results[name], '\n')
    end
    return table.concat(lines, '\n\n')
end

local function tryLibrary(source)
    local lib = vm.getLibrary(source)
    if not lib then
        return nil
    end
    local fmt = getDocFormater()
    local md = markdown()
    if lib.value.description then
        md:add('md', lib.value.description:gsub('%(doc%:(.-)%)', function (tag)
            if fmt then
                return '(' .. lang.script(fmt, tag) .. ')'
            end
        end))
    end
    if lib.value.enums then
        md:add('md', '-------------')
        md:add('lua', buildLibEnums(lib.value))
    end
    if lib.value.doc and fmt then
        md:add('md', ('[%s](%s)'):format(lang.script.HOVER_VIEW_DOCUMENTS, lang.script(fmt, 'pdf-' .. lib.value.doc)))
    end
    return md:string()
end

local function getBindComment(source)
    local lines = {}
    for _, doc in ipairs(source.bindDocs) do
        if doc.type == 'doc.comment' then
            lines[#lines+1] = doc.comment.text:sub(2)
        elseif #lines > 0 then
            break
        end
    end
    if #lines == 0 then
        return nil
    end
    return table.concat(lines, '\n')
end

local function buildEnumChunk(docType, name)
    local enums = vm.getDocEnums(docType)
    if #enums == 0 then
        return
    end
    local types = {}
    for _, tp in ipairs(docType.types) do
        types[#types+1] = tp[1]
    end
    local lines = {}
    lines[#lines+1] = ('%s: %s'):format(name, table.concat(types))
    for _, enum in ipairs(enums) do
        lines[#lines+1] = ('   %s %s%s'):format(
               (enum.default    and '->')
            or (enum.additional and '+>')
            or ' |',
            enum[1],
            enum.comment and (' -- %s'):format(enum.comment) or ''
        )
    end
    return table.concat(lines, '\n')
end

local function getBindEnums(source)
    local mark = {}
    local chunks = {}
    local returnIndex = 0
    for _, doc in ipairs(source.bindDocs) do
        if doc.type == 'doc.param' then
            local name = doc.param[1]
            if mark[name] then
                goto CONTINUE
            end
            mark[name] = true
            chunks[#chunks+1] = buildEnumChunk(doc.extends, name)
        elseif doc.type == 'doc.return' then
            for _, rtn in ipairs(doc.returns) do
                returnIndex = returnIndex + 1
                local name = rtn.name and rtn.name[1] or ('(return %d)'):format(returnIndex)
                if mark[name] then
                    goto CONTINUE
                end
                mark[name] = true
                chunks[#chunks+1] = buildEnumChunk(rtn, name)
            end
        end
        ::CONTINUE::
    end
    if #chunks == 0 then
        return nil
    end
    return table.concat(chunks, '\n\n')
end

local function tryDocComment(source)
    if source.type == 'field'
    or source.type == 'method' then
        source = source.parent
    end
    if not source.bindDocs then
        return
    end
    local comment = getBindComment(source)
    local enums   = getBindEnums(source)
    local md = markdown()
    if comment then
        md:add('md', comment)
    end
    if enums then
        md:add('lua', enums)
    end
    return md:string()
end

local function tryDocOverloadToComment(source)
    if source.type ~= 'doc.type.function' then
        return
    end
    local doc = source.parent
    if doc.type ~= 'doc.overload'
    or not doc.bindSources then
        return
    end
    for _, src in ipairs(doc.bindSources) do
        local md = tryDocComment(src)
        if md then
            return md
        end
    end
end

return function (source)
    if source.type == 'string' then
        return asString(source)
    end
    return tryLibrary(source)
        or tryDocOverloadToComment(source)
        or tryDocComment(source)
end