summaryrefslogtreecommitdiff
path: root/server/src/method/textDocument/codeAction.lua
blob: 53987d2d1afcef537b29b0cc469cf5066301e899 (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 lang = require 'language'

local function disableDiagnostic(lsp, uri, data, callback)
    callback {
        title = lang.script('ACTION_DISABLE_DIAG', data.code),
        kind = 'quickfix',
        command = {
            title = lang.script.COMMAND_DISABLE_DIAG,
            command = 'config',
            arguments = {
                {
                    key = {'diagnostics', 'disable'},
                    action = 'add',
                    value = data.code,
                }
            }
        }
    }
end

local function addGlobal(lines, text, data, callback)
    local start = lines:position(data.range.start.line + 1, data.range.start.character + 1)
    local finish = lines:position(data.range['end'].line + 1, data.range['end'].character)
    local name = text:sub(start, finish)
    if #name < 0 or name:find('[^%w_]') then
        return
    end
    callback {
        title = lang.script('ACTION_MARK_GLOBAL', name),
        kind = 'quickfix',
        command = {
            title = lang.script.COMMAND_MARK_GLOBAL,
            command = 'config',
            arguments = {
                {
                    key = {'diagnostics', 'globals'},
                    action = 'add',
                    value = name,
                }
            }
        },
    }
end

local function solveUndefinedGlobal(lsp, uri, data, callback)
    local vm, lines, text = lsp:getVM(uri)
    if not vm then
        return
    end
    addGlobal(lines, text, data, callback)
end

local function solveLowercaseGlobal(lsp, uri, data, callback)
    local vm, lines, text = lsp:getVM(uri)
    if not vm then
        return
    end
    addGlobal(lines, text, data, callback)
end

local function solveTrailingSpace(lsp, uri, data, callback)
    callback {
        title = lang.script.ACTION_REMOVE_SPACE,
        kind = 'quickfix',
        command = {
            title = lang.script.COMMAND_REMOVE_SPACE,
            command = 'removeSpace',
            arguments = {
                {
                    uri = uri,
                }
            }
        },
    }
end

local function solveNewlineCall(lsp, uri, data, callback)
    callback {
        title = lang.script.ACTION_ADD_SEMICOLON,
        kind = 'quickfix',
        edit = {
            changes = {
                [uri] = {
                    {
                        range = {
                            start = data.range.start,
                            ['end'] = data.range.start,
                        },
                        newText = ';',
                    }
                }
            }
        }
    }
end

local function solveAmbiguity1(lsp, uri, data, callback)
    callback {
        title = lang.script.ACTION_ADD_BRACKETS,
        kind = 'quickfix',
        command = {
            title = lang.script.COMMAND_ADD_BRACKETS,
            command = 'solve',
            arguments = {
                {
                    name = 'ambiguity-1',
                    uri = uri,
                    range = data.range,
                }
            }
        },
    }
end

local function findSyntax(astErr, lines, data)
    local start = lines:position(data.range.start.line + 1, data.range.start.character + 1)
    local finish = lines:position(data.range['end'].line + 1, data.range['end'].character)
    for _, err in ipairs(astErr) do
        if err.start == start and err.finish == finish then
            return err
        end
    end
    return nil
end

local function solveSyntax(lsp, uri, data, callback)
    local obj = lsp:getFile(uri)
    if not obj then
        return
    end
    local astErr, lines = obj.astErr, obj.lines
    if not astErr or not lines then
        return
    end
    local err = findSyntax(astErr, lines, data)
    if not err then
        return nil
    end
    if err.version then
        callback {
            title = lang.script.CHANGE_RUNTIME_VERSION,
            kind = 'quickfix',
            command = {
                title = lang.script.COMMAND_RUNTIME_VERSION,
                command = 'config',
                arguments = {
                    {
                        key = {'runtime', 'version'},
                        action = 'set',
                        value = err.version,
                    }
                }
            },
        }
    end
end

local function solveDiagnostic(lsp, uri, data, callback)
    if data.source == lang.script.DIAG_SYNTAX_CHECK then
        solveSyntax(lsp, uri, data, callback)
    end
    if not data.code then
        return
    end
    if data.code == 'undefined-global' then
        solveUndefinedGlobal(lsp, uri, data, callback)
    end
    if data.code == 'trailing-space' then
        solveTrailingSpace(lsp, uri, data, callback)
    end
    if data.code == 'newline-call' then
        solveNewlineCall(lsp, uri, data, callback)
    end
    if data.code == 'ambiguity-1' then
        solveAmbiguity1(lsp, uri, data, callback)
    end
    if data.code == 'lowercase-global' then
        solveLowercaseGlobal(lsp, uri, data, callback)
    end
    disableDiagnostic(lsp, uri, data, callback)
end

return function (lsp, params)
    local uri = params.textDocument.uri

    local results = {}

    for _, data in ipairs(params.context.diagnostics) do
        solveDiagnostic(lsp, uri, data, function (result)
            results[#results+1] = result
        end)
    end

    if #results == 0 then
        return nil
    end

    return results
end