summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--script/files.lua52
-rw-r--r--script/plugin.lua14
-rw-r--r--script/workspace/loading.lua30
-rw-r--r--script/workspace/workspace.lua69
-rw-r--r--test.lua2
5 files changed, 94 insertions, 73 deletions
diff --git a/script/files.lua b/script/files.lua
index 415dc2fb..ead1ea90 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,11 @@ function m.getOpenedCache(uri)
end
--- 标记为库文件
-function m.setLibraryUri(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
end
--- 是否是库文件
@@ -107,8 +112,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
--- 是否存在
@@ -344,6 +350,25 @@ function m.getChildFiles(uri)
return results
end
+function m.addRef(uri)
+ local file = m.fileMap[uri]
+ if not file then
+ return
+ end
+ file._ref = (file._ref or 0) + 1
+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 +380,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
@@ -752,24 +776,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 ()
diff --git a/script/plugin.lua b/script/plugin.lua
index 88c4578f..0914c0c0 100644
--- a/script/plugin.lua
+++ b/script/plugin.lua
@@ -43,13 +43,6 @@ function m.isReady()
return m.interface ~= nil
end
-local function resetFiles()
- local files = require 'files'
- for uri in files.eachFile() do
- files.resetText(uri)
- end
-end
-
---@async
local function checkTrustLoad()
local filePath = LOGPATH .. '/trusted'
@@ -75,7 +68,8 @@ local function checkTrustLoad()
return true
end
-function m.init()
+---@param scp scope
+function m.init(scp)
if m.hasInited then
return
end
@@ -84,7 +78,7 @@ function m.init()
local ws = require 'workspace'
m.interface = {}
- local pluginPath = ws.getAbsolutePath(config.get(nil, 'Lua.runtime.plugin'))
+ local pluginPath = ws.getAbsolutePath(scp.uri, config.get(scp.uri, 'Lua.runtime.plugin'))
log.info('plugin path:', pluginPath)
if not pluginPath then
return
@@ -110,7 +104,7 @@ function m.init()
return
end
- resetFiles()
+ ws.resetFiles(scp)
end)
end
diff --git a/script/workspace/loading.lua b/script/workspace/loading.lua
index 6a17d933..c3e8c00c 100644
--- a/script/workspace/loading.lua
+++ b/script/workspace/loading.lua
@@ -10,6 +10,7 @@ local pub = require 'pub.pub'
---@field scp scope
---@field _bar progress
---@field _stash function[]
+---@field _refs uri[]
local mt = {}
mt.__index = mt
@@ -75,27 +76,33 @@ function mt:scanFile(uri, libraryUri)
return
end
log.info(('Preload file at: %s , size = %.3f KB'):format(uri, #content / 1024.0))
+ table.insert(self.scp:get 'cachedUris', uri)
+ files.setText(uri, content, false)
+ files.addRef(uri)
if libraryUri then
log.info('++++As library of:', libraryUri)
- files.setLibraryUri(uri, libraryUri)
+ files.setLibraryUri(self.scp, 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)
+ self._stash[#self._stash+1] = function ()
+ 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))
+ table.insert(self.scp:get 'cachedUris', uri)
+ files.saveDll(uri, content)
+ files.addRef(uri)
+ if libraryUri then
+ log.info('++++As library of:', libraryUri)
+ end
end
- files.saveDll(uri, content)
end)
await.delay()
end
@@ -148,6 +155,7 @@ function m.create(scp)
_bar = progress.create(lang.script('WORKSPACE_LOADING', scp.uri)),
_stash = {},
}, mt)
+ scp:set('cachedUris', {})
return loading
end
diff --git a/script/workspace/workspace.lua b/script/workspace/workspace.lua
index 6630e432..12971ae5 100644
--- a/script/workspace/workspace.lua
+++ b/script/workspace/workspace.lua
@@ -6,11 +6,7 @@ local config = require 'config'
local glob = require 'glob'
local platform = require 'bee.platform'
local await = require 'await'
-local proto = require 'proto.proto'
-local lang = require 'language'
local library = require 'library'
-local progress = require 'progress'
-local define = require "proto.define"
local client = require 'client'
local plugin = require 'plugin'
local util = require 'utility'
@@ -24,7 +20,6 @@ m.type = 'workspace'
function m.initRoot(uri)
m.rootUri = uri
- m.rootPath = furi.decode(uri)
log.info('Workspace init root: ', uri)
local logPath = fs.path(LOGPATH) / (uri:gsub('[/:]+', '_') .. '.log')
@@ -86,7 +81,7 @@ function m.getNativeMatcher(scp)
end
-- config.get(nil, 'workspace.useGitIgnore'
if config.get(scp.uri, 'Lua.workspace.useGitIgnore') then
- local buf = pub.awaitTask('loadFile', furi.encode(m.rootPath .. '/.gitignore'))
+ local buf = pub.awaitTask('loadFile', m.rootUri .. '/.gitignore')
if buf then
for line in buf:gmatch '[^\r\n]+' do
if line:sub(1, 1) ~= '#' then
@@ -95,7 +90,7 @@ function m.getNativeMatcher(scp)
end
end
end
- buf = pub.awaitTask('loadFile', furi.encode(m.rootPath .. '/.git/info/exclude'))
+ buf = pub.awaitTask('loadFile', m.rootUri .. '/.git/info/exclude')
if buf then
for line in buf:gmatch '[^\r\n]+' do
if line:sub(1, 1) ~= '#' then
@@ -107,7 +102,7 @@ function m.getNativeMatcher(scp)
end
-- config.get(nil, 'workspace.ignoreSubmodules'
if config.get(scp.uri, 'Lua.workspace.ignoreSubmodules') then
- local buf = pub.awaitTask('loadFile', furi.encode(m.rootPath .. '/.gitmodules'))
+ local buf = pub.awaitTask('loadFile', m.rootUri .. '/.gitmodules')
if buf then
for path in buf:gmatch('path = ([^\r\n]+)') do
log.info('Ignore by .gitmodules:', path)
@@ -179,9 +174,11 @@ end
--- 文件是否被忽略
---@async
+---@param uri uri
function m.isIgnored(uri)
- local path = m.getRelativePath(uri)
- local ignore = m.getNativeMatcher()
+ local scp = m.getScope(uri)
+ local path = m.getRelativePath(uri)
+ local ignore = m.getNativeMatcher(scp)
if not ignore then
return false
end
@@ -238,7 +235,7 @@ function m.awaitPreload(scp)
local librarys = m.getLibraryMatchers(scp)
do
- log.info('Scan files at:', m.rootPath)
+ log.info('Scan files at:', m.rootUri)
---@async
native:scan(furi.decode(scp.uri), function (path)
ld:scanFile(furi.encode(path))
@@ -398,17 +395,20 @@ end
---@param uriOrPath uri|string
---@return string
function m.getRelativePath(uriOrPath)
- local path
+ local path, uri
if uriOrPath:sub(1, 5) == 'file:' then
path = furi.decode(uriOrPath)
+ uri = uriOrPath
else
path = uriOrPath
+ uri = furi.encode(uriOrPath)
end
- if not m.rootPath then
+ local scp = m.getScope(uri)
+ if not scp.uri then
local relative = m.normalize(path)
return relative:gsub('^[/\\]+', '')
end
- local _, pos = m.normalize(path):find(m.rootPath, 1, true)
+ local _, pos = m.normalize(path):find(furi.decode(scp.uri), 1, true)
if pos then
return m.normalize(path:sub(pos + 1)):gsub('^[/\\]+', '')
else
@@ -416,14 +416,6 @@ function m.getRelativePath(uriOrPath)
end
end
-function m.isWorkspaceUri(uri)
- if not m.rootUri then
- return false
- end
- local ruri = m.rootUri
- return uri:sub(1, #ruri) == ruri
-end
-
--- 获取工作区等级的缓存
function m.getCache(name)
if not m.cache[name] then
@@ -464,12 +456,33 @@ function m.init()
end
end
+---@param scp scope
+function m.removeFiles(scp)
+ local cachedUris = scp:get 'cachedUris'
+ if not cachedUris then
+ return
+ end
+ scp:set('cachedUris', nil)
+ for _, uri in ipairs(cachedUris) do
+ files.delRef(uri)
+ end
+end
+
+---@param scp scope
+function m.resetFiles(scp)
+ local cachedUris = scp:get 'cachedUris'
+ if not cachedUris then
+ return
+ end
+ for _, uri in ipairs(cachedUris) do
+ files.resetText(uri)
+ end
+end
+
---@async
---@param scp scope
function m.awaitReload(scp)
- files.flushAllLibrary(scp)
- files.removeAllClosed(scp)
- files.flushCache(scp)
+ m.removeFiles(scp)
plugin.init(scp)
m.awaitPreload(scp)
scp:set('ready', true)
@@ -538,8 +551,8 @@ fw.event(function (changes) ---@async
for _, change in ipairs(changes) do
local path = change.path
local uri = furi.encode(path)
- if not m.isWorkspaceUri(uri)
- and not files.isLibrary(uri) then
+ local scp = m.getScope(uri)
+ if scp.type == 'fallback' then
goto CONTINUE
end
if change.type == 'create' then
@@ -564,7 +577,7 @@ fw.event(function (changes) ---@async
-- 排除类文件发生更改需要重新扫描
if filename == '.gitignore'
or filename == '.gitmodules' then
- m.reload()
+ m.reload(scp)
break
end
end
diff --git a/test.lua b/test.lua
index d26a6438..8bf17e8b 100644
--- a/test.lua
+++ b/test.lua
@@ -41,7 +41,7 @@ local function loadDocMetas()
for _, path in ipairs(library.metaPaths) do
local uri = furi.encode(path)
files.setText(uri, fsu.loadFile(path))
- files.setLibraryPath(uri, library.metaPath)
+ files.addRef(uri)
end
end