Skip to content

Commit 6640161

Browse files
committed
Async-related improvements
- Replaced removeRedundantBlankLines by removeRedundantBlankLinesEdit which doesn't have side-effects but returns an array of TextEdit - Fixed onWillSaveTextDocument to call event.waitUntil which is the only way to actually apply changes synchronously. - Removed the return in onDidSaveTextDocument, the result of the lambda for an event isn't actually used for anything - Changed Worksheet#run() to have a more precise result type and to only run something if prepareRun() succeeded.
1 parent 4524016 commit 6640161

File tree

1 file changed

+31
-26
lines changed

1 file changed

+31
-26
lines changed

vscode-dotty/src/worksheet.ts

Lines changed: 31 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
11
import * as vscode from 'vscode'
2+
import { TextEdit } from 'vscode'
23

34
import {
4-
asWorksheetRunParams, WorksheetRunRequest, WorksheetRunParams,
5+
asWorksheetRunParams, WorksheetRunRequest, WorksheetRunParams, WorksheetRunResult,
56
WorksheetPublishOutputParams, WorksheetPublishOutputNotification
67
} from './protocol'
78
import { BaseLanguageClient, DocumentSelector } from 'vscode-languageclient'
@@ -41,24 +42,35 @@ class Worksheet {
4142

4243
/**
4344
* 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.
4647
*/
47-
prepareRun(): void {
48-
this.removeRedundantBlankLines().then(_ => this.reset())
48+
prepareRun(): TextEdit[] {
49+
const edits = this.removeRedundantBlankLinesEdits()
50+
this.reset()
51+
return edits
4952
}
5053

5154
/**
5255
* Run the worksheet in `document`, display a progress bar during the run.
5356
*/
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+
})
6274
})
6375
}
6476

@@ -144,16 +156,16 @@ class Worksheet {
144156
}
145157

146158
/**
147-
* Remove the repeated blank lines in the source.
159+
* TextEdits to remove the repeated blank lines in the source.
148160
*
149161
* Running a worksheet can insert new lines in the worksheet so that the
150162
* output of a line fits below the line. Before a run, we remove blank
151163
* lines in the worksheet to keep its length under control.
152164
*
153165
* @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.
155167
*/
156-
private removeRedundantBlankLines() {
168+
private removeRedundantBlankLinesEdits(): TextEdit[] {
157169

158170
const document = this.document
159171
const lineCount = document.lineCount
@@ -189,13 +201,7 @@ class Worksheet {
189201
addRange()
190202
}
191203

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))
199205
}
200206

201207
private hasDecoration(line: number): boolean {
@@ -214,15 +220,14 @@ export class WorksheetProvider implements Disposable {
214220
vscode.workspace.onWillSaveTextDocument(event => {
215221
const worksheet = this.worksheetFor(event.document)
216222
if (worksheet) {
217-
// Block file saving until the worksheet is ready to be run.
218-
worksheet.prepareRun()
223+
event.waitUntil(Promise.resolve(worksheet.prepareRun()))
219224
}
220225
}),
221226
vscode.workspace.onDidSaveTextDocument(document => {
222227
const runWorksheetOnSave = vscode.workspace.getConfiguration("dotty").get("runWorksheetOnSave")
223228
const worksheet = this.worksheetFor(document)
224229
if (runWorksheetOnSave && worksheet) {
225-
return worksheet.run()
230+
worksheet.run()
226231
}
227232
}),
228233
vscode.workspace.onDidCloseTextDocument(document => {

0 commit comments

Comments
 (0)