summaryrefslogtreecommitdiff
path: root/server/src/method/textDocument/codeAction.lua
blob: 80faee510e258008045b482a02e8397e2fe03756 (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
local function disableDiagnostic(lsp, uri, data, callback)
    callback {
        title = ('禁用诊断(%s)'):format(data.code),
        kind = 'quickfix',
        command = {
            title = '禁用诊断',
            command = 'config',
            arguments = {
                {
                    key = {'diagnostics', 'disable'},
                    action = 'add',
                    value = data.code,
                }
            }
        }
    }
end

local function solveUndefinedGlobal(lsp, uri, data, callback)
    local vm, lines, text = lsp:getVM(uri)
    if not vm then
        return
    end
    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 = ('标记 `%s` 为已定义的全局变量'):format(name),
        kind = 'quickfix',
        command = {
            title = '标记全局变量',
            command = 'config',
            arguments = {
                {
                    key = {'diagnostics', 'globals'},
                    action = 'add',
                    value = name,
                }
            }
        },
    }
end

local function solveTrailingSpace(lsp, uri, data, callback)
    callback {
        title = '清除所有后置空格',
        kind = 'quickfix',
        command = {
            title = '清除所有后置空格',
            command = 'removeSpace',
            arguments = {
                {
                    uri = uri,
                }
            }
        },
    }
end

local function solveNewlineCall(lsp, uri, data, callback)
    callback {
        title = '添加 `;`',
        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 = '添加括号',
        kind = 'quickfix',
        command = {
            title = '添加括号',
            command = 'solve',
            arguments = {
                {
                    name = 'ambiguity-1',
                    uri = uri,
                    range = data.range,
                }
            }
        },
    }
end

local function solveDiagnostic(lsp, uri, data, callback)
    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
    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