Skip to content

Commit 3b526aa

Browse files
committed
simple state persistence
1 parent 89a1c80 commit 3b526aa

File tree

8 files changed

+29
-39
lines changed

8 files changed

+29
-39
lines changed

script/files.lua

Lines changed: 22 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -214,6 +214,12 @@ local function pluginOnSetText(file, text)
214214
return text
215215
end
216216

217+
---@param file file
218+
function m.removeState(file)
219+
file.state = nil
220+
m.stateMap[file.uri] = nil
221+
end
222+
217223
--- 设置文件文本
218224
---@param uri uri
219225
---@param text? string
@@ -255,14 +261,14 @@ function m.setText(uri, text, isTrust, callback)
255261
end
256262
local clock = os.clock()
257263
local newText = pluginOnSetText(file, text)
258-
m.stateMap[uri] = nil
259-
file.text = newText
260-
file.trusted = isTrust
261-
file.originText = text
262-
file.rows = nil
263-
file.words = nil
264-
file.cache = {}
265-
file.cacheActiveTime = math.huge
264+
m.removeState(file)
265+
file.text = newText
266+
file.trusted = isTrust
267+
file.originText = text
268+
file.rows = nil
269+
file.words = nil
270+
file.compileCount = 0
271+
file.cache = {}
266272
m.globalVersion = m.globalVersion + 1
267273
m.onWatch('version', uri)
268274
if create then
@@ -305,10 +311,10 @@ function m.setRawText(uri, text)
305311
if not text then
306312
return
307313
end
308-
m.stateMap[uri] = nil
309314
local file = m.fileMap[uri]
310315
file.text = text
311316
file.originText = text
317+
m.removeState(file)
312318
end
313319

314320
function m.getCachedRows(uri)
@@ -461,8 +467,8 @@ function m.remove(uri)
461467
if not file then
462468
return
463469
end
470+
m.removeState(file)
464471
m.fileMap[uri] = nil
465-
m.stateMap[uri] = nil
466472
m._pairsCache = nil
467473

468474
m.fileCount = m.fileCount - 1
@@ -560,6 +566,12 @@ function m.compileStateThen(state, file)
560566
end
561567
end
562568

569+
file.compileCount = file.compileCount + 1
570+
if file.compileCount >= 3 then
571+
file.state = state
572+
log.debug('State persistence:', file.uri)
573+
end
574+
563575
m.onWatch('compile', file.uri)
564576
end
565577

@@ -704,7 +716,6 @@ function m.getState(uri)
704716
return nil
705717
end
706718
local state = m.compileState(uri)
707-
file.cacheActiveTime = timer.clock()
708719
return state
709720
end
710721

@@ -765,7 +776,6 @@ function m.getCache(uri)
765776
if not file then
766777
return nil
767778
end
768-
--file.cacheActiveTime = timer.clock()
769779
return file.cache
770780
end
771781

@@ -891,23 +901,4 @@ function m.onWatch(ev, uri)
891901
end
892902
end
893903

894-
function m.init()
895-
--TODO 可以清空文件缓存,之后看要不要启用吧
896-
--timer.loop(10, function ()
897-
-- local list = {}
898-
-- for _, file in pairs(m.fileMap) do
899-
-- if timer.clock() - file.cacheActiveTime > 10.0 then
900-
-- file.cacheActiveTime = math.huge
901-
-- file.ast = nil
902-
-- file.cache = {}
903-
-- list[#list+1] = file.uri
904-
-- end
905-
-- end
906-
-- if #list > 0 then
907-
-- log.info('Flush file caches:', #list, '\n', table.concat(list, '\n'))
908-
-- collectgarbage()
909-
-- end
910-
--end)
911-
end
912-
913904
return m

script/provider/provider.lua

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -134,7 +134,6 @@ m.register 'initialize' {
134134
m.register 'initialized'{
135135
---@async
136136
function (params)
137-
files.init()
138137
local _ <close> = progress.create(workspace.getFirstScope().uri, lang.script.WINDOW_INITIALIZING, 0.5)
139138
m.updateConfig()
140139
local registrations = {}
@@ -277,7 +276,7 @@ m.register 'textDocument/didOpen' {
277276
end)
278277
files.open(uri)
279278
workspace.awaitReady(uri)
280-
files.getState(uri)
279+
files.compileState(uri)
281280
end
282281
}
283282

script/workspace/loading.lua

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -99,7 +99,7 @@ function mt:loadFile(uri, libraryUri)
9999
-- self._sets[#self._sets+1] = waker
100100
--end)
101101
files.setText(uri, content, false)
102-
files.getState(uri)
102+
files.compileState(uri)
103103
if not self._cache[uri] then
104104
files.addRef(uri)
105105
end

test/crossfile/completion.lua

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -69,7 +69,7 @@ function TEST(data)
6969
mainUri = uri
7070
end
7171
files.setText(uri, script)
72-
files.getState(uri)
72+
files.compileState(uri)
7373
end
7474

7575
local _ <close> = function ()

test/crossfile/definition.lua

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,7 @@ function TEST(datas)
5353
sourceUri = uri
5454
end
5555
files.setText(uri, newScript)
56-
files.getState(uri)
56+
files.compileState(uri)
5757
end
5858

5959
local _ <close> = function ()

test/crossfile/diagnostic.lua

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@ function TEST(datas)
4747
end
4848
data.content = newScript
4949
files.setText(uri, newScript)
50-
files.getState(uri)
50+
files.compileState(uri)
5151
end
5252

5353
local _ <close> = function ()

test/crossfile/hover.lua

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@ function TEST(expect)
4444
local script, list = catch(file.content, '?')
4545
local uri = furi.encode(file.path)
4646
files.setText(uri, script)
47-
files.getState(uri)
47+
files.compileState(uri)
4848
if #list['?'] > 0 then
4949
sourceUri = uri
5050
sourcePos = (list['?'][1][1] + list['?'][1][2]) // 2

test/crossfile/references.lua

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -74,7 +74,7 @@ function TEST(datas)
7474
sourceUri = uri
7575
end
7676
files.setText(uri, newScript)
77-
files.getState(uri)
77+
files.compileState(uri)
7878
end
7979

8080
local _ <close> = function ()

0 commit comments

Comments
 (0)