Skip to content

Commit

Permalink
Fix multiple formula lines being applied from the bottom up
Browse files Browse the repository at this point in the history
They are now applied from the top down.

Fixes #8
  • Loading branch information
tgrosinger committed Nov 26, 2021
1 parent 94e408c commit 654fe2f
Show file tree
Hide file tree
Showing 2 changed files with 68 additions and 1 deletion.
2 changes: 1 addition & 1 deletion src/calc/calc.ts
Original file line number Diff line number Diff line change
Expand Up @@ -170,7 +170,7 @@ export const parseAndApply = (
// If there is no error,
return formulas.andThen((innerFormulas: Formula[]) =>
// for each formula
innerFormulas.reduce<Result<Table, Error>>(
innerFormulas.reduceRight<Result<Table, Error>>(
(prevValue, formula) =>
// If the previous formula didn't give an error
prevValue.andThen(
Expand Down
67 changes: 67 additions & 0 deletions test/calc.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1957,5 +1957,72 @@ describe('Formulas', () => {
]);
}
});

it('should apply multiple formula lines sequentially', () => {
{
const textEditor = new TextEditor([
'foo',
'| A | B |',
'| --- | --- |',
'| 1 | 2 |',
'| 3 | 4 |',
'| 5 | 6 |',
'| | |',
'<!-- TBLFM: @>$>=@3 -->',
'<!-- TBLFM: @>$1=@4 -->',
]);
textEditor.setCursorPosition(new Point(1, 0));
const tableEditor = new TableEditor(textEditor);
const err = tableEditor.evaluateFormulas(defaultOptions);
const pos = textEditor.getCursorPosition();
expect(err).to.be.undefined;
expect(pos.row).to.equal(1);
expect(pos.column).to.equal(0);
expect(textEditor.getSelectionRange()).to.be.undefined;
expect(textEditor.getLines()).to.deep.equal([
'foo',
'| A | B |',
'| --- | --- |',
'| 1 | 2 |',
'| 3 | 4 |',
'| 5 | 6 |',
'| 5 | 4 |',
'<!-- TBLFM: @>$>=@3 -->',
'<!-- TBLFM: @>$1=@4 -->',
]);
}
{
const textEditor = new TextEditor([
'foo',
'| A | B |',
'| --- | --- |',
'| 1 | 2 |',
'| 3 | 4 |',
'| 5 | 6 |',
'| | |',
'<!-- TBLFM: @>$>=@3 -->',
'<!-- TBLFM: @>$1=(@>$2+3) -->',
]);
textEditor.setCursorPosition(new Point(1, 0));
const tableEditor = new TableEditor(textEditor);
const err = tableEditor.evaluateFormulas(defaultOptions);
const pos = textEditor.getCursorPosition();
expect(err).to.be.undefined;
expect(pos.row).to.equal(1);
expect(pos.column).to.equal(0);
expect(textEditor.getSelectionRange()).to.be.undefined;
expect(textEditor.getLines()).to.deep.equal([
'foo',
'| A | B |',
'| --- | --- |',
'| 1 | 2 |',
'| 3 | 4 |',
'| 5 | 6 |',
'| 7 | 4 |',
'<!-- TBLFM: @>$>=@3 -->',
'<!-- TBLFM: @>$1=(@>$2+3) -->',
]);
}
});
});
});

0 comments on commit 654fe2f

Please sign in to comment.