diff options
author | 最萌小汐 <sumneko@hotmail.com> | 2019-09-20 11:50:03 +0800 |
---|---|---|
committer | 最萌小汐 <sumneko@hotmail.com> | 2019-09-20 11:50:03 +0800 |
commit | f0fa85971692ec7ff98f31230c11d1e9f1f7c362 (patch) | |
tree | caeb7aef589abaa557931872911595d05e499ce0 /server-beta/src/proto | |
parent | e02ead0fa3d86e4c9ecb26ce796bfeb20e751934 (diff) | |
download | lua-language-server-f0fa85971692ec7ff98f31230c11d1e9f1f7c362.zip |
整理目录
Diffstat (limited to 'server-beta/src/proto')
-rw-r--r-- | server-beta/src/proto/init.lua | 8 | ||||
-rw-r--r-- | server-beta/src/proto/proto.lua | 80 |
2 files changed, 88 insertions, 0 deletions
diff --git a/server-beta/src/proto/init.lua b/server-beta/src/proto/init.lua new file mode 100644 index 00000000..5c73dd7e --- /dev/null +++ b/server-beta/src/proto/init.lua @@ -0,0 +1,8 @@ +local proto = require 'proto.proto' +local util = require 'utility' + +proto.on('initialize', function (params) + log.debug(util.dump(params)) +end) + +return proto diff --git a/server-beta/src/proto/proto.lua b/server-beta/src/proto/proto.lua new file mode 100644 index 00000000..d35d76ba --- /dev/null +++ b/server-beta/src/proto/proto.lua @@ -0,0 +1,80 @@ +local subprocess = require 'bee.subprocess' +local util = require 'utility' +local task = require 'task' +local pub = require 'pub' +local jsonrpc = require 'jsonrpc' +local ErrorCodes = require 'define.ErrorCodes' + +local m = {} + +m.ability = {} + +local function isOptionalMethod(method) + return method:sub(1, 2) == '$/' +end + +function m.on(method, callback) + m.ability[method] = callback +end + +function m.response(id, res) + -- res 可能是nil,为了转成json时保留nil,使用 container 容器 + local data = util.container() + data.id = id + data.result = res + local buf = jsonrpc.encode(data) + io.stdout:write(buf) +end + +function m.responseErr(id, code, message) + local buf = jsonrpc.encode { + id = id, + error = { + code = code, + message = message, + } + } + io.stdout:write(buf) +end + +function m.doProto(proto) + local method = proto.method + local abil = m.ability[method] + if not abil then + if not isOptionalMethod(method) then + log.warn('Recieved unknown proto: ' .. method) + end + if proto.id then + m.responseErr(proto.id, ErrorCodes.MethodNotFound, method) + end + return + end + task.create(function () + local clock = os.clock() + local ok, res = xpcall(abil, log.error, proto.params) + local passed = os.clock() - clock + if passed > 0.2 then + log.debug(('Method [%s] takes [%.3f]sec.'):format(method, passed)) + end + if ok then + m.response(proto.id, res) + else + m.responseErr(proto.id, ErrorCodes.InternalError, res) + end + end) +end + +function m.listen() + subprocess.filemode(io.stdin, 'b') + subprocess.filemode(io.stdout, 'b') + io.stdin:setvbuf 'no' + io.stdout:setvbuf 'no' + task.create(function () + while true do + local proto = pub.task('loadProto') + m.doProto(proto) + end + end) +end + +return m |