diff options
author | 最萌小汐 <sumneko@hotmail.com> | 2019-03-28 14:57:22 +0800 |
---|---|---|
committer | 最萌小汐 <sumneko@hotmail.com> | 2019-03-28 14:57:22 +0800 |
commit | 2a5d1448e745355a8e7b0ac837193b781784e5b1 (patch) | |
tree | 01c689ecdfb84beca66e19cff4bc847f3955c50c /server | |
parent | a6af19e85b8209043bd0cb62ba3bfae92e82badb (diff) | |
download | lua-language-server-2a5d1448e745355a8e7b0ac837193b781784e5b1.zip |
清除后置空格
Diffstat (limited to 'server')
-rw-r--r-- | server/src/json/decode.lua | 4 | ||||
-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 |
4 files changed, 143 insertions, 44 deletions
diff --git a/server/src/json/decode.lua b/server/src/json/decode.lua index aebd49ca..fcc40de5 100644 --- a/server/src/json/decode.lua +++ b/server/src/json/decode.lua @@ -110,8 +110,8 @@ local Token = P String = '"' * Cs(V'Char'^0) * '"', Char = V'Esc' + (1 - P'"' - P'\t' - V'Nl'), Esc = P'\\' * C(S'tnr"\\') / EscMap, - Hash = V'Spnl' * '{' * V'Spnl' * HashTable(V'Object'^-1 * (P',' * V'Object')^0) * V'Spnl' * P'}' * V'Spnl', - Array = V'Spnl' * '[' * V'Spnl' * Ct(V'Value'^-1 * (P',' * V'Spnl' * V'Value')^0) * V'Spnl' * P']' * V'Spnl', + Hash = V'Spnl' * '{' * V'Spnl' * HashTable((V'Object' + P',')^0) * V'Spnl' * P'}' * V'Spnl', + Array = V'Spnl' * '[' * V'Spnl' * Ct((V'Value' + P',')^0) * V'Spnl' * P']' * V'Spnl', Object = V'Spnl' * V'Key' * V'Spnl' * V'Value' * V'Spnl', Key = V'String' * V'Spnl' * ':', Value = V'Hash' + V'Array' + V'Bool' + V'Null' + V'String' + V'Float' + V'Int', 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 |