Skip to content

Commit

Permalink
Remove initial empty cell from codeManager.getCells() (#1505)
Browse files Browse the repository at this point in the history
* Remove initial empty cell from getCells()

* Include any initial whitespace on line with first text

* Fix tests

* Add tests for code-manager.getCells

* Fix tests; append cells to an empty notebook
  • Loading branch information
kylebarron committed Jan 8, 2019
1 parent 2814771 commit a9d8c15
Show file tree
Hide file tree
Showing 3 changed files with 56 additions and 14 deletions.
18 changes: 13 additions & 5 deletions lib/code-manager.js
Expand Up @@ -282,18 +282,26 @@ export function getCells(
} else {
breakpoints = getBreakpoints(editor);
}
return getCellsForBreakPoints(breakpoints);
return getCellsForBreakPoints(editor, breakpoints);
}

export function getCellsForBreakPoints(
editor: atom$TextEditor,
breakpoints: Array<atom$Point>
): Array<atom$Range> {
let start = new Point(0, 0);
return _.map(breakpoints, end => {
const cell = new Range(start, end);
start = new Point(end.row + 1, 0);
return cell;
// Let start be earliest row with text
editor.scan(/\S/, match => {
start = new Point(match.range.start.row, 0);
match.stop();
});
return _.compact(
_.map(breakpoints, end => {
const cell = end.isEqual(start) ? null : new Range(start, end);
start = new Point(end.row + 1, 0);
return cell;
})
);
}

export function moveDown(editor: atom$TextEditor, row: number) {
Expand Down
40 changes: 39 additions & 1 deletion spec/code-manager-spec.js
Expand Up @@ -69,7 +69,9 @@ describe("CodeManager", () => {
[[6, 0], [10, 5]]
].map(toRange); // zero-to-bp1 // nextRow of bp1 to bp2 // nextRow of bp2 to bp3 // nextRow of bp3 to bp4

expect(CM.getCellsForBreakPoints(points)).toEqual(cellsExpected);
expect(CM.getCellsForBreakPoints(editor, points)).toEqual(
cellsExpected
);
});
});
describe("getCells", () => {
Expand Down Expand Up @@ -101,6 +103,42 @@ describe("CodeManager", () => {
].map(toRange); // zero-to-row0:bp // nextRow of row0:bp to row2:bp // nextRow of row2:bp to EOF(= implicit bp)
expect(CM.getCells(editor)).toEqual(cellsExpected);
});
it("doesn't create cell from initial empty whitespace", () => {
const code = [
"",
"",
"print('hello world')",
"# %%",
"print('foo bar')"
];
editor.setText(code.join("\n") + "\n");
const cellsExpected = [[[2, 0], [3, 0]], [[4, 0], [5, 0]]].map(
toRange
);
expect(CM.getCells(editor)).toEqual(cellsExpected);
});
it("doesn't create cell from initial empty whitespace with cell marker", () => {
const code = [
"",
"# %%",
"print('hello world')",
"# %%",
"print('foo bar')"
];
editor.setText(code.join("\n") + "\n");
const cellsExpected = [[[2, 0], [3, 0]], [[4, 0], [5, 0]]].map(
toRange
);
expect(CM.getCells(editor)).toEqual(cellsExpected);
});
it("doesn't create initial empty cell with no whitespace", () => {
const code = ["print('hello world')", "# %%", "print('foo bar')"];
editor.setText(code.join("\n") + "\n");
const cellsExpected = [[[0, 0], [1, 0]], [[2, 0], [3, 0]]].map(
toRange
);
expect(CM.getCells(editor)).toEqual(cellsExpected);
});
});
describe("with arg(= breakpoints)", () => {
it("return cells(range) from passed breakpoints(with auto-sort-by-position)", () => {
Expand Down
12 changes: 4 additions & 8 deletions spec/store/index-spec.js
Expand Up @@ -315,14 +315,10 @@ describe("Store", () => {
expect(store.notebook).toBeNull();
});

it("should return a single cell notebook for empty file", () => {
it("should return an empty notebook for empty file", () => {
store.updateEditor(editor);
// Build a notebook with one code cell.
let codeCell = commutable.emptyCodeCell.set("source", "");
const nb = commutable.appendCellToNotebook(
commutable.emptyNotebook,
codeCell
);
const nb = commutable.emptyNotebook;
expect(store.notebook).toEqual(commutable.toJS(nb));
});

Expand All @@ -336,7 +332,7 @@ describe("Store", () => {
// The outputted notebook will have three cells because currently a cell
// is always created before the first `# %%`
let nb = commutable.appendCellToNotebook(
commutable.monocellNotebook,
commutable.emptyNotebook,
codeCell1
);
nb = commutable.appendCellToNotebook(nb, codeCell2);
Expand All @@ -353,7 +349,7 @@ describe("Store", () => {
// The outputted notebook will have three cells because currently a cell
// is always created before the first `# %%`
let nb = commutable.appendCellToNotebook(
commutable.monocellNotebook,
commutable.emptyNotebook,
codeCell
);
nb = commutable.appendCellToNotebook(nb, markdownCell);
Expand Down

0 comments on commit a9d8c15

Please sign in to comment.