@@ -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
162166function _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 ( )
@@ -317,6 +324,7 @@ function worksheetDisplayResult(lineNumber: number, evalResult: string, workshee
317324 const decorationMargin = margin - editor . document . lineAt ( actualLine ) . text . length
318325 const decorationType = worksheetCreateDecoration ( decorationMargin , line )
319326 worksheet . decorationTypes . push ( decorationType )
327+ worksheet . decoratedLines . add ( actualLine )
320328
321329 const decoration = { range : new vscode . Range ( decorationPosition , decorationPosition ) , hoverMessage : line }
322330 editor . setDecorations ( decorationType , [ decoration ] )
0 commit comments