@@ -63,6 +63,14 @@ class Worksheet implements Disposable {
63
63
*/
64
64
private canceller ?: CancellationTokenSource = undefined
65
65
66
+ /**
67
+ * The edits that should be applied to this worksheet.
68
+ *
69
+ * This is used to ensure that the blank lines added to fit the output of the worksheet
70
+ * are inserted in the same order as the output arrived.
71
+ */
72
+ private applyEdits : Promise < void > = Promise . resolve ( )
73
+
66
74
constructor ( readonly document : vscode . TextDocument , readonly client : BaseLanguageClient ) {
67
75
}
68
76
@@ -73,7 +81,7 @@ class Worksheet implements Disposable {
73
81
this . canceller = undefined
74
82
}
75
83
this . _onDidStateChange . dispose ( )
76
- }
84
+ }
77
85
78
86
/** Remove all decorations, and resets this worksheet. */
79
87
private reset ( ) : void {
@@ -83,6 +91,7 @@ class Worksheet implements Disposable {
83
91
this . decoratedLines . clear ( )
84
92
this . runVersion = - 1
85
93
this . margin = this . longestLine ( ) + 5
94
+ this . applyEdits = Promise . resolve ( )
86
95
}
87
96
88
97
/**
@@ -111,6 +120,13 @@ class Worksheet implements Disposable {
111
120
return this . canceller != undefined
112
121
}
113
122
123
+ /** Display the output in the worksheet's editor. */
124
+ handleMessage ( output : WorksheetPublishOutputParams , editor : vscode . TextEditor ) {
125
+ this . applyEdits = this . applyEdits . then ( ( ) => {
126
+ this . displayAndSaveResult ( output . line - 1 , output . content , editor )
127
+ } )
128
+ }
129
+
114
130
/**
115
131
* Run the worksheet in `document`, if a previous run is in progress, it is
116
132
* cancelled first.
@@ -160,10 +176,10 @@ class Worksheet implements Disposable {
160
176
* @param runResult The result itself.
161
177
* @param worksheet The worksheet that receives the result.
162
178
* @param editor The editor where to display the result.
163
- * @return A `Thenable ` that will insert necessary lines to fit the output
179
+ * @return A `Promise ` that will insert necessary lines to fit the output
164
180
* and display the decorations upon completion.
165
181
*/
166
- public displayAndSaveResult ( lineNumber : number , runResult : string , editor : vscode . TextEditor ) {
182
+ public async displayAndSaveResult ( lineNumber : number , runResult : string , editor : vscode . TextEditor ) : Promise < void > {
167
183
const resultLines = runResult . trim ( ) . split ( / \r \n | \r | \n / g)
168
184
169
185
// The line where the next decoration should be put.
@@ -183,21 +199,18 @@ class Worksheet implements Disposable {
183
199
this . runVersion += 1
184
200
}
185
201
186
- return vscode . workspace . applyEdit ( addNewLinesEdit ) . then ( _ => {
187
- for ( let line of resultLines ) {
188
- const decorationPosition = new vscode . Position ( actualLine , 0 )
189
- const decorationMargin = this . margin - editor . document . lineAt ( actualLine ) . text . length
190
- const decorationType = this . createDecoration ( decorationMargin , line )
191
- const decorationOptions = { range : new vscode . Range ( decorationPosition , decorationPosition ) , hoverMessage : line }
192
- const decoration = new Decoration ( decorationType , decorationOptions )
193
-
194
- this . decoratedLines . add ( actualLine )
195
- this . decorations . push ( decoration )
196
-
197
- editor . setDecorations ( decorationType , [ decorationOptions ] )
198
- actualLine += 1
199
- }
200
- } )
202
+ await vscode . workspace . applyEdit ( addNewLinesEdit ) ;
203
+ for ( let line of resultLines ) {
204
+ const decorationPosition = new vscode . Position ( actualLine , 0 ) ;
205
+ const decorationMargin = this . margin - editor . document . lineAt ( actualLine ) . text . length ;
206
+ const decorationType = this . createDecoration ( decorationMargin , line ) ;
207
+ const decorationOptions = { range : new vscode . Range ( decorationPosition , decorationPosition ) , hoverMessage : line } ;
208
+ const decoration = new Decoration ( decorationType , decorationOptions ) ;
209
+ this . decoratedLines . add ( actualLine ) ;
210
+ this . decorations . push ( decoration ) ;
211
+ editor . setDecorations ( decorationType , [ decorationOptions ] ) ;
212
+ actualLine += 1 ;
213
+ }
201
214
}
202
215
203
216
/**
@@ -409,7 +422,7 @@ export class WorksheetProvider implements Disposable {
409
422
* Handle the result of running part of a worksheet.
410
423
* This is called when we receive a `worksheet/publishOutput`.
411
424
*
412
- * @param message The result of running part of a worksheet.
425
+ * @param output The result of running part of a worksheet.
413
426
*/
414
427
private handleMessage ( output : WorksheetPublishOutputParams ) {
415
428
const editor = vscode . window . visibleTextEditors . find ( e => {
@@ -420,7 +433,7 @@ export class WorksheetProvider implements Disposable {
420
433
if ( editor ) {
421
434
const worksheet = this . worksheetFor ( editor . document )
422
435
if ( worksheet ) {
423
- worksheet . displayAndSaveResult ( output . line - 1 , output . content , editor )
436
+ worksheet . handleMessage ( output , editor )
424
437
}
425
438
}
426
439
}
0 commit comments