summaryrefslogtreecommitdiff
path: root/script-beta/core/hover/description.lua
blob: ae33e133a422dd9a83d368a514f8bc9d0fed2657 (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
local vm    = require 'vm'
local ws    = require 'workspace'
local furi  = require 'file-uri'
local files = require 'files'

local function asString(source)
    local literal = vm.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 node = call.node
        local lib = vm.getLibrary(node)
        if not lib then
            return
        end
        if     lib.name == 'require' then
            result = ws.findUrisByRequirePath(literal, true)
        elseif lib.name == 'dofile'
        or     lib.name == 'loadfile' then
            result = ws.findUrisByFilePath(literal, true)
        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

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