summaryrefslogtreecommitdiff
path: root/server/src/core/find_source.lua
blob: a64a047e8d0777c77517e8e2fcc965f5a57947a3 (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
local function isContainPos(obj, pos)
    if obj.start <= pos and obj.finish >= pos then
        return true
    end
    return false
end

local function isValidSource(source)
    return source.start ~= nil and source.start ~= 0
end

local function matchFilter(source, filter)
    if not filter then
        return true
    end
    return filter[source.type]
end

local function findAtPos(vm, pos, filter)
    local res = {}
    vm:eachSource(function (source)
        if isValidSource(source)
        and isContainPos(source, pos)
        and matchFilter(source, filter)
        then
            res[#res+1] = source
        end
    end)
    if #res == 0 then
        return nil
    end
    table.sort(res, function (a, b)
        if a == b then
            return false
        end
        local rangeA = a.finish - a.start
        local rangeB = b.finish - b.start
        -- 特殊处理:func 'str' 的情况下,list与string的范围会完全相同,此时取string
        if rangeA == rangeB then
            if b.type == 'call' and #b == 1 and b[1] == a then
                return true
            elseif a.type == 'call' and #a == 1 and a[1] == b then
                return false
            else
                return a.id < b.id
            end
        end
        return rangeA < rangeB
    end)
    local source = res[1]
    if not source then
        return nil
    end
    return source
end

return function (vm, pos, filter)
    return findAtPos(vm, pos, filter)
end