summaryrefslogtreecommitdiff
path: root/script/core/completion/postfix.lua
blob: 434c2a82c2b5988db3614f3f1dbdac3aea45ccda (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
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
local guide        = require 'parser.guide'
local lookback     = require 'core.look-backward'
local matchKey     = require 'core.matchkey'
local subString    = require 'core.substring'
local define       = require 'proto.define'
local markdown     = require 'provider.markdown'
local config       = require 'config'

local actions = {}

local function register(key)
    return function (data)
        actions[#actions+1] = {
            key  = key,
            data = data
        }
    end
end

local function hasNonFieldInNode(source)
    local block = guide.getParentBlock(source)
    while source ~= block do
        if source.type == 'call'
        or source.type == 'getindex'
        or source.type == 'getmethod' then
            return true
        end
        source = source.parent
    end
    return false
end

register 'function' {
    function (state, source, callback)
        if  source.type ~= 'getglobal'
        and source.type ~= 'getfield'
        and source.type ~= 'getlocal'
        and source.type ~= 'local' then
            return
        end
        if hasNonFieldInNode(source) then
            return
        end
        local subber = subString(state)
        callback(string.format('function %s($1)\n\t$0\nend'
            , subber(source.start + 1, source.finish)
        ))
    end
}

register 'method' {
    function (state, source, callback)
        if  source.type == 'getfield' then
            if hasNonFieldInNode(source) then
                return
            end
            local subber = subString(state)
            callback(string.format('function %s:%s($1)\n\t$0\nend'
                , subber(source.start + 1, source.dot.start)
                , subber(source.dot .finish + 1, source.finish)
            ))
        end
        if  source.type == 'getmethod' then
            if hasNonFieldInNode(source.parent) then
                return
            end
            local subber = subString(state)
            callback(string.format('function %s:%s($1)\n\t$0\nend'
                , subber(source.start + 1, source.colon.start)
                , subber(source.colon.finish + 1, source.finish)
            ))
        end
    end
}

register 'pcall' {
    function (state, source, callback)
        if  source.type ~= 'getglobal'
        and source.type ~= 'getfield'
        and source.type ~= 'getmethod'
        and source.type ~= 'getindex'
        and source.type ~= 'getlocal'
        and source.type ~= 'call' then
            return
        end
        local subber = subString(state)
        if source.type == 'call' then
            if source.args and #source.args > 0 then
                callback(string.format('pcall(%s, %s)'
                    , subber(source.node.start + 1, source.node.finish)
                    , subber(source.args[1].start + 1, source.args[#source.args].finish)
                ))
            else
                callback(string.format('pcall(%s)'
                    , subber(source.node.start + 1, source.node.finish)
                ))
            end
        else
            callback(string.format('pcall(%s$1)$0'
                , subber(source.start + 1, source.finish)
            ))
        end
    end
}

register 'xpcall' {
    function (state, source, callback)
        if  source.type ~= 'getglobal'
        and source.type ~= 'getfield'
        and source.type ~= 'getmethod'
        and source.type ~= 'getindex'
        and source.type ~= 'getlocal'
        and source.type ~= 'call' then
            return
        end
        local subber = subString(state)
        if source.type == 'call' then
            if source.args and #source.args > 0 then
                callback(string.format('xpcall(%s, ${1:debug.traceback}, %s)$0'
                    , subber(source.node.start + 1, source.node.finish)
                    , subber(source.args[1].start + 1, source.args[#source.args].finish)
                ))
            else
                callback(string.format('xpcall(%s, ${1:debug.traceback})$0'
                    , subber(source.node.start + 1, source.node.finish)
                ))
            end
        else
            callback(string.format('xpcall(%s, ${1:debug.traceback}$2)$0'
                , subber(source.start + 1, source.finish)
            ))
        end
    end
}

local accepts = {
    ['local']     = true,
    ['getlocal']  = true,
    ['getglobal'] = true,
    ['getfield']  = true,
    ['getindex']  = true,
    ['getmethod'] = true,
    ['call']      = true,
}

local function checkPostFix(state, word, wordPosition, position, results)
    local source = guide.eachSourceContain(state.ast, wordPosition, function (source)
        if accepts[source.type] then
            return source
        end
    end)
    if not source then
        return
    end
    for _, action in ipairs(actions) do
        if matchKey(word, action.key) then
            action.data[1](state, source, function (newText)
                results[#results+1] = {
                    label      = action.key,
                    kind       = define.CompletionItemKind.Event,
                    description= markdown()
                                    : add('lua', newText)
                                    : string(),
                    textEdit   = {
                        start   = wordPosition + 1,
                        finish  = position,
                        newText = newText,
                    },
                    additionalTextEdits = {
                        {
                            start   = source.start,
                            finish  = wordPosition + 1,
                            newText = '',
                        },
                    },
                }
            end)
        end
    end
end

return function (state, position, results)
    if guide.isInString(state.ast, position) then
        return false
    end
    local text = state.lua
    local offset = guide.positionToOffset(state, position)
    local word, newOffset = lookback.findWord(text, offset)
    if newOffset then
        offset = newOffset - 1
    end
    local symbol = text:sub(offset, offset)
    if symbol == config.get 'Lua.completion.postfix' then
        local wordPosition = guide.offsetToPosition(state, offset - 1)
        checkPostFix(state, word or '', wordPosition, position, results)
        return symbol ~= '.' and symbol ~= ':'
    end
    return false
end