summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
author最萌小汐 <sumneko@hotmail.com>2021-12-30 17:11:43 +0800
committer最萌小汐 <sumneko@hotmail.com>2021-12-30 17:11:43 +0800
commitd726459e81cfb9081da4f29eb8d25d73b0fda759 (patch)
tree514ef6e3e49123bc68e162741e3a3166f779dcbb
parenta48139e5dd7e7a6e9e3bd00a41334a1e66d1a227 (diff)
downloadlua-language-server-d726459e81cfb9081da4f29eb8d25d73b0fda759.zip
update
-rw-r--r--script/workspace/loading.lua16
-rw-r--r--script/workspace/workspace.lua38
2 files changed, 38 insertions, 16 deletions
diff --git a/script/workspace/loading.lua b/script/workspace/loading.lua
index c3e8c00c..cbcfa567 100644
--- a/script/workspace/loading.lua
+++ b/script/workspace/loading.lua
@@ -11,6 +11,7 @@ local pub = require 'pub.pub'
---@field _bar progress
---@field _stash function[]
---@field _refs uri[]
+---@field _cache table<uri, boolean>
local mt = {}
mt.__index = mt
@@ -58,7 +59,7 @@ end
---@param uri uri
---@param libraryUri boolean
---@async
-function mt:scanFile(uri, libraryUri)
+function mt:loadFile(uri, libraryUri)
if files.isLua(uri) then
if not libraryUri then
self.preload = self.preload + 1
@@ -75,8 +76,11 @@ function mt:scanFile(uri, libraryUri)
if not content then
return
end
+ if self._cache[uri] then
+ return
+ end
log.info(('Preload file at: %s , size = %.3f KB'):format(uri, #content / 1024.0))
- table.insert(self.scp:get 'cachedUris', uri)
+ self._cache[uri] = true
files.setText(uri, content, false)
files.addRef(uri)
if libraryUri then
@@ -95,8 +99,11 @@ function mt:scanFile(uri, libraryUri)
if not content then
return
end
+ if self._cache[uri] then
+ return
+ end
log.info(('Preload dll at: %s , size = %.3f KB'):format(uri, #content / 1024.0))
- table.insert(self.scp:get 'cachedUris', uri)
+ self._cache[uri] = true
files.saveDll(uri, content)
files.addRef(uri)
if libraryUri then
@@ -154,8 +161,9 @@ function m.create(scp)
scp = scp,
_bar = progress.create(lang.script('WORKSPACE_LOADING', scp.uri)),
_stash = {},
+ _cache = {},
}, mt)
- scp:set('cachedUris', {})
+ scp:set('cachedUris', loading._cache)
return loading
end
diff --git a/script/workspace/workspace.lua b/script/workspace/workspace.lua
index 4d904773..2376f1e8 100644
--- a/script/workspace/workspace.lua
+++ b/script/workspace/workspace.lua
@@ -213,11 +213,24 @@ function m.awaitLoadFile(uri)
log.info('Scan files at:', uri)
---@async
native:scan(furi.decode(uri), function (path)
- ld:scanFile(furi.encode(path))
+ ld:loadFile(furi.encode(path))
end)
ld:loadAll()
end
+function m.removeFile(uri)
+ for _, scp in ipairs(m.folders) do
+ if scp:isChildUri(uri)
+ or scp:isLinkedUri(uri) then
+ local cachedUris = scp:get 'cachedUris'
+ if cachedUris[uri] then
+ cachedUris[uri] = nil
+ files.delRef(uri)
+ end
+ end
+ end
+end
+
--- 预读工作区内所有文件
---@async
---@param scp scope
@@ -247,7 +260,7 @@ function m.awaitPreload(scp)
log.info('Scan files at:', m.rootUri)
---@async
native:scan(furi.decode(scp.uri), function (path)
- ld:scanFile(furi.encode(path))
+ ld:loadFile(furi.encode(path))
end)
end
@@ -255,7 +268,7 @@ function m.awaitPreload(scp)
log.info('Scan library at:', libMatcher.uri)
---@async
libMatcher.matcher:scan(furi.decode(libMatcher.uri), function (path)
- ld:scanFile(furi.encode(path), libMatcher.uri)
+ ld:loadFile(furi.encode(path), libMatcher.uri)
end)
watchers[#watchers+1] = fw.watch(furi.decode(libMatcher.uri))
end
@@ -386,13 +399,13 @@ function m.init()
end
---@param scp scope
-function m.removeFiles(scp)
+function m.flushFiles(scp)
local cachedUris = scp:get 'cachedUris'
if not cachedUris then
return
end
scp:set('cachedUris', nil)
- for _, uri in ipairs(cachedUris) do
+ for uri in pairs(cachedUris) do
files.delRef(uri)
end
end
@@ -403,7 +416,7 @@ function m.resetFiles(scp)
if not cachedUris then
return
end
- for _, uri in ipairs(cachedUris) do
+ for uri in pairs(cachedUris) do
files.resetText(uri)
end
end
@@ -411,7 +424,7 @@ end
---@async
---@param scp scope
function m.awaitReload(scp)
- m.removeFiles(scp)
+ m.flushFiles(scp)
plugin.init(scp)
m.awaitPreload(scp)
scp:set('ready', true)
@@ -483,20 +496,18 @@ fw.event(function (changes) ---@async
for _, change in ipairs(changes) do
local path = change.path
local uri = furi.encode(path)
- local scp = m.getScope(uri)
- if scp.type == 'fallback' then
- goto CONTINUE
- end
if change.type == 'create' then
log.debug('FileChangeType.Created', uri)
m.awaitLoadFile(uri)
elseif change.type == 'delete' then
log.debug('FileChangeType.Deleted', uri)
files.remove(uri)
+ m.removeFile(uri)
local childs = files.getChildFiles(uri)
for _, curi in ipairs(childs) do
log.debug('FileChangeType.Deleted.Child', curi)
files.remove(curi)
+ m.removeFile(uri)
end
elseif change.type == 'change' then
if m.isValidLuaUri(uri) then
@@ -509,7 +520,10 @@ fw.event(function (changes) ---@async
-- 排除类文件发生更改需要重新扫描
if filename == '.gitignore'
or filename == '.gitmodules' then
- m.reload(scp)
+ local scp = m.getScope(uri)
+ if scp.type ~= 'fallback' then
+ m.reload(scp)
+ end
break
end
end