diff options
Diffstat (limited to 'server/src/method')
-rw-r--r-- | server/src/method/initialize.lua | 3 | ||||
-rw-r--r-- | server/src/method/textDocument/codeAction.lua | 107 | ||||
-rw-r--r-- | server/src/method/workspace/executeCommand.lua | 73 |
3 files changed, 141 insertions, 42 deletions
diff --git a/server/src/method/initialize.lua b/server/src/method/initialize.lua index f63b9f7c..ecd354c4 100644 --- a/server/src/method/initialize.lua +++ b/server/src/method/initialize.lua @@ -41,7 +41,8 @@ return function (lsp) }, executeCommandProvider = { commands = { - 'config' + 'config', + 'removeSpace', }, }, } diff --git a/server/src/method/textDocument/codeAction.lua b/server/src/method/textDocument/codeAction.lua index e85a8aa3..845b2591 100644 --- a/server/src/method/textDocument/codeAction.lua +++ b/server/src/method/textDocument/codeAction.lua @@ -1,49 +1,74 @@ -local function solveDiagnostic(lsp, uri, data, callback) - if data.code then - callback { - title = ('禁用诊断(%s)'):format(data.code), - kind = 'quickfix', - diagnostics = {data}, - command = { - title = '禁用诊断', - command = 'config', - arguments = { - { - key = {'diagnostics', 'disable'}, - action = 'add', - value = data.code, - } +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 - if data.code == 'undefined-global' then - 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', - diagnostics = {data}, - command = { - title = '标记全局变量', - command = 'config', - arguments = { - { - key = {'diagnostics', 'globals'}, - action = 'add', - value = name, - } + 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 solveDiagnostic(lsp, uri, data, callback) + if data.code then + disableDiagnostic(lsp, uri, data, callback) + 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 end diff --git a/server/src/method/workspace/executeCommand.lua b/server/src/method/workspace/executeCommand.lua index 4ee06c5b..49fc6ee7 100644 --- a/server/src/method/workspace/executeCommand.lua +++ b/server/src/method/workspace/executeCommand.lua @@ -1,9 +1,41 @@ local fs = require 'bee.filesystem' local json = require 'json' local config = require 'config' +local rpc = require 'rpc' local command = {} +local function isContainPos(obj, start, finish) + if obj.start <= start and obj.finish + 1 >= finish then + return true + end + return false +end + +local function isInString(vm, start, finish) + for _, source in ipairs(vm.sources) do + if source.type == 'string' and isContainPos(source, start, finish) then + return true + end + end + return false +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 @@ -45,6 +77,47 @@ function command.config(lsp, data) 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 = '清除所有后置空格', + edit = { + changes = { + [uri] = textEdit, + } + }, + }) +end + return function (lsp, params) local name = params.command if not command[name] then |