diff options
Diffstat (limited to 'script/files.lua')
-rw-r--r-- | script/files.lua | 117 |
1 files changed, 43 insertions, 74 deletions
diff --git a/script/files.lua b/script/files.lua index 074a262e..d18fade3 100644 --- a/script/files.lua +++ b/script/files.lua @@ -64,6 +64,7 @@ function m.open(uri) cache = {}, } m.onWatch('open', uri) + m.addRef(uri) end --- 关闭文件 @@ -75,6 +76,7 @@ function m.close(uri) file.trusted = false end m.onWatch('close', uri) + m.delRef(uri) end --- 是否打开 @@ -93,8 +95,12 @@ function m.getOpenedCache(uri) end --- 标记为库文件 -function m.setLibraryPath(uri, libraryPath) - m.libraryMap[uri] = libraryPath +---@param scp scope +---@param uri uri +---@param libraryUri uri +function m.setLibraryUri(scp, uri, libraryUri) + scp:get 'libraryMap' [uri] = libraryUri + scp:addLink(uri) end --- 是否是库文件 @@ -107,8 +113,9 @@ function m.getLibraryPath(uri) return m.libraryMap[uri] end -function m.flushAllLibrary() - m.libraryMap = {} +---@param scp scope +function m.flushAllLibrary(scp) + scp:set('libraryMap', {}) end --- 是否存在 @@ -174,7 +181,7 @@ function m.setText(uri, text, isTrust, version) return end if not isTrust then - local encoding = config.get 'Lua.runtime.fileEncoding' + local encoding = config.get(uri, 'Lua.runtime.fileEncoding') text = encoder.decode(encoding, text) end file.version = version @@ -344,6 +351,28 @@ function m.getChildFiles(uri) return results end +function m.addRef(uri) + local file = m.fileMap[uri] + if not file then + return nil + end + file._ref = (file._ref or 0) + 1 + return function () + m.delRef(uri) + end +end + +function m.delRef(uri) + local file = m.fileMap[uri] + if not file then + return + end + file._ref = (file._ref or 0) - 1 + if file._ref <= 0 then + m.remove(uri) + end +end + --- 移除文件 ---@param uri uri function m.remove(uri) @@ -355,7 +384,6 @@ function m.remove(uri) m.fileMap[uri] = nil m.astMap[uri] = nil m._pairsCache = nil - m.flushFileCache(uri) m.fileCount = m.fileCount - 1 m.globalVersion = m.globalVersion + 1 @@ -365,43 +393,6 @@ function m.remove(uri) m.onWatch('remove', originUri) end ---- 移除所有文件 -function m.removeAll() - local ws = require 'workspace.workspace' - m.globalVersion = m.globalVersion + 1 - await.close('files.version') - m.onWatch('version') - m._pairsCache = nil - for uri in pairs(m.fileMap) do - if not m.libraryMap[uri] then - m.fileCount = m.fileCount - 1 - m.fileMap[uri] = nil - m.astMap[uri] = nil - m.onWatch('remove', uri) - end - end - ws.flushCache() - --m.notifyCache = {} -end - ---- 移除所有关闭的文件 -function m.removeAllClosed() - m.globalVersion = m.globalVersion + 1 - await.close('files.version') - m.onWatch('version') - m._pairsCache = nil - for uri in pairs(m.fileMap) do - if not m.openMap[uri] - and not m.libraryMap[uri] then - m.fileCount = m.fileCount - 1 - m.fileMap[uri] = nil - m.astMap[uri] = nil - m.onWatch('remove', uri) - end - end - --m.notifyCache = {} -end - --- 获取一个包含所有文件uri的数组 ---@return uri[] function m.getAllUris() @@ -452,7 +443,7 @@ function m.compileState(uri, text) local client = require 'client' if not m.isOpen(uri) and not m.isLibrary(uri) - and #text >= config.get 'Lua.workspace.preloadFileSize' * 1000 then + and #text >= config.get(uri, 'Lua.workspace.preloadFileSize') * 1000 then if not m.notifyCache['preloadFileSize'] then m.notifyCache['preloadFileSize'] = {} m.notifyCache['skipLargeFileCount'] = 0 @@ -462,7 +453,7 @@ function m.compileState(uri, text) m.notifyCache['skipLargeFileCount'] = m.notifyCache['skipLargeFileCount'] + 1 local message = lang.script('WORKSPACE_SKIP_LARGE_FILE' , ws.getRelativePath(uri) - , config.get 'Lua.workspace.preloadFileSize' + , config.get(uri, 'Lua.workspace.preloadFileSize') , #text / 1000 ) if m.notifyCache['skipLargeFileCount'] <= 1 then @@ -478,11 +469,11 @@ function m.compileState(uri, text) local clock = os.clock() local state, err = parser.compile(text , 'Lua' - , config.get 'Lua.runtime.version' + , config.get(uri, 'Lua.runtime.version') , { - special = config.get 'Lua.runtime.special', - unicodeName = config.get 'Lua.runtime.unicodeName', - nonstandardSymbol = config.get 'Lua.runtime.nonstandardSymbol', + special = config.get(uri, 'Lua.runtime.special'), + unicodeName = config.get(uri, 'Lua.runtime.unicodeName'), + nonstandardSymbol = config.get(uri, 'Lua.runtime.nonstandardSymbol'), } ) local passed = os.clock() - clock @@ -633,13 +624,9 @@ function m.getCache(uri) end --- 获取文件关联 -function m.getAssoc() - if m.assocVersion == config.get 'version' then - return m.assocMatcher - end - m.assocVersion = config.get 'version' +function m.getAssoc(uri) local patt = {} - for k, v in pairs(config.get 'files.associations') do + for k, v in pairs(config.get(uri, 'files.associations')) do if v == 'lua' then patt[#patt+1] = k end @@ -659,7 +646,7 @@ function m.isLua(uri) if ext == 'lua' then return true end - local matcher = m.getAssoc() + local matcher = m.getAssoc(uri) local path = furi.decode(uri) return matcher(path) end @@ -752,24 +739,6 @@ function m.onWatch(ev, uri) end end -function m.flushCache() - for uri, file in pairs(m.fileMap) do - file.cacheActiveTime = math.huge - m.astMap[uri] = nil - file.cache = {} - end -end - -function m.flushFileCache(uri) - local file = m.fileMap[uri] - if not file then - return - end - file.cacheActiveTime = math.huge - m.astMap[uri] = nil - file.cache = {} -end - function m.init() --TODO 可以清空文件缓存,之后看要不要启用吧 --timer.loop(10, function () |