Skip to content

Commit ec0adc8

Browse files
authored
Consistent EOL for new notebook cells (#290)
1 parent 3d08d4a commit ec0adc8

File tree

2 files changed

+19
-5
lines changed

2 files changed

+19
-5
lines changed

src/platform/notebook/common/alternativeContentEditGenerator.ts

Lines changed: 18 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,17 @@ export class AlternativeNotebookContentEditGenerator implements IAlternativeNote
6060
return format;
6161
}
6262

63+
/**
64+
* Given a NotebookDocument or Uri, and a cell kind, return the EOL for the new cell.
65+
* If the notebook is empty, then return the default EOL.
66+
* Else default to the EOL of the first cell of the given kind.
67+
* This way we have a consistent EOL for new cells (matching existing cells).
68+
*/
69+
private getEOLForNewCell(notebookOrUri: NotebookDocument | Uri, cellKind: NotebookCellKind): string | undefined {
70+
const eolInExistingCodeCell = isUri(notebookOrUri) ? undefined : (notebookOrUri.getCells().find(c => c.kind === cellKind)?.document.eol ?? undefined);
71+
return eolInExistingCodeCell ? eolInExistingCodeCell === EndOfLine.LF ? '\n' : '\r\n' : EOL;
72+
}
73+
6374
/**
6475
* Given a stream of lines for the alternative content, generate the corresponding edits to apply to the notebook document.
6576
* We accept a NotebookDocument or a Uri.
@@ -94,7 +105,8 @@ export class AlternativeNotebookContentEditGenerator implements IAlternativeNote
94105
if (!notebookEditEmitted && format === 'text' && linesCollected.length && !lineMightHaveCellMarker(firstNonEmptyLine)) {
95106
const uri = isUri(notebookOrUri) ? notebookOrUri : notebookOrUri.uri;
96107
if (isJupyterNotebookUri(uri)) {
97-
const cellData = new NotebookCellData(NotebookCellKind.Code, linesCollected.join(EOL), 'python');
108+
const eolForNewCell = this.getEOLForNewCell(notebookOrUri, NotebookCellKind.Code);
109+
const cellData = new NotebookCellData(NotebookCellKind.Code, linesCollected.join(eolForNewCell), 'python');
98110
yield NotebookEdit.insertCells(0, [cellData]);
99111
this.logger.logger.info(`No new cells were emitted for ${uri.toString()}. Emitting a new cell with the contents of the code.`);
100112
} else {
@@ -247,7 +259,8 @@ export class AlternativeNotebookContentEditGenerator implements IAlternativeNote
247259
}
248260
editsEmitted = true;
249261
} else if (cellInfo.insertEdit) {
250-
cellInfo.insertEdit.newCells[0].value = cellInfo.lines.join(EOL);
262+
const eolForNewCell = this.getEOLForNewCell(notebookOrUri, cellInfo.insertEdit.newCells[0].kind);
263+
cellInfo.insertEdit.newCells[0].value = cellInfo.lines.join(eolForNewCell);
251264
} else {
252265
// Insert the new cell.
253266
const cellData = new NotebookCellData(cellInfo.language === 'markdown' ? NotebookCellKind.Markup : NotebookCellKind.Code, line.line, cellInfo.language);
@@ -266,7 +279,7 @@ export class AlternativeNotebookContentEditGenerator implements IAlternativeNote
266279
// If the format is correct, then we should have emitted some edits.
267280
// If we don't exit here we end up deleting all the cells in the notebook.
268281
if (!editsEmitted && allLines.length) {
269-
this.logger.logger.warn(`No edits generated for notebook ${notebookOrUri.uri.toString()}. This is likely due to an invalid format. Expected format: ${format}. Provided content as follows:\n\n${allLines.join(EOL)}`);
282+
this.logger.logger.warn(`No edits generated for notebook ${notebookOrUri.uri.toString()}. This is likely due to an invalid format. Expected format: ${format}. Provided content as follows:\n\n${allLines.join('\n')}`);
270283
return;
271284
}
272285

@@ -297,8 +310,9 @@ export class AlternativeNotebookContentEditGenerator implements IAlternativeNote
297310
// insert items
298311
for (const change of diffResult.filter(d => d.type === 'insert')) {
299312
const expectedCell = expectedCells[change.modifiedCellIndex];
300-
const source = expectedCell.lines.join(EOL);
301313
const kind = expectedCell.language === 'markdown' ? NotebookCellKind.Markup : NotebookCellKind.Code;
314+
const eolForNewCell = this.getEOLForNewCell(notebookOrUri, kind);
315+
const source = expectedCell.lines.join(eolForNewCell);
302316
const cellData = new NotebookCellData(kind, source, expectedCell.language);
303317
yield NotebookEdit.insertCells(expectedCell.index, [cellData]);
304318
}

src/platform/notebook/common/helpers.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ export class LineOfText {
2222
}
2323
}
2424

25-
// TODO @rebornix for notebook alternative content we use \n always as the line ending for now.
25+
/** End of Line for alternative Notebook contnt is always \n */
2626
export const EOL = '\n';
2727
export type LineOfCellText = {
2828
type: 'start';

0 commit comments

Comments
 (0)