diff options
Diffstat (limited to 'server/src')
-rw-r--r-- | server/src/async/async.lua | 55 | ||||
-rw-r--r-- | server/src/async/proto.lua | 7 | ||||
-rw-r--r-- | server/src/async/scanfiles.lua | 17 | ||||
-rw-r--r-- | server/src/method/textDocument/didClose.lua | 1 | ||||
-rw-r--r-- | server/src/method/textDocument/didOpen.lua | 1 | ||||
-rw-r--r-- | server/src/service.lua | 7 | ||||
-rw-r--r-- | server/src/workspace.lua | 19 |
7 files changed, 52 insertions, 55 deletions
diff --git a/server/src/async/async.lua b/server/src/async/async.lua index 8c75540c..6e700ee0 100644 --- a/server/src/async/async.lua +++ b/server/src/async/async.lua @@ -20,30 +20,26 @@ local response = thread.channel(%q) local errlog = thread.channel 'errlog' local function task() - local dump, env = request:bpop() - if env then - setmetatable(env, { __index = _ENV }) - else - env = _ENV - end + local dump, arg = request:bpop() + local env = setmetatable({ + IN = request, + OUT = response, + ERR = errlog, + }, { __index = _ENV }) local f, err = load(dump, '=task', 't', env) if not f then errlog:push(err) return end - local results = table.pack(pcall(f)) - local ok = table.remove(results, 1) - if not ok then - local err = table.remove(results, 1) - errlog:push(err) - return - end - results.n = results.n - 1 - response:push(results) + local result = f(arg) + response:push(result) end while true do - task() + local ok, result = xpcall(task, debug.traceback) + if not ok then + errlog:push(result) + end end ]]):format(package.cpath, package.path, requestName, responseName) log.debug('Create thread, id: ', id) @@ -55,7 +51,11 @@ end } end -local function call(dump, env, callback) +local function run(name, arg, callback) + local dump = io.load(ROOT / 'src' / 'async' / (name .. '.lua')) + if not dump then + error(('找不到[%s]'):format(name)) + end local task = table.remove(idlePool) if not task then task = createTask() @@ -64,7 +64,9 @@ local function call(dump, env, callback) task = task, callback = callback, } - task.request:push(dump, env) + task.request:push(dump, arg) + -- TODO 线程回收后禁止外部再使用通道 + return task.request, task.response end local function onTick() @@ -73,17 +75,18 @@ local function onTick() log.error(msg) end for id, running in pairs(runningList) do - local ok, results = running.task.response:pop() - if ok then - runningList[id] = nil - idlePool[#idlePool+1] = running.task - xpcall(running.callback, log.debug, table.unpack(results)) + if running.callback then + local ok, result = running.task.response:pop() + if ok then + runningList[id] = nil + idlePool[#idlePool+1] = running.task + xpcall(running.callback, log.error, result) + end end end end return { - onTick = onTick, - call = call, - require = require, + onTick = onTick, + run = run, } diff --git a/server/src/async/proto.lua b/server/src/async/proto.lua index 4570de2e..ef0b3300 100644 --- a/server/src/async/proto.lua +++ b/server/src/async/proto.lua @@ -1,7 +1,4 @@ -local thread = require 'bee.thread' local json = require 'json' -local proto = thread.channel 'proto' -local errlog = thread.channel 'errlog' local function pushError(...) local t = table.pack(...) @@ -9,7 +6,7 @@ local function pushError(...) t[i] = tostring(t[i]) end local buf = table.concat(t, '\t') - errlog:push(buf) + ERR:push(buf) end local function readProtoHeader() @@ -51,7 +48,7 @@ local function readProto() if not data then return end - proto:push(data) + OUT:push(data) end while true do diff --git a/server/src/async/scanfiles.lua b/server/src/async/scanfiles.lua new file mode 100644 index 00000000..900cc0a8 --- /dev/null +++ b/server/src/async/scanfiles.lua @@ -0,0 +1,17 @@ +local root = ... + +require 'utility' +local fs = require 'bee.filesystem' +local list = {} +local ignore = { + ['.git'] = true, + ['node_modules'] = true, +} + +for path in io.scan(fs.path(root), ignore) do + if path:extension():string() == '.lua' then + list[#list+1] = path:string() + end +end + +OUT:push(list) diff --git a/server/src/method/textDocument/didClose.lua b/server/src/method/textDocument/didClose.lua index 7d097407..d4edb624 100644 --- a/server/src/method/textDocument/didClose.lua +++ b/server/src/method/textDocument/didClose.lua @@ -1,6 +1,5 @@ return function (lsp, params) local doc = params.textDocument lsp:removeText(doc.uri, doc.version) - lsp:close(doc.uri) return true end diff --git a/server/src/method/textDocument/didOpen.lua b/server/src/method/textDocument/didOpen.lua index b6710d6b..27fdda71 100644 --- a/server/src/method/textDocument/didOpen.lua +++ b/server/src/method/textDocument/didOpen.lua @@ -1,6 +1,5 @@ return function (lsp, params) local doc = params.textDocument lsp:saveText(doc.uri, doc.version, doc.text) - lsp:open(doc.uri) return true end diff --git a/server/src/service.lua b/server/src/service.lua index d0089c1b..78cadd38 100644 --- a/server/src/service.lua +++ b/server/src/service.lua @@ -7,8 +7,6 @@ local parser = require 'parser' local core = require 'core' local lang = require 'language' -thread.newchannel 'proto' - local ErrorCodes = { -- Defined by JSON RPC ParseError = -32700, @@ -432,9 +430,8 @@ function mt:listen() io.stdin:setvbuf 'no' io.stdout:setvbuf 'no' - self._proto = thread.channel 'proto' - - async.call([[require 'async.proto']]) + local _, out = async.run 'proto' + self._proto = out while true do async.onTick() diff --git a/server/src/workspace.lua b/server/src/workspace.lua index 5582ca8d..5d541ad2 100644 --- a/server/src/workspace.lua +++ b/server/src/workspace.lua @@ -77,23 +77,8 @@ function mt:init(rootUri) local logPath = ROOT / 'log' / (rootUri:gsub('[/:]+', '_') .. '.log') log.info('Log path: ', logPath) log.init(ROOT, logPath) - async.call([[ - require 'utility' - local fs = require 'bee.filesystem' - local list = {} - local ignore = { - ['.git'] = true, - ['node_modules'] = true, - } - for path in io.scan(fs.path(ROOT)) do - if path:extension():string() == '.lua' then - list[#list+1] = path:string() - end - end - return list - ]], { - ROOT = self.root:string() - }, function (list) + + async.run('scanfiles', self.root:string(), function (list) log.info(('Found [%d] files'):format(#list)) local ignored = {} for name in pairs(config.config.workspace.ignoreDir) do |