summaryrefslogtreecommitdiff
path: root/script/core/find_lib.lua
blob: e76549a8b95db683a406702ef6b3c6f302ce8649 (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
local hoverName = require 'core.hover.name'

local function getParentName(lib, isObject)
    for _, parent in ipairs(lib.parent) do
        if isObject then
            if parent.type == 'object' then
                return parent.nick or parent.name
            end
        else
            if parent.type ~= 'object' then
                return parent.nick or parent.name
            end
        end
    end
    return ''
end

local function findLib(source)
    local value = source:bindValue()
    local lib = value:getLib()
    if not lib then
        return nil
    end
    if lib.parent then
        if source:get 'object' then
            -- *string:sub
            local fullKey = ('*%s:%s'):format(getParentName(lib, true), lib.name)
            return lib, fullKey
        else
            local parentValue = source:get 'parent'
            if parentValue and parentValue:getType() == 'string' then
                -- *string.sub
                local fullKey = ('*%s.%s'):format(getParentName(lib, false), lib.name)
                return lib, fullKey
            else
                -- string.sub
                local fullKey = ('%s.%s'):format(getParentName(lib, false), lib.name)
                return lib, fullKey
            end
        end
    else
        local name = hoverName(source)
        local libName = lib.nick or lib.name
        if name == libName or not libName then
            return lib, name
        elseif name == '' then
            return lib, libName
        else
            return lib, ('%s<%s>'):format(name, libName)
        end
    end
end

return function (source)
    if source:bindValue() then
        local lib, fullKey = findLib(source)
        return lib, fullKey
    end
    if source:get 'in index' then
        source = source:get 'in index'
        local lib, fullKey = findLib(source)
        return lib, fullKey
    end
    return nil
end