summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--server/src/json/decode.lua10
-rw-r--r--server/src/method/init.lua1
-rw-r--r--server/src/method/initialize.lua5
-rw-r--r--server/src/method/textDocument/codeAction.lua11
-rw-r--r--server/src/method/workspace/executeCommand.lua55
5 files changed, 72 insertions, 10 deletions
diff --git a/server/src/json/decode.lua b/server/src/json/decode.lua
index 6a93d127..aebd49ca 100644
--- a/server/src/json/decode.lua
+++ b/server/src/json/decode.lua
@@ -119,10 +119,12 @@ local Token = P
return function (str, save_sort_)
save_sort = save_sort_
- local table, pos = Token:match(str)
- if not pos or pos <= #str then
- pos = pos or 1
- error(('没匹配完[%s]\n%s'):format(pos, str:sub(pos, pos+100)))
+ local table, res, pos = Token:match(str)
+ if not table then
+ if not pos or pos <= #str then
+ pos = pos or 1
+ error(('没匹配完[%s][%s]\n%s'):format(pos, res, str:sub(pos, pos+100)))
+ end
end
return table
end
diff --git a/server/src/method/init.lua b/server/src/method/init.lua
index 2fe9f4b1..565fdb64 100644
--- a/server/src/method/init.lua
+++ b/server/src/method/init.lua
@@ -25,5 +25,6 @@ init 'textDocument/signatureHelp'
init 'workspace/didChangeConfiguration'
init 'workspace/didChangeWatchedFiles'
init 'workspace/didChangeWorkspaceFolders'
+init 'workspace/executeCommand'
return method
diff --git a/server/src/method/initialize.lua b/server/src/method/initialize.lua
index 5148e24b..f63b9f7c 100644
--- a/server/src/method/initialize.lua
+++ b/server/src/method/initialize.lua
@@ -39,6 +39,11 @@ return function (lsp)
changeNotifications = true,
}
},
+ executeCommandProvider = {
+ commands = {
+ 'config'
+ },
+ },
}
}
end
diff --git a/server/src/method/textDocument/codeAction.lua b/server/src/method/textDocument/codeAction.lua
index 4c4e85a3..04a07e3b 100644
--- a/server/src/method/textDocument/codeAction.lua
+++ b/server/src/method/textDocument/codeAction.lua
@@ -1,5 +1,4 @@
return function (lsp, params)
- log.debug(table.dump(params))
local uri = params.textDocument.uri
local result = {}
@@ -13,17 +12,17 @@ return function (lsp, params)
title = '测试',
command = 'config',
arguments = {
- key = {'diagnostics', 'disable'},
- action = 'add',
- value = data.code,
+ {
+ key = {'diagnostics', 'disable'},
+ action = 'add',
+ value = data.code,
+ }
}
}
}
end
end
- log.debug(table.dump(result))
-
if #result == 0 then
return nil
end
diff --git a/server/src/method/workspace/executeCommand.lua b/server/src/method/workspace/executeCommand.lua
new file mode 100644
index 00000000..4ee06c5b
--- /dev/null
+++ b/server/src/method/workspace/executeCommand.lua
@@ -0,0 +1,55 @@
+local fs = require 'bee.filesystem'
+local json = require 'json'
+local config = require 'config'
+
+local command = {}
+
+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, 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 = {attr}
+ elseif type(attr) == 'table' then
+ else
+ return
+ end
+
+ attr[#attr+1] = data.value
+ setting[key] = attr
+ end
+
+ io.save(vscodePath / 'settings.json', json.encode(setting) .. '\r\n')
+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