1
1
import * as vscode from 'vscode'
2
+ import { TextEdit } from 'vscode'
2
3
3
4
import {
4
- asWorksheetRunParams , WorksheetRunRequest , WorksheetRunParams ,
5
+ asWorksheetRunParams , WorksheetRunRequest , WorksheetRunParams , WorksheetRunResult ,
5
6
WorksheetPublishOutputParams , WorksheetPublishOutputNotification
6
7
} from './protocol'
7
8
import { BaseLanguageClient , DocumentSelector } from 'vscode-languageclient'
@@ -41,24 +42,35 @@ class Worksheet {
41
42
42
43
/**
43
44
* Reset the "worksheet state" (margin and number of inserted lines), and
44
- * removes redundant blank lines that have been inserted by a previous
45
- * run.
45
+ * return an array of TextEdit that remove the redundant blank lines that have
46
+ * been inserted by a previous run.
46
47
*/
47
- prepareRun ( ) : void {
48
- this . removeRedundantBlankLines ( ) . then ( _ => this . reset ( ) )
48
+ prepareRun ( ) : TextEdit [ ] {
49
+ const edits = this . removeRedundantBlankLinesEdits ( )
50
+ this . reset ( )
51
+ return edits
49
52
}
50
53
51
54
/**
52
55
* Run the worksheet in `document`, display a progress bar during the run.
53
56
*/
54
- run ( ) : Thenable < { } > {
55
- this . prepareRun ( )
56
- return vscode . window . withProgress ( {
57
- location : vscode . ProgressLocation . Notification ,
58
- title : "Run the worksheet" ,
59
- cancellable : true
60
- } , ( _ , token ) => {
61
- return this . client . sendRequest ( WorksheetRunRequest . type , asWorksheetRunParams ( this . document ) , token )
57
+ run ( ) : Promise < WorksheetRunResult > {
58
+ return new Promise ( ( resolve , reject ) => {
59
+ const textEdits = this . prepareRun ( )
60
+ const edit = new vscode . WorkspaceEdit ( )
61
+ edit . set ( this . document . uri , textEdits )
62
+ vscode . workspace . applyEdit ( edit ) . then ( editSucceeded => {
63
+ if ( editSucceeded ) {
64
+ return resolve ( vscode . window . withProgress ( {
65
+ location : vscode . ProgressLocation . Notification ,
66
+ title : "Run the worksheet" ,
67
+ cancellable : true
68
+ } , ( _ , token ) => this . client . sendRequest (
69
+ WorksheetRunRequest . type , asWorksheetRunParams ( this . document ) , token
70
+ ) ) )
71
+ } else
72
+ reject ( )
73
+ } )
62
74
} )
63
75
}
64
76
@@ -144,16 +156,16 @@ class Worksheet {
144
156
}
145
157
146
158
/**
147
- * Remove the repeated blank lines in the source.
159
+ * TextEdits to remove the repeated blank lines in the source.
148
160
*
149
161
* Running a worksheet can insert new lines in the worksheet so that the
150
162
* output of a line fits below the line. Before a run, we remove blank
151
163
* lines in the worksheet to keep its length under control.
152
164
*
153
165
* @param worksheet The worksheet where blank lines must be removed.
154
- * @return A `Thenable` removing the blank lines upon completion .
166
+ * @return An array of `TextEdit` that remove the blank lines.
155
167
*/
156
- private removeRedundantBlankLines ( ) {
168
+ private removeRedundantBlankLinesEdits ( ) : TextEdit [ ] {
157
169
158
170
const document = this . document
159
171
const lineCount = document . lineCount
@@ -189,13 +201,7 @@ class Worksheet {
189
201
addRange ( )
190
202
}
191
203
192
- return rangesToRemove . reverse ( ) . reduce ( ( chain : Thenable < boolean > , range ) => {
193
- return chain . then ( _ => {
194
- const edit = new vscode . WorkspaceEdit ( )
195
- edit . delete ( document . uri , range )
196
- return vscode . workspace . applyEdit ( edit )
197
- } )
198
- } , Promise . resolve ( true ) )
204
+ return rangesToRemove . reverse ( ) . map ( range => vscode . TextEdit . delete ( range ) )
199
205
}
200
206
201
207
private hasDecoration ( line : number ) : boolean {
@@ -214,15 +220,14 @@ export class WorksheetProvider implements Disposable {
214
220
vscode . workspace . onWillSaveTextDocument ( event => {
215
221
const worksheet = this . worksheetFor ( event . document )
216
222
if ( worksheet ) {
217
- // Block file saving until the worksheet is ready to be run.
218
- worksheet . prepareRun ( )
223
+ event . waitUntil ( Promise . resolve ( worksheet . prepareRun ( ) ) )
219
224
}
220
225
} ) ,
221
226
vscode . workspace . onDidSaveTextDocument ( document => {
222
227
const runWorksheetOnSave = vscode . workspace . getConfiguration ( "dotty" ) . get ( "runWorksheetOnSave" )
223
228
const worksheet = this . worksheetFor ( document )
224
229
if ( runWorksheetOnSave && worksheet ) {
225
- return worksheet . run ( )
230
+ worksheet . run ( )
226
231
}
227
232
} ) ,
228
233
vscode . workspace . onDidCloseTextDocument ( document => {
0 commit comments