diff options
author | 最萌小汐 <sumneko@hotmail.com> | 2018-12-06 10:21:49 +0800 |
---|---|---|
committer | 最萌小汐 <sumneko@hotmail.com> | 2018-12-06 10:21:49 +0800 |
commit | cd57345c054659f27e7b9fdf91d30074a6dca997 (patch) | |
tree | 891a7e1ca8c60820e4e1748664e5f84136feffcc | |
parent | c84d61d473c514f4fe5483f38a8d159ffe8689cd (diff) | |
download | lua-language-server-cd57345c054659f27e7b9fdf91d30074a6dca997.zip |
json表支持null
-rw-r--r-- | server/src/json/init.lua | 1 | ||||
-rw-r--r-- | server/src/json/table.lua | 62 | ||||
-rw-r--r-- | server/src/service.lua | 23 |
3 files changed, 67 insertions, 19 deletions
diff --git a/server/src/json/init.lua b/server/src/json/init.lua index c28e7aed..db513678 100644 --- a/server/src/json/init.lua +++ b/server/src/json/init.lua @@ -1,6 +1,7 @@ local api = { decode = require 'json.decode', encode = require 'json.encode', + table = require 'json.table', } return api diff --git a/server/src/json/table.lua b/server/src/json/table.lua new file mode 100644 index 00000000..351bfe07 --- /dev/null +++ b/server/src/json/table.lua @@ -0,0 +1,62 @@ + +local pairs = pairs +local next = next +local sort = table.sort +local type = type +local rawset = rawset +local move = table.move +local setmetatable = setmetatable + +local function sort_table(tbl) + if not tbl then + tbl = {} + end + local mt = {} + local keys = {} + local mark = {} + local n = 0 + for key in next, tbl do + n=n+1;keys[n] = key + mark[key] = true + end + sort(keys) + function mt:__newindex(key, value) + rawset(self, key, value) + n=n+1;keys[n] = key + mark[key] = true + if type(value) == 'table' then + sort_table(value) + end + end + function mt:__pairs() + local list = {} + local m = 0 + for key in next, self do + if not mark[key] then + m=m+1;list[m] = key + end + end + if m > 0 then + move(keys, 1, n, m+1) + sort(list) + for i = 1, m do + local key = list[i] + keys[i] = key + mark[key] = true + end + n = n + m + end + local i = 0 + return function () + i = i + 1 + local key = keys[i] + return key, self[key] + end + end + + return setmetatable(tbl, mt) +end + +return function (tbl) + return sort_table(tbl) +end diff --git a/server/src/service.lua b/server/src/service.lua index ae1d8c9d..c5987150 100644 --- a/server/src/service.lua +++ b/server/src/service.lua @@ -64,25 +64,10 @@ function mt:_doProto(proto) local method = proto.method local params = proto.params local response, err = self:_callMethod(method, params) - if id then - if response then - self:_send { - id = id, - result = response, - } - else - self:_send { - id = id, - error = { - code = ErrorCodes.UnknownErrorCode, - message = err or ('Lack of response: ' .. method), - }, - } - end - end - if response == nil then - log.error(err or ('Lack of response: ' .. method)) - end + local proto = json.table() + proto.id = id + proto.result = response + self:_send(proto) end function mt:_buildTextCache() |