summaryrefslogtreecommitdiff
path: root/server/src/method/workspace/executeCommand.lua
diff options
context:
space:
mode:
author最萌小汐 <sumneko@hotmail.com>2019-03-28 14:57:22 +0800
committer最萌小汐 <sumneko@hotmail.com>2019-03-28 14:57:22 +0800
commit2a5d1448e745355a8e7b0ac837193b781784e5b1 (patch)
tree01c689ecdfb84beca66e19cff4bc847f3955c50c /server/src/method/workspace/executeCommand.lua
parenta6af19e85b8209043bd0cb62ba3bfae92e82badb (diff)
downloadlua-language-server-2a5d1448e745355a8e7b0ac837193b781784e5b1.zip
清除后置空格
Diffstat (limited to 'server/src/method/workspace/executeCommand.lua')
-rw-r--r--server/src/method/workspace/executeCommand.lua73
1 files changed, 73 insertions, 0 deletions
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