summaryrefslogtreecommitdiff
path: root/script/plugins/ffi/cdefRerence.lua
blob: 14643f0ffbf68540e995e71d37489f00746d6acd (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
local files     = require 'files'
local guide     = require 'parser.guide'
local vm        = require 'vm'
local reference = require 'core.reference'
local find      = string.find
local remove    = table.remove

local function getCdefSourcePosition(ffi_state)
    local cdef_position = ffi_state.ast.returns[1][1]
    local source = vm.getFields(cdef_position)
    for index, value in ipairs(source) do
        local name = guide.getKeyName(value)
        if name == 'cdef' then
            return value.field.start
        end
    end
end

---@async
return function ()
    local ffi_state
    for uri in files.eachFile() do
        if find(uri, "ffi.lua", 0, true) and find(uri, "lua-language-server", 0, true) then
            ffi_state = files.getState(uri)
            break
        end
    end
    if ffi_state then
        local res = reference(ffi_state.uri, getCdefSourcePosition(ffi_state), true)
        if res then
            if res[1].uri == ffi_state.uri then
                remove(res, 1)
            end
            return res
        end
    end
end