summaryrefslogtreecommitdiff
path: root/server
diff options
context:
space:
mode:
author最萌小汐 <sumneko@hotmail.com>2018-12-06 18:07:10 +0800
committer最萌小汐 <sumneko@hotmail.com>2018-12-06 18:07:10 +0800
commit979676be219b83c7295409824ee8280a3335cc5a (patch)
treee4928df884ddd961671d62876e84d2bec7c9ab19 /server
parentd119c1cefff8a6a2812ac10375297f033a8cf5f0 (diff)
downloadlua-language-server-979676be219b83c7295409824ee8280a3335cc5a.zip
枚举
Diffstat (limited to 'server')
-rw-r--r--server/libs/lua53/basic.lni38
-rw-r--r--server/src/json/init.lua1
-rw-r--r--server/src/json/table.lua62
-rw-r--r--server/src/matcher/find_lib.lua4
-rw-r--r--server/src/matcher/hover.lua89
-rw-r--r--server/src/service.lua20
-rw-r--r--server/src/utility.lua58
7 files changed, 179 insertions, 93 deletions
diff --git a/server/libs/lua53/basic.lni b/server/libs/lua53/basic.lni
index 596b88f4..4963887a 100644
--- a/server/libs/lua53/basic.lni
+++ b/server/libs/lua53/basic.lni
@@ -2,8 +2,46 @@
type = 'function'
[assert]
+[[.args]]
+name = 'v'
+[[.args]]
+name = 'message'
+[[.returns]]
[collectgarbage]
+[[.args]]
+name = 'opt'
+type = 'string'
+optional = 'after'
+[[.args]]
+name = 'arg'
+type = 'number'
+optional = 'self'
+[[.returns]]
+[[.enums]]
+name = 'opt'
+enum = 'collect'
+[[.enums]]
+name = 'opt'
+enum = 'stop'
+[[.enums]]
+name = 'opt'
+enum = 'restart'
+[[.enums]]
+name = 'opt'
+enum = 'count'
+[[.enums]]
+name = 'opt'
+enum = 'step'
+[[.enums]]
+name = 'opt'
+enum = 'setpause'
+[[.enums]]
+name = 'opt'
+enum = 'setstepmul'
+[[.enums]]
+name = 'opt'
+enum = 'isrunning'
[dofile]
diff --git a/server/src/json/init.lua b/server/src/json/init.lua
index db513678..c28e7aed 100644
--- a/server/src/json/init.lua
+++ b/server/src/json/init.lua
@@ -1,7 +1,6 @@
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
deleted file mode 100644
index 351bfe07..00000000
--- a/server/src/json/table.lua
+++ /dev/null
@@ -1,62 +0,0 @@
-
-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/matcher/find_lib.lua b/server/src/matcher/find_lib.lua
index a7b15bbb..ca5577d1 100644
--- a/server/src/matcher/find_lib.lua
+++ b/server/src/matcher/find_lib.lua
@@ -13,7 +13,7 @@ local function getLibs()
if Libs then
return Libs
end
- Libs = {}
+ Libs = table.container()
for path in io.scan(ROOT / 'libs') do
local buf = io.load(path)
if buf then
@@ -22,7 +22,7 @@ local function getLibs()
end
local language = require 'language'
- local locale = {}
+ local locale = table.container()
for path in io.scan(ROOT / 'locale' / language / 'libs') do
local buf = io.load(path)
if buf then
diff --git a/server/src/matcher/hover.lua b/server/src/matcher/hover.lua
index 2b78216a..79a47ede 100644
--- a/server/src/matcher/hover.lua
+++ b/server/src/matcher/hover.lua
@@ -6,21 +6,30 @@ local function buildArgs(lib)
return ''
end
local strs = {}
- for i, rtn in ipairs(lib.args) do
- if rtn.optional then
- strs[#strs+1] = '['
+ for i, arg in ipairs(lib.args) do
+ if arg.optional then
+ if i > 1 then
+ strs[#strs+1] = ' ['
+ else
+ strs[#strs+1] = '['
+ end
end
if i > 1 then
strs[#strs+1] = ', '
end
- strs[#strs+1] = ('%s:%s'):format(
- rtn.name or ('arg' .. tostring(i)),
- (rtn.type or 'any')
- )
- if rtn.default then
- strs[#strs+1] = ('(%q)'):format(rtn.default)
+ if arg.name then
+ strs[#strs+1] = ('%s:'):format(arg.name)
end
- if rtn.optional then
+ strs[#strs+1] = arg.type or 'any'
+ if arg.default then
+ strs[#strs+1] = ('(%q)'):format(arg.default)
+ end
+ if arg.optional == 'self' then
+ strs[#strs+1] = ']'
+ end
+ end
+ for _, arg in ipairs(lib.args) do
+ if arg.optional == 'after' then
strs[#strs+1] = ']'
end
end
@@ -34,29 +43,73 @@ local function buildReturns(lib)
local strs = {}
for i, rtn in ipairs(lib.returns) do
if rtn.optional then
- strs[#strs+1] = '['
+ if i > 1 then
+ strs[#strs+1] = ' ['
+ else
+ strs[#strs+1] = '['
+ end
end
if i > 1 then
strs[#strs+1] = ', '
end
- strs[#strs+1] = ('%s:%s'):format(
- rtn.name or ('res' .. tostring(i)),
- (rtn.type or 'any')
- )
+ if rtn.name then
+ strs[#strs+1] = ('%s:'):format(rtn.name)
+ end
+ strs[#strs+1] = rtn.type or 'any'
if rtn.default then
strs[#strs+1] = ('(%q)'):format(rtn.default)
end
- if rtn.optional then
+ if rtn.optional == 'self' then
+ strs[#strs+1] = ']'
+ end
+ end
+ for _, rtn in ipairs(lib.returns) do
+ if rtn.optional == 'after' then
strs[#strs+1] = ']'
end
end
- return '\n-> ' .. table.concat(strs)
+ return '\n -> ' .. table.concat(strs)
+end
+
+local function buildEnum(lib)
+ if not lib.enums then
+ return ''
+ end
+ local container = table.container()
+ for _, enum in ipairs(lib.enums) do
+ if not enum.name or not enum.enum then
+ goto NEXT_ENUM
+ end
+ if not container[enum.name] then
+ container[enum.name] = {}
+ if lib.args then
+ for _, arg in ipairs(lib.args) do
+ if arg.name == enum.name then
+ container[enum.name].type = arg.type
+ break
+ end
+ end
+ end
+ end
+ table.insert(container[enum.name], enum)
+ ::NEXT_ENUM::
+ end
+ local strs = {}
+ for name, enums in pairs(container) do
+ strs[#strs+1] = ('\n%s:%s'):format(name, enums.type or '')
+ for _, enum in ipairs(enums) do
+ strs[#strs+1] = '\n | '
+ strs[#strs+1] = ('%q: %s'):format(enum.enum, enum.description or '')
+ end
+ end
+ return table.concat(strs)
end
local function buildFunctionHover(lib, name)
local title = ('function %s(%s)%s'):format(name, buildArgs(lib), buildReturns(lib))
+ local enum = buildEnum(lib)
local tip = lib.description or ''
- return ('```lua\n%s\n```\n%s'):format(title, tip)
+ return ('```lua\n%s%s\n```\n%s'):format(title, enum, tip)
end
return function (results, pos)
diff --git a/server/src/service.lua b/server/src/service.lua
index ab33b8e2..98d993f2 100644
--- a/server/src/service.lua
+++ b/server/src/service.lua
@@ -66,20 +66,20 @@ end
function mt:_doProto(proto)
local id = proto.id
- local method = proto.method
+ local name = proto.method
local params = proto.params
- local response, err = self:_callMethod(method, params)
+ local response, err = self:_callMethod(name, params)
if not id then
return
end
- local proto = json.table()
- proto.id = id
+ local container = table.container()
+ container.id = id
if err then
- proto.error = err
+ container.error = err
else
- proto.result = response
+ container.result = response
end
- self:_send(proto)
+ self:_send(container)
end
function mt:_doDiagnostic()
@@ -95,11 +95,11 @@ function mt:_doDiagnostic()
self._needDiagnostics[uri] = nil
end
for uri, data in pairs(copy) do
- local method = 'textDocument/publishDiagnostics'
- local res = self:_callMethod(method, data)
+ local name = 'textDocument/publishDiagnostics'
+ local res = self:_callMethod(name, data)
if res then
self:_send {
- method = method,
+ method = name,
params = {
uri = uri,
diagnostics = res,
diff --git a/server/src/utility.lua b/server/src/utility.lua
index e6288741..88c26668 100644
--- a/server/src/utility.lua
+++ b/server/src/utility.lua
@@ -6,6 +6,10 @@ local type = type
local pairs = pairs
local ipairs = ipairs
local math_type = math.type
+local next = next
+local rawset = rawset
+local move = table.move
+local setmetatable = setmetatable
local TAB = setmetatable({}, { __index = function (self, n)
self[n] = string_rep('\t', n)
@@ -63,6 +67,60 @@ function table.dump(tbl)
return table.concat(lines, '\n')
end
+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
+ table_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)
+ table_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
+
+function table.container(tbl)
+ return sort_table(tbl)
+end
+
function io.load(file_path)
local f, e = io.open(file_path:string(), 'rb')
if not f then