summaryrefslogtreecommitdiff
path: root/script/plugins/astHelper.lua
blob: 6f303c7995bfbed9882004cd1c13e2d64b913bb6 (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
local luadoc = require 'parser.luadoc'
local ssub = require 'core.substring'
local guide = require 'parser.guide'
local _M = {}

function _M.buildComment(t, value)
    return {
        type   = 'comment.short',
        start  = 1,
        finish = 1,
        text   = "-@" .. t .. " " .. value,
    }
end

function _M.InsertDoc(ast, comm)
    local comms = ast.state.comms or {}
    comms[#comms+1] = comm
    ast.state.comms = comms
end

--- give the local/global variable add doc.class
---@param ast parser.object
---@param source parser.object local/global variable
---@param classname string
function _M.addClassDoc(ast, source, classname)
    if source.type ~= 'local' and not guide.isGlobal(source) then
        return false
    end
    --TODO fileds
    --TODO callers
    local comment = _M.buildComment("class", classname)
    comment.start = source.start - 1
    comment.finish = comment.start

    return luadoc.buildAndBindDoc(ast, source, comment)
end

---remove `ast` function node `index` arg, the variable will be the function local variable
---@param source parser.object function node
---@param index integer
---@return parser.object?
function _M.removeArg(source, index)
    if source.type == 'function' then
        local arg = table.remove(source.args, index)
        if not arg then
            return nil
        end
        arg.parent = arg.parent.parent
        return arg
    end
    return nil
end

--- 把特定函数当成构造函数,`index` 参数是self
---@param classname string
---@param source parser.object function node
---@param index integer
function _M.addClassDocAtParam(ast, classname, source, index)
    local arg = _M.removeArg(source, index)
    if arg then
        return _M.addClassDoc(ast, arg, classname)
    end
    return false
end

return _M