diff options
Diffstat (limited to 'server-beta/src')
-rw-r--r-- | server-beta/src/brave/init.lua | 30 | ||||
-rw-r--r-- | server-beta/src/proto/provider.lua | 5 | ||||
-rw-r--r-- | server-beta/src/pub/pub.lua | 4 | ||||
-rw-r--r-- | server-beta/src/workspace/workspace.lua | 29 |
4 files changed, 67 insertions, 1 deletions
diff --git a/server-beta/src/brave/init.lua b/server-beta/src/brave/init.lua index cea5e1cf..4616110f 100644 --- a/server-beta/src/brave/init.lua +++ b/server-beta/src/brave/init.lua @@ -1,6 +1,9 @@ local brave = require 'brave.brave' local jsonrpc = require 'jsonrpc' local parser = require 'parser' +local fs = require 'bee.filesystem' +local furi = require 'file-uri' +local util = require 'utility' brave.on('loadProto', function () while true do @@ -25,4 +28,31 @@ brave.on('compile', function (text) } end) +brave.on('listDirectory', function (uri) + local path = fs.path(furi.decode(uri)) + local uris = {} + local dirs = {} + for child in path:list_directory() do + local childUri = furi.encode(child:string()) + uris[#uris+1] = childUri + if fs.is_directory(child) then + dirs[childUri] = true + end + end + return { + uris = uris, + dirs = dirs, + } +end) + +brave.on('loadFile', function (uri) + local filename = furi.decode(uri) + return util.loadFile(filename) +end) + +brave.on('saveFile', function (params) + local filename = furi.decode(params.uri) + return util.saveFile(filename, params.text) +end) + return brave diff --git a/server-beta/src/proto/provider.lua b/server-beta/src/proto/provider.lua index 01121a15..0b9c285e 100644 --- a/server-beta/src/proto/provider.lua +++ b/server-beta/src/proto/provider.lua @@ -8,11 +8,14 @@ local inte = require 'proto.interface' local workspace = require 'workspace' proto.on('initialize', function (params) - log.debug(util.dump(params)) + --log.debug(util.dump(params)) if params.workspaceFolders then local name = params.workspaceFolders[1].name local uri = params.workspaceFolders[1].uri workspace.init(name, uri) + task.create(function () + workspace.preload() + end) end return { capabilities = cap.initer, diff --git a/server-beta/src/pub/pub.lua b/server-beta/src/pub/pub.lua index b077ad9f..ca9d7382 100644 --- a/server-beta/src/pub/pub.lua +++ b/server-beta/src/pub/pub.lua @@ -64,6 +64,7 @@ function m.pushTask(brave, info) return end brave.taskpad:push(info.name, info.id, info.params) + log.info(('Push task %q to # %d, queue length %d'):format(info.name, brave.id, #m.taskQueue)) return task.wait(function (waker) info.callback = waker brave.taskMap[info.id] = info @@ -77,6 +78,7 @@ function m.pushSyncTask(brave, info) end brave.taskpad:push(info.name, info.id, info.params) brave.taskMap[info.id] = info + log.info(('Push task %q to # %d, queue length %d'):format(info.name, brave.id, #m.taskQueue)) end --- 从勇者处接收任务反馈 @@ -122,6 +124,7 @@ function m.task(name, params) -- 当有勇者提交任务反馈后,尝试把按顺序将堆积任务 -- 交给该勇者 m.taskQueue[#m.taskQueue+1] = info + log.info(('Add task %q in queue, length %d.'):format(name, #m.taskQueue)) end --- 发布同步任务,如果任务进入了队列,会返回执行器 @@ -146,6 +149,7 @@ function m.syncTask(name, params, callback) -- 当有勇者提交任务反馈后,尝试把按顺序将堆积任务 -- 交给该勇者 m.taskQueue[#m.taskQueue+1] = info + log.info(('Add task %q in queue, length %d.'):format(name, #m.taskQueue)) return info end diff --git a/server-beta/src/workspace/workspace.lua b/server-beta/src/workspace/workspace.lua index d3669031..8508e5c5 100644 --- a/server-beta/src/workspace/workspace.lua +++ b/server-beta/src/workspace/workspace.lua @@ -1,9 +1,38 @@ +local pub = require 'pub' +local fs = require 'bee.filesystem' +local furi = require 'file-uri' + local m = {} m.type = 'workspace' +--- 初始化工作区 function m.init(name, uri) m.name = name m.uri = uri end +--- 预读工作区内所有文件(异步) +function m.preload() + log.info('Preload start.') + local function scan(dir, callback) + local result = pub.task('listDirectory', dir) + if not result then + return + end + for i = 1, #result.uris do + local childUri = result.uris[i] + if result.dirs[childUri] then + scan(childUri, callback) + else + callback(childUri) + end + end + end + scan(m.uri, function (uri) + local text = pub.task('loadFile', uri) + log.debug('Preload file at: ' .. uri, #text) + end) + log.info('Preload finish.') +end + return m |