diff options
author | sumneko <sumneko@hotmail.com> | 2021-11-28 02:53:00 +0800 |
---|---|---|
committer | sumneko <sumneko@hotmail.com> | 2021-11-28 02:53:00 +0800 |
commit | 74bcaaff8846baa997d1594fb8086ca59bc0e9ff (patch) | |
tree | 3468e9b38fb09315ab0c23364207e730fb1aff79 | |
parent | 6b7bbbd97cd8166d0371ec235771ff14ce5e3cf8 (diff) | |
download | lua-language-server-74bcaaff8846baa997d1594fb8086ca59bc0e9ff.zip |
update
-rw-r--r-- | script/client.lua | 34 | ||||
-rw-r--r-- | script/files.lua | 2 | ||||
-rw-r--r-- | script/workspace/loading.lua | 101 | ||||
-rw-r--r-- | script/workspace/workspace.lua | 135 |
4 files changed, 137 insertions, 135 deletions
diff --git a/script/client.lua b/script/client.lua index c4e545f4..a0935df6 100644 --- a/script/client.lua +++ b/script/client.lua @@ -6,6 +6,7 @@ local define = require 'proto.define' local config = require 'config' local converter = require 'proto.converter' local json = require 'json-beautify' +local await = require 'await' local m = {} @@ -108,10 +109,8 @@ end ---@param type message.type ---@param message string ---@param titles string[] ----@return string action ----@return integer index ----@async -function m.awaitRequestMessage(type, message, titles) +---@param callback fun(action: string, index: integer) +function m.requestMessage(type, message, titles, callback) proto.notify('window/logMessage', { type = define.MessageType[type] or 3, message = message, @@ -124,15 +123,29 @@ function m.awaitRequestMessage(type, message, titles) } map[title] = i end - local item = proto.awaitRequest('window/showMessageRequest', { + proto.request('window/showMessageRequest', { type = define.MessageType[type] or 3, message = message, actions = actions, - }) - if not item then - return nil - end - return item.title, map[item.title] + }, function (item) + if item then + callback(item.title, map[item.title]) + else + callback(nil, nil) + end + end) +end + +---@param type message.type +---@param message string +---@param titles string[] +---@return string action +---@return integer index +---@async +function m.awaitRequestMessage(type, message, titles) + return await.wait(function (waker) + m.requestMessage(type, message, titles, waker) + end) end ---@param type message.type @@ -289,7 +302,6 @@ function m.setConfig(changes, onlyMemory) finalChanges[#finalChanges+1] = change end end - change.uri = m.info.rootUri end if onlyMemory then return diff --git a/script/files.lua b/script/files.lua index 00e76623..415dc2fb 100644 --- a/script/files.lua +++ b/script/files.lua @@ -93,7 +93,7 @@ function m.getOpenedCache(uri) end --- 标记为库文件 -function m.setLibraryPath(uri, libraryPath) +function m.setLibraryUri(uri, libraryPath) m.libraryMap[uri] = libraryPath end diff --git a/script/workspace/loading.lua b/script/workspace/loading.lua index 1142b943..6a17d933 100644 --- a/script/workspace/loading.lua +++ b/script/workspace/loading.lua @@ -1,8 +1,13 @@ local progress = require 'progress' local lang = require 'language' local await = require 'await' +local files = require 'files' +local config = require 'config.config' +local client = require 'client' +local pub = require 'pub.pub' ---@class workspace.loading +---@field scp scope ---@field _bar progress ---@field _stash function[] local mt = {} @@ -18,7 +23,85 @@ function mt:update() self._bar:setPercentage(self.read / self.max * 100.0) end -function mt:load() +---@param uri uri +function mt:checkMaxPreload(uri) + local max = config.get(uri, 'Lua.workspace.maxPreload') + if self.preload <= max then + return true + end + if self.scp:get 'hasHintedMaxPreload' then + return false + end + self.scp:set('hasHintedMaxPreload', true) + client.requestMessage('Info' + , lang.script('MWS_MAX_PRELOAD', max) + , { + lang.script + } + , function (_, index) + if index == 1 then + client.setConfig { + { + key = 'Lua.workspace.maxPreload', + uri = self.scp.uri, + action = 'set', + value = max + math.max(1000, max), + } + } + end + end + ) + return false +end + +---@param uri uri +---@param libraryUri boolean +---@async +function mt:scanFile(uri, libraryUri) + if files.isLua(uri) then + if not libraryUri then + self.preload = self.preload + 1 + if not self:checkMaxPreload(uri) then + return + end + end + self.max = self.max + 1 + self:update() + pub.task('loadFile', uri, function (content) + self._stash[#self._stash+1] = function () + self.read = self.read + 1 + self:update() + if not content then + return + end + log.info(('Preload file at: %s , size = %.3f KB'):format(uri, #content / 1024.0)) + if libraryUri then + log.info('++++As library of:', libraryUri) + files.setLibraryUri(uri, libraryUri) + end + files.setText(uri, content, false) + end + end) + elseif files.isDll(uri) then + self.max = self.max + 1 + self:update() + pub.task('loadFile', uri, function (content) + self.read = self.read + 1 + self:update() + if not content then + return + end + log.info(('Preload dll at: %s , size = %.3f KB'):format(uri, #content / 1024.0)) + if libraryUri then + log.info('++++As library of:', libraryUri) + end + files.saveDll(uri, content) + end) + await.delay() + end +end + +function mt:loadStashed() self:update() if self._loadLock then return @@ -39,21 +122,14 @@ function mt:load() end ---@async -function mt:loadAll(callback) - while true do - callback() - self:update() - if self.read >= self.max then - break - end +function mt:loadAll() + while self.read < self.max do + log.info(('Loaded %d/%d files'):format(self.read, self.max)) + self:loadStashed() await.sleep(0.1) end end -function mt:isFinished() - return self.read >= self.max -end - function mt:remove() self._bar:remove() end @@ -68,6 +144,7 @@ local m = {} ---@return workspace.loading function m.create(scp) local loading = setmetatable({ + scp = scp, _bar = progress.create(lang.script('WORKSPACE_LOADING', scp.uri)), _stash = {}, }, mt) diff --git a/script/workspace/workspace.lua b/script/workspace/workspace.lua index a40f2a3d..6630e432 100644 --- a/script/workspace/workspace.lua +++ b/script/workspace/workspace.lua @@ -70,6 +70,7 @@ local globInteferFace = { --- 创建排除文件匹配器 ---@async +---@param scp scope function m.getNativeMatcher(scp) if scp:get 'nativeMatcher' then return scp:get 'nativeMatcher' @@ -199,113 +200,23 @@ function m.isValidLuaUri(uri) return true end -local function loadFileFactory(scp, progressData, isLibrary) - return function (path) ---@async - local uri = furi.encode(path) - if files.isLua(uri) then - if not isLibrary and progressData.preload >= config.get(nil, 'Lua.workspace.maxPreload') then - if not m.hasHitMaxPreload then - m.hasHitMaxPreload = true - proto.request('window/showMessageRequest', { - type = define.MessageType.Info, - message = lang.script('MWS_MAX_PRELOAD', config.get(nil, 'Lua.workspace.maxPreload')), - actions = { - { - title = lang.script.WINDOW_INCREASE_UPPER_LIMIT, - }, - { - title = lang.script.WINDOW_CLOSE, - } - } - }, function (item) - if not item then - return - end - if item.title == lang.script.WINDOW_INCREASE_UPPER_LIMIT then - client.setConfig { - { - key = 'Lua.workspace.maxPreload', - action = 'set', - value = config.get(nil, 'Lua.workspace.maxPreload') - + math.max(1000, config.get(nil, 'Lua.workspace.maxPreload')), - } - } - end - end) - end - return - end - if not isLibrary then - progressData.preload = progressData.preload + 1 - end - progressData.max = progressData.max + 1 - progressData:update() - pub.task('loadFile', uri, function (text) - local loader = function () - if text then - log.info(('Preload file at: %s , size = %.3f KB'):format(uri, #text / 1024.0)) - if isLibrary then - log.info('++++As library of:', root) - files.setLibraryPath(uri, root) - end - files.setText(uri, text, false) - else - files.remove(uri) - end - progressData.read = progressData.read + 1 - progressData:update() - end - if progressData.loaders then - progressData.loaders[#progressData.loaders+1] = loader - else - loader() - end - end) - end - if files.isDll(uri) then - progressData.max = progressData.max + 1 - progressData:update() - pub.task('loadFile', uri, function (content) - if content then - log.info(('Preload file at: %s , size = %.3f KB'):format(uri, #content / 1024.0)) - if isLibrary then - log.info('++++As library of:', root) - end - files.saveDll(uri, content) - end - progressData.read = progressData.read + 1 - progressData:update() - end) - end - await.delay() - end -end - ---@async function m.awaitLoadFile(uri) - local progressBar <close> = progress.create(lang.script.WORKSPACE_LOADING) - local progressData = { - max = 0, - read = 0, - preload = 0, - update = function (self) - progressBar:setMessage(('%d/%d'):format(self.read, self.max)) - progressBar:setPercentage(self.read / self.max * 100) - end - } - local nativeLoader = loadFileFactory(m.rootPath, progressData) - local native = m.getNativeMatcher() - if native then - log.info('Scan files at:', m.rootPath) - native:scan(furi.decode(uri), nativeLoader) - end + local scp = m.getScope(uri) + local ld <close> = loading.create(scp) + local native = m.getNativeMatcher(scp) + log.info('Scan files at:', uri) + ---@async + native:scan(furi.decode(uri), function (path) + ld:scanFile(furi.encode(path)) + end) + ld:loadAll() end --- 预读工作区内所有文件 ---@async ---@param scp scope function m.awaitPreload(scp) - local diagnostic = require 'provider.diagnostic' await.close 'preload' await.setID 'preload' await.sleep(0.1) @@ -323,26 +234,28 @@ function m.awaitPreload(scp) log.info('Preload start:', scp.uri) - local nativeLoader = loadFileFactory(scp, ld) - local native = m.getNativeMatcher(scp) - local librarys = m.getLibraryMatchers(scp) - if native then + local native = m.getNativeMatcher(scp) + local librarys = m.getLibraryMatchers(scp) + + do log.info('Scan files at:', m.rootPath) - native:scan(m.rootPath, nativeLoader) + ---@async + native:scan(furi.decode(scp.uri), function (path) + ld:scanFile(furi.encode(path)) + end) end + for _, libMatcher in ipairs(librarys) do - local libraryLoader = loadFileFactory(scp, ld, libMatcher.uri) log.info('Scan library at:', libMatcher.uri) - libMatcher.matcher:scan(furi.decode(libMatcher.uri), libraryLoader) + ---@async + libMatcher.matcher:scan(furi.decode(libMatcher.uri), function (path) + ld:scanFile(furi.encode(path), libMatcher.uri) + end) watchers[#watchers+1] = fw.watch(furi.decode(libMatcher.uri)) end log.info(('Found %d files.'):format(ld.max)) - while not ld:isFinished() do - log.info(('Loaded %d/%d files'):format(ld.read, ld.max)) - ld:load() - await.sleep(0.1) - end + ld:loadAll() log.info('Preload finish.') end |