Skip to content

Commit 5b467f5

Browse files
committed
Remove only lines added to fit output
1 parent 4ace38d commit 5b467f5

File tree

1 file changed

+16
-8
lines changed

1 file changed

+16
-8
lines changed

vscode-dotty/src/worksheet.ts

Lines changed: 16 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,9 @@ class Worksheet {
1818
/** The number of blank lines that have been inserted to fit the output so far. */
1919
insertedLines: number = 0
2020

21+
/** The lines that contain decorations */
22+
decoratedLines: Set<number> = new Set<number>()
23+
2124
/** The minimum margin to add so that the decoration is shown after all text. */
2225
margin: number = 0
2326

@@ -28,6 +31,7 @@ class Worksheet {
2831
reset() {
2932
this.decorationTypes.forEach(decoration => decoration.dispose())
3033
this.insertedLines = 0
34+
this.decoratedLines.clear()
3135
this.margin = longestLine(this.document) + 5
3236
this.finished = false
3337
}
@@ -160,7 +164,7 @@ export function prepareWorksheet(event: vscode.TextDocumentWillSaveEvent) {
160164
}
161165

162166
function _prepareWorksheet(worksheet: Worksheet) {
163-
return removeRedundantBlankLines(worksheet.document).then(_ => worksheet.reset())
167+
return removeRedundantBlankLines(worksheet).then(_ => worksheet.reset())
164168
}
165169

166170
/**
@@ -228,14 +232,18 @@ function longestLine(document: vscode.TextDocument) {
228232
*
229233
* Evaluating a worksheet can insert new lines in the worksheet so that the
230234
* output of a line fits below the line. Before evaluation, we remove blank
231-
* lines in the worksheet to keep its length under control. This could potentially
232-
* remove manually added blank lines.
235+
* lines in the worksheet to keep its length under control.
233236
*
234-
* @param document The document where blank lines must be removed.
237+
* @param worksheet The worksheet where blank lines must be removed.
235238
* @return A `Thenable` removing the blank lines upon completion.
236239
*/
237-
function removeRedundantBlankLines(document: vscode.TextDocument) {
240+
function removeRedundantBlankLines(worksheet: Worksheet) {
241+
242+
function hasDecoration(line: number): boolean {
243+
return worksheet.decoratedLines.has(line)
244+
}
238245

246+
const document = worksheet.document
239247
const lineCount = document.lineCount
240248
let rangesToRemove: vscode.Range[] = []
241249
let rangeStart = 0
@@ -245,14 +253,13 @@ function removeRedundantBlankLines(document: vscode.TextDocument) {
245253
function addRange() {
246254
inRange = false
247255
if (rangeStart < rangeEnd) {
248-
// Keep one line between separate chunks of code
249-
rangesToRemove.push(new vscode.Range(rangeStart, 0, rangeEnd - 1, 0))
256+
rangesToRemove.push(new vscode.Range(rangeStart, 0, rangeEnd, 0))
250257
}
251258
return
252259
}
253260

254261
for (let i = 0; i < lineCount; ++i) {
255-
const isEmpty = document.lineAt(i).isEmptyOrWhitespace
262+
const isEmpty = document.lineAt(i).isEmptyOrWhitespace && hasDecoration(i)
256263
if (inRange) {
257264
if (isEmpty) rangeEnd += 1
258265
else addRange()
@@ -318,6 +325,7 @@ function worksheetDisplayResult(lineNumber: number, message: string, worksheet:
318325
const decorationMargin = margin - editor.document.lineAt(actualLine).text.length
319326
const decorationType = worksheetCreateDecoration(decorationMargin, line)
320327
worksheet.decorationTypes.push(decorationType)
328+
worksheet.decoratedLines.add(actualLine)
321329

322330
const decoration = { range: new vscode.Range(decorationPosition, decorationPosition), hoverMessage: line }
323331
editor.setDecorations(decorationType, [decoration])

0 commit comments

Comments
 (0)