@@ -16,6 +16,7 @@ local scope = require 'workspace.scope'
1616local lazy = require ' lazytable'
1717local cacher = require ' lazy-cacher'
1818local sp = require ' bee.subprocess'
19+ local pub = require ' pub'
1920
2021--- @class file
2122--- @field uri uri
@@ -554,10 +555,13 @@ function m.compileStateThen(state, file)
554555end
555556
556557--- @param uri uri
557- --- @param file file
558558--- @param async boolean ?
559559--- @return parser.state ?
560- function m .compileState (uri , file , async )
560+ function m .compileState (uri , async )
561+ local file = m .fileMap [uri ]
562+ if not file then
563+ return
564+ end
561565 if file .state then
562566 return file .state
563567 end
@@ -586,17 +590,41 @@ function m.compileState(uri, file, async)
586590 end
587591 return nil
588592 end
593+
594+ --- @type brave.param.compile.options
595+ local options = {
596+ special = config .get (uri , ' Lua.runtime.special' ),
597+ unicodeName = config .get (uri , ' Lua.runtime.unicodeName' ),
598+ nonstandardSymbol = util .arrayToHash (config .get (uri , ' Lua.runtime.nonstandardSymbol' )),
599+ }
600+
601+ if async then
602+ --- @type brave.param.compile
603+ local params = {
604+ text = file .text ,
605+ mode = ' Lua' ,
606+ version = config .get (uri , ' Lua.runtime.version' ),
607+ options = options
608+ }
609+ pub .task (' compile' , params , function (result )
610+ if file .text ~= params .text then
611+ return
612+ end
613+ if not result .state then
614+ log .error (' Compile failed:' , uri , result .err )
615+ return
616+ end
617+ m .compileStateThen (result .state , file )
618+ end )
619+ end
620+
589621 local prog <close> = progress .create (uri , lang .script .WINDOW_COMPILING , 0.5 )
590622 prog :setMessage (ws .getRelativePath (uri ))
591623 local clock = os.clock ()
592624 local state , err = parser .compile (file .text
593625 , ' Lua'
594626 , config .get (uri , ' Lua.runtime.version' )
595- , {
596- special = config .get (uri , ' Lua.runtime.special' ),
597- unicodeName = config .get (uri , ' Lua.runtime.unicodeName' ),
598- nonstandardSymbol = util .arrayToHash (config .get (uri , ' Lua.runtime.nonstandardSymbol' )),
599- }
627+ , options
600628 )
601629 local passed = os.clock () - clock
602630 if passed > 0.1 then
@@ -626,7 +654,7 @@ function m.getState(uri)
626654 if not file then
627655 return nil
628656 end
629- local state = m .compileState (uri , file )
657+ local state = m .compileState (uri )
630658 file .cacheActiveTime = timer .clock ()
631659 return state
632660end
0 commit comments