diff options
author | 最萌小汐 <sumneko@hotmail.com> | 2021-03-08 21:11:14 +0800 |
---|---|---|
committer | 最萌小汐 <sumneko@hotmail.com> | 2021-03-08 21:11:14 +0800 |
commit | 0be605ef1eab73a662540ae2f02766aee47149eb (patch) | |
tree | 49f9a881656ad4241bf198dcbb36d8026b92543d /script/core/command/jsonToLua.lua | |
parent | 242d5ed3ff9b84ffa2ceae33df5641fd0a611519 (diff) | |
download | lua-language-server-0be605ef1eab73a662540ae2f02766aee47149eb.zip |
code-action: convert JSON to Lua
Diffstat (limited to 'script/core/command/jsonToLua.lua')
-rw-r--r-- | script/core/command/jsonToLua.lua | 36 |
1 files changed, 36 insertions, 0 deletions
diff --git a/script/core/command/jsonToLua.lua b/script/core/command/jsonToLua.lua new file mode 100644 index 00000000..6b5613b9 --- /dev/null +++ b/script/core/command/jsonToLua.lua @@ -0,0 +1,36 @@ +local files = require 'files' +local json = require 'json' +local util = require 'utility' +local proto = require 'proto' +local define = require 'proto.define' +local lang = require 'language' + +return function (data) + local text = files.getText(data.uri) + if not text then + return + end + local jsonStr = text:sub(data.start, data.finish) + local suc, res = pcall(json.decode, jsonStr) + if not suc then + proto.notify('window/showMessage', { + type = define.MessageType.Warning, + message = lang.script('COMMAND_JSON_TO_LUA_FAILED', res:match '%:%d+%:(.+)'), + }) + return + end + local luaStr = util.dump(res) + proto.awaitRequest('workspace/applyEdit', { + label = 'json to lua', + edit = { + changes = { + [files.getOriginUri(data.uri)] = { + { + range = files.range(data.uri, data.start, data.finish), + newText = luaStr, + } + } + } + } + }) +end |