@@ -18,6 +18,9 @@ class Worksheet {
18
18
/** The number of blank lines that have been inserted to fit the output so far. */
19
19
insertedLines : number = 0
20
20
21
+ /** The lines that contain decorations */
22
+ decoratedLines : Set < number > = new Set < number > ( )
23
+
21
24
/** The minimum margin to add so that the decoration is shown after all text. */
22
25
margin : number = 0
23
26
@@ -28,6 +31,7 @@ class Worksheet {
28
31
reset ( ) {
29
32
this . decorationTypes . forEach ( decoration => decoration . dispose ( ) )
30
33
this . insertedLines = 0
34
+ this . decoratedLines . clear ( )
31
35
this . margin = longestLine ( this . document ) + 5
32
36
this . finished = false
33
37
}
@@ -160,7 +164,7 @@ export function prepareWorksheet(event: vscode.TextDocumentWillSaveEvent) {
160
164
}
161
165
162
166
function _prepareWorksheet ( worksheet : Worksheet ) {
163
- return removeRedundantBlankLines ( worksheet . document ) . then ( _ => worksheet . reset ( ) )
167
+ return removeRedundantBlankLines ( worksheet ) . then ( _ => worksheet . reset ( ) )
164
168
}
165
169
166
170
/**
@@ -228,14 +232,18 @@ function longestLine(document: vscode.TextDocument) {
228
232
*
229
233
* Evaluating a worksheet can insert new lines in the worksheet so that the
230
234
* 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.
233
236
*
234
- * @param document The document where blank lines must be removed.
237
+ * @param worksheet The worksheet where blank lines must be removed.
235
238
* @return A `Thenable` removing the blank lines upon completion.
236
239
*/
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
+ }
238
245
246
+ const document = worksheet . document
239
247
const lineCount = document . lineCount
240
248
let rangesToRemove : vscode . Range [ ] = [ ]
241
249
let rangeStart = 0
@@ -245,14 +253,13 @@ function removeRedundantBlankLines(document: vscode.TextDocument) {
245
253
function addRange ( ) {
246
254
inRange = false
247
255
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 ) )
250
257
}
251
258
return
252
259
}
253
260
254
261
for ( let i = 0 ; i < lineCount ; ++ i ) {
255
- const isEmpty = document . lineAt ( i ) . isEmptyOrWhitespace
262
+ const isEmpty = document . lineAt ( i ) . isEmptyOrWhitespace && hasDecoration ( i )
256
263
if ( inRange ) {
257
264
if ( isEmpty ) rangeEnd += 1
258
265
else addRange ( )
@@ -318,6 +325,7 @@ function worksheetDisplayResult(lineNumber: number, message: string, worksheet:
318
325
const decorationMargin = margin - editor . document . lineAt ( actualLine ) . text . length
319
326
const decorationType = worksheetCreateDecoration ( decorationMargin , line )
320
327
worksheet . decorationTypes . push ( decorationType )
328
+ worksheet . decoratedLines . add ( actualLine )
321
329
322
330
const decoration = { range : new vscode . Range ( decorationPosition , decorationPosition ) , hoverMessage : line }
323
331
editor . setDecorations ( decorationType , [ decoration ] )
0 commit comments