Skip to content

Commit 518b602

Browse files
committed
cleanup
1 parent 4d36079 commit 518b602

File tree

3 files changed

+85
-57
lines changed

3 files changed

+85
-57
lines changed

script/brave/work.lua

Lines changed: 23 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ brave.on('loadFile', function (path)
2626
return util.loadFile(path)
2727
end)
2828

29-
brave.on('removeCaches', function(path)
29+
brave.on('removeCaches', function (path)
3030
local fs = require 'bee.filesystem'
3131
local fsu = require 'fs-utility'
3232
for dir in fs.pairs(fs.path(path)) do
@@ -38,3 +38,25 @@ brave.on('removeCaches', function(path)
3838
end
3939
end
4040
end)
41+
42+
---@class brave.param.compile
43+
---@field text string
44+
---@field mode string
45+
---@field version string
46+
---@field options brave.param.compile.options
47+
48+
---@class brave.param.compile.options
49+
---@field special table<string, string>
50+
---@field unicodeName boolean
51+
---@field nonstandardSymbol table<string, true>
52+
53+
---@param param brave.param.compile
54+
brave.on('compile', function (param)
55+
local parser = require 'parser'
56+
local state, err = parser.compile(param.text
57+
, param.mode
58+
, param.version
59+
, param.options
60+
)
61+
return state, err
62+
end)

script/files.lua

Lines changed: 61 additions & 55 deletions
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,6 @@ function m.reset()
4949
m.globalVersion = 0
5050
m.fileCount = 0
5151
m.astCount = 0
52-
m.astMap = {}
5352
end
5453

5554
m.reset()
@@ -252,7 +251,7 @@ function m.setText(uri, text, isTrust, callback)
252251
file.originText = text
253252
file.rows = nil
254253
file.words = nil
255-
m.astMap[uri] = nil
254+
file.state = nil
256255
file.cache = {}
257256
file.cacheActiveTime = math.huge
258257
m.globalVersion = m.globalVersion + 1
@@ -296,7 +295,7 @@ function m.setRawText(uri, text)
296295
local file = m.fileMap[uri]
297296
file.text = text
298297
file.originText = text
299-
m.astMap[uri] = nil
298+
file.state = nil
300299
end
301300

302301
function m.getCachedRows(uri)
@@ -440,7 +439,6 @@ function m.remove(uri)
440439
return
441440
end
442441
m.fileMap[uri] = nil
443-
m.astMap[uri] = nil
444442
m._pairsCache = nil
445443

446444
m.fileCount = m.fileCount - 1
@@ -508,12 +506,60 @@ function m.getLazyCache()
508506
return m.lazyCache
509507
end
510508

511-
function m.compileState(uri, text)
509+
---@param state parser.state
510+
---@param file file
511+
function m.compileStateThen(state, file)
512+
file.state = state
513+
514+
state.uri = file.uri
515+
state.lua = file.text
516+
state.ast.uri = file.uri
517+
state._diffInfo = file._diffInfo
518+
state.originLines = file.originLines
519+
state.originText = file.originText
520+
521+
local clock = os.clock()
522+
parser.luadoc(state)
523+
local passed = os.clock() - clock
524+
if passed > 0.1 then
525+
log.warn(('Parse LuaDoc of [%s] takes [%.3f] sec, size [%.3f] kb.'):format(file.uri, passed, #file.text / 1000))
526+
end
527+
528+
if LAZY and not file.trusted then
529+
local cache = m.getLazyCache()
530+
local id = ('%d'):format(file.id)
531+
clock = os.clock()
532+
state = lazy.build(state, cache:writterAndReader(id)):entry()
533+
passed = os.clock() - clock
534+
if passed > 0.1 then
535+
log.warn(('Convert lazy-table for [%s] takes [%.3f] sec, size [%.3f] kb.'):format(file.uri, passed, #file.text / 1000))
536+
end
537+
else
538+
m.astCount = m.astCount + 1
539+
local removed
540+
setmetatable(state, {__gc = function ()
541+
if removed then
542+
return
543+
end
544+
removed = true
545+
m.astCount = m.astCount - 1
546+
end})
547+
end
548+
end
549+
550+
---@param uri uri
551+
---@param file file
552+
---@param async boolean?
553+
---@return parser.state?
554+
function m.compileState(uri, file, async)
555+
if file.state then
556+
return file.state
557+
end
512558
local ws = require 'workspace'
513559
local client = require 'client'
514560
if not m.isOpen(uri)
515561
and not m.isLibrary(uri)
516-
and #text >= config.get(uri, 'Lua.workspace.preloadFileSize') * 1000 then
562+
and #file.text >= config.get(uri, 'Lua.workspace.preloadFileSize') * 1000 then
517563
if not m.notifyCache['preloadFileSize'] then
518564
m.notifyCache['preloadFileSize'] = {}
519565
m.notifyCache['skipLargeFileCount'] = 0
@@ -524,7 +570,7 @@ function m.compileState(uri, text)
524570
local message = lang.script('WORKSPACE_SKIP_LARGE_FILE'
525571
, ws.getRelativePath(uri)
526572
, config.get(uri, 'Lua.workspace.preloadFileSize')
527-
, #text / 1000
573+
, #file.text / 1000
528574
)
529575
if m.notifyCache['skipLargeFileCount'] <= 1 then
530576
client.showMessage('Info', message)
@@ -537,7 +583,7 @@ function m.compileState(uri, text)
537583
local prog <close> = progress.create(uri, lang.script.WINDOW_COMPILING, 0.5)
538584
prog:setMessage(ws.getRelativePath(uri))
539585
local clock = os.clock()
540-
local state, err = parser.compile(text
586+
local state, err = parser.compile(file.text
541587
, 'Lua'
542588
, config.get(uri, 'Lua.runtime.version')
543589
, {
@@ -548,48 +594,17 @@ function m.compileState(uri, text)
548594
)
549595
local passed = os.clock() - clock
550596
if passed > 0.1 then
551-
log.warn(('Compile [%s] takes [%.3f] sec, size [%.3f] kb.'):format(uri, passed, #text / 1000))
597+
log.warn(('Compile [%s] takes [%.3f] sec, size [%.3f] kb.'):format(uri, passed, #file.text / 1000))
552598
end
553-
--await.delay()
554-
if state then
555-
state.uri = uri
556-
state.lua = text
557-
state.ast.uri = uri
558-
559-
local clock = os.clock()
560-
parser.luadoc(state)
561-
local passed = os.clock() - clock
562-
if passed > 0.1 then
563-
log.warn(('Parse LuaDoc of [%s] takes [%.3f] sec, size [%.3f] kb.'):format(uri, passed, #text / 1000))
564-
end
565-
566-
local file = m.fileMap[uri]
567-
if LAZY and not file.trusted then
568-
local cache = m.getLazyCache()
569-
local id = ('%d'):format(file.id)
570-
clock = os.clock()
571-
state = lazy.build(state, cache:writterAndReader(id)):entry()
572-
passed = os.clock() - clock
573-
if passed > 0.1 then
574-
log.warn(('Convert lazy-table for [%s] takes [%.3f] sec, size [%.3f] kb.'):format(uri, passed, #text / 1000))
575-
end
576-
else
577-
m.astCount = m.astCount + 1
578-
local removed
579-
setmetatable(state, {__gc = function ()
580-
if removed then
581-
return
582-
end
583-
removed = true
584-
m.astCount = m.astCount - 1
585-
end})
586-
end
587599

588-
return state
589-
else
600+
if not state then
590601
log.error('Compile failed:', uri, err)
591602
return nil
592603
end
604+
605+
m.compileStateThen(state, file)
606+
607+
return state
593608
end
594609

595610
---@class parser.state
@@ -605,16 +620,7 @@ function m.getState(uri)
605620
if not file then
606621
return nil
607622
end
608-
local state = m.astMap[uri]
609-
if not state then
610-
state = m.compileState(uri, file.text)
611-
m.astMap[uri] = state
612-
file.state = state
613-
state._diffInfo = file._diffInfo
614-
state.originLines = file.originLines
615-
state.originText = file.originText
616-
--await.delay()
617-
end
623+
local state = m.compileState(uri, file)
618624
file.cacheActiveTime = timer.clock()
619625
return state
620626
end

script/service/service.lua

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -260,7 +260,7 @@ end
260260
function m.start()
261261
util.enableCloseFunction()
262262
await.setErrorHandle(log.error)
263-
pub.recruitBraves(4)
263+
pub.recruitBraves(8)
264264
proto.listen()
265265
m.report()
266266
m.testVersion()

0 commit comments

Comments
 (0)