diff options
author | 最萌小汐 <sumneko@hotmail.com> | 2019-09-20 14:39:11 +0800 |
---|---|---|
committer | 最萌小汐 <sumneko@hotmail.com> | 2019-09-20 14:39:11 +0800 |
commit | 8a9202e826169f688b2dbb271e3a2b587c347a73 (patch) | |
tree | a4f68e3f6e2922d7ef1b4d9c97b8f1a82df0f26c /server-beta/src/proto | |
parent | 99357fa1782f409201efe502713efce011015da8 (diff) | |
download | lua-language-server-8a9202e826169f688b2dbb271e3a2b587c347a73.zip |
完整的proto
Diffstat (limited to 'server-beta/src/proto')
-rw-r--r-- | server-beta/src/proto/proto.lua | 59 |
1 files changed, 53 insertions, 6 deletions
diff --git a/server-beta/src/proto/proto.lua b/server-beta/src/proto/proto.lua index d35d76ba..cc9988a8 100644 --- a/server-beta/src/proto/proto.lua +++ b/server-beta/src/proto/proto.lua @@ -5,12 +5,19 @@ local pub = require 'pub' local jsonrpc = require 'jsonrpc' local ErrorCodes = require 'define.ErrorCodes' +local reqCounter = util.counter() + local m = {} m.ability = {} +m.waiting = {} -local function isOptionalMethod(method) - return method:sub(1, 2) == '$/' +function m.getMethodName(proto) + if proto.method:sub(1, 2) == '$/' then + return proto.method:sub(3), true + else + return proto.method, false + end end function m.on(method, callback) @@ -37,11 +44,32 @@ function m.responseErr(id, code, message) io.stdout:write(buf) end -function m.doProto(proto) - local method = proto.method +function m.notify(name, params) + local buf = jsonrpc.encode { + method = name, + params = params, + } + io.stdout:write(buf) +end + +function m.request(name, params) + local id = reqCounter() + local buf = jsonrpc.encode { + id = id, + method = name, + params = params, + } + io.stdout:write(buf) + return task.wait(function (waker) + m.waiting[id] = waker + end) +end + +function m.doMethod(proto) + local method, optional = m.getMethodName(proto) local abil = m.ability[method] if not abil then - if not isOptionalMethod(method) then + if not optional then log.warn('Recieved unknown proto: ' .. method) end if proto.id then @@ -64,6 +92,21 @@ function m.doProto(proto) end) end +function m.doResponse(proto) + local id = proto.id + local waker = m.waiting[id] + if not waker then + log.warn('Response id not found: ' .. util.dump(proto)) + return + end + m.waiting[id] = nil + if proto.error then + log.warn(('Response error [%d]: %s'):format(proto.error.code, proto.error.message)) + return + end + waker(proto.result) +end + function m.listen() subprocess.filemode(io.stdin, 'b') subprocess.filemode(io.stdout, 'b') @@ -72,7 +115,11 @@ function m.listen() task.create(function () while true do local proto = pub.task('loadProto') - m.doProto(proto) + if proto.method then + m.doMethod(proto) + else + m.doResponse(proto) + end end end) end |