summaryrefslogtreecommitdiff
path: root/script-beta/core/code-action.lua
blob: de7ef6acfe77ef3f60dde5c9847a409014c98137 (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
local files   = require 'files'
local lang    = require 'language'
local define  = require 'proto.define'
local guide   = require 'parser.guide'
local library = require 'library'

local function disableDiagnostic(code, results)
    results[#results+1] = {
        title   = lang.script('ACTION_DISABLE_DIAG', code),
        kind    = 'quickfix',
        command = {
            title    = lang.script.COMMAND_DISABLE_DIAG,
            command  = 'lua.config',
            arguments = {
                {
                    key    = 'Lua.diagnostics.disable',
                    action = 'add',
                    value  = code,
                }
            }
        }
    }
end

local function markGlobal(name, results)
    results[#results+1] = {
        title   = lang.script('ACTION_MARK_GLOBAL', name),
        kind    = 'quickfix',
        command = {
            title     = lang.script.COMMAND_MARK_GLOBAL,
            command   = 'lua.config',
            arguments = {
                {
                    key    = 'Lua.diagnostics.globals',
                    action = 'add',
                    value  = name,
                }
            }
        }
    }
end

local function changeVersion(version, results)
    results[#results+1] = {
        title   = lang.script('ACTION_RUNTIME_VERSION', version),
        kind    = 'quickfix',
        command = {
            title     = lang.script.COMMAND_RUNTIME_VERSION,
            command   = 'lua.config',
            arguments = {
                {
                    key    = 'Lua.runtime.version',
                    action = 'set',
                    value  = version,
                }
            }
        },
    }
end

local function solveUndefinedGlobal(uri, diag, results)
    local ast    = files.getAst(uri)
    local text   = files.getText(uri)
    local lines  = files.getLines(uri)
    local offset = define.offsetOfWord(lines, text, diag.range.start)
    guide.eachSourceContain(ast.ast, offset, function (source)
        if source.type ~= 'getglobal' then
            return
        end

        local name = guide.getName(source)
        markGlobal(name, results)

        if library.other[name] then
            for _, version in ipairs(library.other[name]) do
                changeVersion(version, results)
            end
        end
    end)
end

local function solveDiagnostic(uri, diag, results)
    if diag.code == 'undefined-global' then
        solveUndefinedGlobal(uri, diag, results)
    end
    disableDiagnostic(diag.code, results)
end

return function (uri, range, diagnostics)
    local ast = files.getAst(uri)
    if not ast then
        return nil
    end

    local results = {}

    for _, data in ipairs(diagnostics) do
        solveDiagnostic(uri, data, results)
    end

    return results
end