summaryrefslogtreecommitdiff
path: root/script-beta/vm/getDocs.lua
blob: ef29dcdbe8a985853722bdb044042d59dbf6b58f (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
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
local files   = require 'files'
local util    = require 'utility'
local guide   = require 'parser.guide'
local vm      = require 'vm.vm'

local builtin = {}
for _, name in ipairs {
    'any'          ,
    'nil'          ,
    'void'         ,
    'boolean'      ,
    'number'       ,
    'integer'      ,
    'thread'       ,
    'table'        ,
    'file'         ,
    'string'       ,
    'userdata'     ,
    'lightuserdata',
    'function'     ,
} do
    builtin[#builtin+1] = {
        type   = 'doc.class.name',
        start  = 0,
        finish = 0,
        [1]    = name,
    }
end

local function getTypesOfFile(uri)
    local types = {}
    local ast = files.getAst(uri)
    if not ast or not ast.ast.docs then
        return types
    end
    guide.eachSource(ast.ast.docs, function (src)
        if src.type == 'doc.type.name'
        or src.type == 'doc.class.name'
        or src.type == 'doc.extends.name'
        or src.type == 'doc.alias.name' then
            local name = src[1]
            if name then
                if not types[name] then
                    types[name] = {}
                end
                types[name][#types[name]+1] = src
            end
        end
    end)
    return types
end

local function getDocTypes(name)
    local results = {}
    for uri in files.eachFile() do
        local cache = files.getCache(uri)
        cache.classes = cache.classes or getTypesOfFile(uri)
        if name == '*' then
            for _, sources in util.sortPairs(cache.classes) do
                for _, source in ipairs(sources) do
                    results[#results+1] = source
                end
            end
        else
            if cache.classes[name] then
                for _, source in ipairs(cache.classes[name]) do
                    results[#results+1] = source
                end
            end
        end
    end
    for _, source in ipairs(builtin) do
        if name == '*' or name == source[1] then
            results[#results+1] = source
        end
    end
    return results
end

function vm.getDocEnums(doc, mark, results)
    mark = mark or {}
    if mark[doc] then
        return nil
    end
    mark[doc] = true
    results = results or {}
    for _, enum in ipairs(doc.enums) do
        results[#results+1] = enum
    end
    for _, resume in ipairs(doc.resumes) do
        results[#results+1] = resume
    end
    for _, unit in ipairs(doc.types) do
        if unit.type == 'doc.type.name' then
            for _, other in ipairs(vm.getDocTypes(unit[1])) do
                if other.type == 'doc.alias.name' then
                    vm.getDocEnums(other.parent.extends, mark, results)
                end
            end
        end
    end
    return results
end

function vm.getDocTypes(name)
    local cache = vm.getCache('getDocTypes')[name]
    if cache ~= nil then
        return cache
    end
    cache = getDocTypes(name)
    vm.getCache('getDocTypes')[name] = cache
    return cache
end

function vm.isMetaFile(uri)
    local status = files.getAst(uri)
    if not status then
        return false
    end
    local cache = files.getCache(uri)
    if cache.isMeta ~= nil then
        return cache.isMeta
    end
    cache.isMeta = false
    if not status.ast.docs then
        return false
    end
    for _, doc in ipairs(status.ast.docs) do
        if doc.type == 'doc.meta' then
            cache.isMeta = true
            return true
        end
    end
    return false
end

function vm.getValidVersions(doc)
    if doc.type ~= 'doc.version' then
        return
    end
    local valids = {
        ['Lua 5.1'] = false,
        ['Lua 5.2'] = false,
        ['Lua 5.3'] = false,
        ['Lua 5.4'] = false,
        ['LuaJIT']  = false,
    }
    for _, version in ipairs(doc.versions) do
        if version.ge and type(version.version) == 'number' then
            for ver in pairs(valids) do
                local verNumber = tonumber(ver:sub(-3))
                if verNumber and verNumber >= version.version then
                    valids[ver] = true
                end
            end
        elseif version.le and type(version.version) == 'number' then
            for ver in pairs(valids) do
                local verNumber = tonumber(ver:sub(-3))
                if verNumber and verNumber <= version.version then
                    valids[ver] = true
                end
            end
        elseif type(version.version) == 'number' then
            valids[('Lua %.1f'):format(version.version)] = true
        elseif 'JIT' == version.version then
            valids['LuaJIT'] = true
        end
    end
    if valids['Lua 5.1'] then
        valids['LuaJIT'] = true
    end
    return valids
end