summaryrefslogtreecommitdiff
path: root/script-beta/core/hover/description.lua
blob: e21ec088678a160f187cef2b6182af8f59340f00 (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
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 function asString(source)
    local literal = guide.getLiteral(source)
    if type(literal) ~= 'string' then
        return nil
    end
    local parent = source.parent
    if parent and parent.type == 'callargs' then
        local result
        local call = parent.parent
        local func = call.node
        local lib = vm.getLibrary(func)
        if not lib then
            return
        end
        if     lib.name == 'require' then
            result = ws.findUrisByRequirePath(literal)
        elseif lib.name == 'dofile'
        or     lib.name == 'loadfile' then
            result = ws.findUrisByFilePath(literal)
        end
        if result and #result > 0 then
            for i, uri in ipairs(result) do
                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('^[/\\]*', '')
                result[i] = ('* [%s](%s)'):format(path, uri)
            end
            table.sort(result)
            return table.concat(result, '\n')
        end
    end
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('markdown', '-------------')
        md:add('markdown', 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('markdown', '-------------')
        md:add('lua', buildLibEnums(lib.value))
    end
    if lib.value.doc and fmt then
        md:add('markdown', ('[%s](%s)'):format(lang.script.HOVER_VIEW_DOCUMENTS, lang.script(fmt, 'pdf-' .. lib.value.doc)))
    end
    return md:string()
end

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