@@ -214,6 +214,12 @@ local function pluginOnSetText(file, text)
214214 return text
215215end
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 )
312318end
313319
314320function 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 )
564576end
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
709720end
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
770780end
771781
@@ -891,23 +901,4 @@ function m.onWatch(ev, uri)
891901 end
892902end
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-
913904return m
0 commit comments