Skip to content

Commit 3d80e21

Browse files
committed
cleanup
1 parent 35d7ad6 commit 3d80e21

File tree

2 files changed

+26
-26
lines changed

2 files changed

+26
-26
lines changed

script/files.lua

Lines changed: 25 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,6 @@ local pub = require 'pub'
2828
---@field text string
2929
---@field version? integer
3030
---@field originLines? integer[]
31-
---@field state? parser.state
3231
---@field diffInfo? table[]
3332
---@field cache table
3433
---@field id integer
@@ -49,7 +48,10 @@ function m.reset()
4948
m.visible = {}
5049
m.globalVersion = 0
5150
m.fileCount = 0
52-
m.astCount = 0
51+
---@type table<uri, parser.state>
52+
m.stateMap = setmetatable({}, util.MODE_V)
53+
---@type table<parser.state, true>
54+
m.stateTrace = setmetatable({}, util.MODE_K)
5355
end
5456

5557
m.reset()
@@ -251,12 +253,12 @@ function m.setText(uri, text, isTrust, callback)
251253
end
252254
local clock = os.clock()
253255
local newText = pluginOnSetText(file, text)
256+
m.stateMap[uri] = nil
254257
file.text = newText
255258
file.trusted = isTrust
256259
file.originText = text
257260
file.rows = nil
258261
file.words = nil
259-
file.state = nil
260262
file.cache = {}
261263
file.cacheActiveTime = math.huge
262264
m.globalVersion = m.globalVersion + 1
@@ -301,10 +303,10 @@ function m.setRawText(uri, text)
301303
if not text then
302304
return
303305
end
306+
m.stateMap[uri] = nil
304307
local file = m.fileMap[uri]
305308
file.text = text
306309
file.originText = text
307-
file.state = nil
308310
end
309311

310312
function m.getCachedRows(uri)
@@ -458,6 +460,7 @@ function m.remove(uri)
458460
return
459461
end
460462
m.fileMap[uri] = nil
463+
m.stateMap[uri] = nil
461464
m._pairsCache = nil
462465

463466
m.fileCount = m.fileCount - 1
@@ -528,8 +531,8 @@ end
528531
---@param state parser.state
529532
---@param file file
530533
function m.compileStateThen(state, file)
531-
file.state = state
532-
534+
m.stateTrace[state] = true
535+
m.stateMap[file.uri] = state
533536
state.uri = file.uri
534537
state.lua = file.text
535538
state.ast.uri = file.uri
@@ -553,16 +556,6 @@ function m.compileStateThen(state, file)
553556
if passed > 0.1 then
554557
log.warn(('Convert lazy-table for [%s] takes [%.3f] sec, size [%.3f] kb.'):format(file.uri, passed, #file.text / 1000))
555558
end
556-
else
557-
m.astCount = m.astCount + 1
558-
local removed
559-
setmetatable(state, {__gc = function ()
560-
if removed then
561-
return
562-
end
563-
removed = true
564-
m.astCount = m.astCount - 1
565-
end})
566559
end
567560
end
568561

@@ -609,8 +602,8 @@ function m.compileStateAsync(uri, callback)
609602
callback(nil)
610603
return
611604
end
612-
if file.state then
613-
callback(file.state)
605+
if m.stateMap[uri] then
606+
callback(m.stateMap[uri])
614607
return
615608
end
616609

@@ -650,8 +643,8 @@ function m.compileState(uri)
650643
if not file then
651644
return
652645
end
653-
if file.state then
654-
return file.state
646+
if m.stateMap[uri] then
647+
return m.stateMap[uri]
655648
end
656649
if not m.checkPreload(uri) then
657650
return
@@ -711,12 +704,10 @@ function m.getState(uri)
711704
return state
712705
end
713706

707+
---@param uri uri
708+
---@return parser.state?
714709
function m.getLastState(uri)
715-
local file = m.fileMap[uri]
716-
if not file then
717-
return nil
718-
end
719-
return file.state
710+
return m.stateMap[uri]
720711
end
721712

722713
function m.getFile(uri)
@@ -873,6 +864,15 @@ function m.getDllWords(uri)
873864
return file.words
874865
end
875866

867+
---@return integer
868+
function m.countStates()
869+
local n = 0
870+
for _ in pairs(m.stateTrace) do
871+
n = n + 1
872+
end
873+
return n
874+
end
875+
876876
--- 注册事件
877877
---@param callback async fun(ev: string, uri: uri)
878878
function m.watch(callback)

script/service/service.lua

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -211,7 +211,7 @@ function m.reportStatus()
211211

212212
local tooltips = {}
213213
local params = {
214-
ast = files.astCount,
214+
ast = files.countStates(),
215215
max = files.fileCount,
216216
mem = collectgarbage('count') / 1000,
217217
}

0 commit comments

Comments
 (0)