@@ -55,7 +55,7 @@ export type ModuleModel = {
5555 selections : any [ ] ;
5656 currentLine : number ;
5757 path : string ;
58- model : Promise < any > ;
58+ model : null | any ;
5959 comments : Array < { commentId : string ; range : [ number , number ] } > ;
6060 currentCommentDecorations : string [ ] ;
6161} ;
@@ -162,7 +162,7 @@ export class ModelsHandler {
162162
163163 this . updateUserSelections ( module , moduleModel . selections ) ;
164164
165- const model = await moduleModel . model ;
165+ const model = moduleModel . model ;
166166
167167 if ( COMMENTS ) {
168168 const newDecorationComments = this . createCommentDecorations (
@@ -200,10 +200,10 @@ export class ModelsHandler {
200200 } ) ;
201201
202202 // Apply the decorations
203- Object . keys ( this . moduleModels ) . forEach ( async path => {
203+ Object . keys ( this . moduleModels ) . forEach ( path => {
204204 const moduleModel = this . moduleModels [ path ] ;
205205 const relativePath = path . replace ( '/sandbox' , '' ) ;
206- const model = await moduleModel . model ;
206+ const model = moduleModel . model ;
207207
208208 if ( ! model ) {
209209 return ;
@@ -232,9 +232,9 @@ export class ModelsHandler {
232232 const newModelPath = '/sandbox' + newPath ;
233233
234234 return Promise . all (
235- Object . keys ( this . moduleModels ) . map ( async path => {
235+ Object . keys ( this . moduleModels ) . map ( path => {
236236 if ( oldModelPath === path && this . moduleModels [ path ] . model ) {
237- const model = await this . moduleModels [ path ] . model ;
237+ const model = this . moduleModels [ path ] . model ;
238238
239239 // This runs remove/add automatically
240240 return this . editorApi . textFileService . move (
@@ -266,17 +266,17 @@ export class ModelsHandler {
266266 // the model is actually resolved. This creates a "natural" queue
267267 if ( ! moduleModel . model ) {
268268 if ( modelEditor ) {
269- moduleModel . model = modelEditor . textModelReference . then (
269+ moduleModel . model = await modelEditor . textModelReference . then (
270270 ref => ref . object . textEditorModel
271271 ) ;
272272 } else {
273- moduleModel . model = this . editorApi . textFileService . models
273+ moduleModel . model = await this . editorApi . textFileService . models
274274 . loadOrCreate ( this . monaco . Uri . file ( moduleModel . path ) )
275275 . then ( model => model . textEditorModel ) ;
276276 }
277277 }
278278
279- const model = await moduleModel . model ;
279+ const model = moduleModel . model ;
280280
281281 this . isApplyingOperation = true ;
282282 this . applyOperationToModel ( operation , false , model ) ;
@@ -289,33 +289,43 @@ export class ModelsHandler {
289289 } ) ;
290290 }
291291
292- public async setModuleCode ( module : Module ) {
292+ /**
293+ * Sets the code of a model in VSCode. This means that we directly change the in-memory
294+ * model and the user will immediately see the code.
295+ * @param module The module to apply the changes of
296+ * @param triggerChangeEvent Whether we should trigger this event to listeners listening to the model (for eg. live)
297+ */
298+ public setModuleCode ( module : Module , triggerChangeEvent = false ) {
293299 const moduleModel = this . getModuleModelByPath ( module . path ) ;
294- const model = await moduleModel . model ;
300+ const model = moduleModel . model ;
295301
296302 if ( ! model ) {
297303 return ;
298304 }
299305
300306 const oldCode = model . getValue ( ) ;
301307 const changeOperation = getTextOperation ( oldCode , module . code ) ;
302- this . isApplyingOperation = true ;
308+ if ( ! triggerChangeEvent ) {
309+ this . isApplyingOperation = true ;
310+ }
303311 this . applyOperationToModel ( changeOperation , false , model ) ;
304- this . isApplyingOperation = false ;
312+ if ( ! triggerChangeEvent ) {
313+ this . isApplyingOperation = false ;
314+ }
305315 }
306316
307317 public clearUserSelections ( userId : string ) {
308318 const decorations = Object . keys ( this . userSelectionDecorations ) . filter ( d =>
309319 d . startsWith ( userId )
310320 ) ;
311- Object . keys ( this . moduleModels ) . forEach ( async key => {
321+ Object . keys ( this . moduleModels ) . forEach ( key => {
312322 const moduleModel = this . moduleModels [ key ] ;
313323
314324 if ( ! moduleModel . model ) {
315325 return ;
316326 }
317327
318- const model = await moduleModel . model ;
328+ const model = moduleModel . model ;
319329
320330 decorations . forEach ( decorationId => {
321331 if ( decorationId . startsWith ( userId + model . id ) ) {
@@ -330,7 +340,7 @@ export class ModelsHandler {
330340
331341 nameTagTimeouts : { [ name : string ] : number } = { } ;
332342
333- public async updateUserSelections (
343+ public updateUserSelections (
334344 module : Module ,
335345 userSelections : EditorSelection [ ] ,
336346 showNameTag = true
@@ -343,7 +353,7 @@ export class ModelsHandler {
343353 return ;
344354 }
345355
346- const model = await moduleModel . model ;
356+ const model = moduleModel . model ;
347357 const lines = model . getLinesContent ( ) || [ ] ;
348358
349359 userSelections . forEach ( ( data : EditorSelection ) => {
@@ -551,7 +561,7 @@ export class ModelsHandler {
551561 }
552562
553563 private applyOperationToModel (
554- operation ,
564+ operation : TextOperation ,
555565 pushStack = false ,
556566 model = this . editorApi . getActiveCodeEditor ( ) . getModel ( )
557567 ) {
@@ -574,8 +584,9 @@ export class ModelsHandler {
574584 for ( let i = 0 ; i < operation . ops . length ; i ++ ) {
575585 const op = operation . ops [ i ] ;
576586 if ( TextOperation . isRetain ( op ) ) {
577- index += op ;
587+ index += op as number ;
578588 } else if ( TextOperation . isInsert ( op ) ) {
589+ const textOp = op as string ;
579590 const { lineNumber, column } = indexToLineAndColumn (
580591 model . getValue ( ) . split ( / \n / ) || [ ] ,
581592 index
@@ -588,8 +599,8 @@ export class ModelsHandler {
588599 ) ;
589600
590601 // if there's a new line
591- if ( / \n / . test ( op ) ) {
592- const eol = / \r \n / . test ( op ) ? 2 : 1 ;
602+ if ( / \n / . test ( textOp ) ) {
603+ const eol = / \r \n / . test ( textOp ) ? 2 : 1 ;
593604 if ( eol !== currentEOLLength ) {
594605 // With this insert the EOL of the document changed on the other side. We need
595606 // to accomodate our EOL to it.
@@ -599,13 +610,14 @@ export class ModelsHandler {
599610
600611 results . push ( {
601612 range,
602- text : op ,
613+ text : textOp ,
603614 forceMoveMarkers : true ,
604615 } ) ;
605616 } else if ( TextOperation . isDelete ( op ) ) {
617+ const delOp = op as number ;
606618 const lines = model . getValue ( ) . split ( / \n / ) || [ ] ;
607619 const from = indexToLineAndColumn ( lines , index ) ;
608- const to = indexToLineAndColumn ( lines , index - op ) ;
620+ const to = indexToLineAndColumn ( lines , index - delOp ) ;
609621 results . push ( {
610622 range : new this . monaco . Range (
611623 from . lineNumber ,
@@ -615,7 +627,7 @@ export class ModelsHandler {
615627 ) ,
616628 text : '' ,
617629 } ) ;
618- index -= op ;
630+ index -= delOp ;
619631 }
620632 }
621633
0 commit comments