summaryrefslogtreecommitdiff
path: root/server/src/method/workspace/executeCommand.lua
blob: 6ae407ae191afa2f334c5d569c2a37cd59e04b66 (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
200
201
202
203
204
205
206
207
208
209
210
211
212
local fs = require 'bee.filesystem'
local json = require 'json'
local config = require 'config'
local rpc = require 'rpc'
local lang = require 'language'

local command = {}

local function isContainPos(obj, start, finish)
    if obj.start <= start and obj.finish >= finish then
        return true
    end
    return false
end

local function isInString(vm, start, finish)
    return vm:eachSource(function (source)
        if source.type == 'string' and isContainPos(source, start, finish) then
            return true
        end
    end)
end

local function posToRange(lines, start, finish)
    local start_row,  start_col  = lines:rowcol(start)
    local finish_row, finish_col = lines:rowcol(finish)
    return {
        start = {
            line = start_row - 1,
            character = start_col - 1,
        },
        ['end'] = {
            line = finish_row - 1,
            character = finish_col,
        },
    }
end

function command.config(lsp, data)
    local def = config.config
    for _, k in ipairs(data.key) do
        def = def[k]
        if not def then
            return
        end
    end
    if data.action == 'add' then
        if type(def) ~= 'table' then
            return
        end
    end

    local vscodePath = lsp.workspace.root / '.vscode'
    local settingBuf = io.load(vscodePath / 'settings.json')
    if not settingBuf then
        fs.create_directories(vscodePath)
    end

    local setting = json.decode(settingBuf or '', true) or {}
    local key = 'Lua.' .. table.concat(data.key, '.')
    local attr = setting[key]

    if data.action == 'add' then
        if attr == nil then
            attr = {}
        elseif type(attr) == 'string' then
            attr = {}
            for str in attr:gmatch '[^;]+' do
                attr[#attr+1] = str
            end
        elseif type(attr) == 'table' then
        else
            return
        end

        attr[#attr+1] = data.value
        setting[key] = attr
    elseif data.action == 'set' then
        setting[key] = data.value
    end

    io.save(vscodePath / 'settings.json', json.encode(setting) .. '\r\n')
end

function command.removeSpace(lsp, data)
    local uri = data.uri
    local vm, lines = lsp:getVM(uri)
    if not vm then
        return
    end

    local textEdit = {}
    for i = 1, #lines do
        local line = lines:line(i)
        local pos = line:find '[ \t]+$'
        if pos then
            local start, finish = lines:range(i)
            start = start + pos - 1
            if isInString(vm, start, finish) then
                goto NEXT_LINE
            end
            textEdit[#textEdit+1] = {
                range = posToRange(lines, start, finish),
                newText = '',
            }
            goto NEXT_LINE
        end

        ::NEXT_LINE::
    end

    if #textEdit == 0 then
        return
    end

    rpc:request('workspace/applyEdit', {
        label = lang.script.COMMAND_REMOVE_SPACE,
        edit = {
            changes = {
                [uri] = textEdit,
            }
        },
    })
end

local opMap = {
    ['+']  = true,
    ['-']  = true,
    ['*']  = true,
    ['/']  = true,
    ['//'] = true,
    ['^']  = true,
    ['<<'] = true,
    ['>>'] = true,
    ['&']  = true,
    ['|']  = true,
    ['~']  = true,
    ['..'] = true,
}

function command.solve(lsp, data)
    local uri = data.uri
    local vm, lines = 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 result = vm:eachSource(function (source)
        if not isContainPos(source, start, finish) then
            return
        end
        if source.op ~= 'or' then
            return
        end
        for x = 1, 2 do
            local y = x % 2 + 1
            local exp = source[x]
            local other = source[y]
            if opMap[exp.op] and not opMap[other.op] then
                if x == 1 then
                    -- (a + b) or c --> a + (b or c)
                    return {
                        start = source[1][2].start,
                        finish = source[2].finish,
                    }
                else
                    -- a or (b + c) --> (a or b) + c
                    return {
                        start = source[1].start,
                        finish = source[2][1].finish,
                    }
                end
            end
        end
    end)

    log.debug(table.dump(result))

    if not result then
        return
    end

    rpc:request('workspace/applyEdit', {
        label = lang.script.COMMAND_ADD_BRACKETS,
        edit = {
            changes = {
                [uri] = {
                    {
                        range = posToRange(lines, result.start, result.start - 1),
                        newText = '(',
                    },
                    {
                        range = posToRange(lines, result.finish + 1, result.finish),
                        newText = ')',
                    },
                }
            }
        },
    })
end

return function (lsp, params)
    local name = params.command
    if not command[name] then
        return
    end
    local result = command[name](lsp, params.arguments[1])
    return result
end