Skip to content

Commit 654fe2f

Browse files
committed
Fix multiple formula lines being applied from the bottom up
They are now applied from the top down. Fixes tgrosinger#8
1 parent 94e408c commit 654fe2f

File tree

2 files changed

+68
-1
lines changed

2 files changed

+68
-1
lines changed

src/calc/calc.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -170,7 +170,7 @@ export const parseAndApply = (
170170
// If there is no error,
171171
return formulas.andThen((innerFormulas: Formula[]) =>
172172
// for each formula
173-
innerFormulas.reduce<Result<Table, Error>>(
173+
innerFormulas.reduceRight<Result<Table, Error>>(
174174
(prevValue, formula) =>
175175
// If the previous formula didn't give an error
176176
prevValue.andThen(

test/calc.ts

+67
Original file line numberDiff line numberDiff line change
@@ -1957,5 +1957,72 @@ describe('Formulas', () => {
19571957
]);
19581958
}
19591959
});
1960+
1961+
it('should apply multiple formula lines sequentially', () => {
1962+
{
1963+
const textEditor = new TextEditor([
1964+
'foo',
1965+
'| A | B |',
1966+
'| --- | --- |',
1967+
'| 1 | 2 |',
1968+
'| 3 | 4 |',
1969+
'| 5 | 6 |',
1970+
'| | |',
1971+
'<!-- TBLFM: @>$>=@3 -->',
1972+
'<!-- TBLFM: @>$1=@4 -->',
1973+
]);
1974+
textEditor.setCursorPosition(new Point(1, 0));
1975+
const tableEditor = new TableEditor(textEditor);
1976+
const err = tableEditor.evaluateFormulas(defaultOptions);
1977+
const pos = textEditor.getCursorPosition();
1978+
expect(err).to.be.undefined;
1979+
expect(pos.row).to.equal(1);
1980+
expect(pos.column).to.equal(0);
1981+
expect(textEditor.getSelectionRange()).to.be.undefined;
1982+
expect(textEditor.getLines()).to.deep.equal([
1983+
'foo',
1984+
'| A | B |',
1985+
'| --- | --- |',
1986+
'| 1 | 2 |',
1987+
'| 3 | 4 |',
1988+
'| 5 | 6 |',
1989+
'| 5 | 4 |',
1990+
'<!-- TBLFM: @>$>=@3 -->',
1991+
'<!-- TBLFM: @>$1=@4 -->',
1992+
]);
1993+
}
1994+
{
1995+
const textEditor = new TextEditor([
1996+
'foo',
1997+
'| A | B |',
1998+
'| --- | --- |',
1999+
'| 1 | 2 |',
2000+
'| 3 | 4 |',
2001+
'| 5 | 6 |',
2002+
'| | |',
2003+
'<!-- TBLFM: @>$>=@3 -->',
2004+
'<!-- TBLFM: @>$1=(@>$2+3) -->',
2005+
]);
2006+
textEditor.setCursorPosition(new Point(1, 0));
2007+
const tableEditor = new TableEditor(textEditor);
2008+
const err = tableEditor.evaluateFormulas(defaultOptions);
2009+
const pos = textEditor.getCursorPosition();
2010+
expect(err).to.be.undefined;
2011+
expect(pos.row).to.equal(1);
2012+
expect(pos.column).to.equal(0);
2013+
expect(textEditor.getSelectionRange()).to.be.undefined;
2014+
expect(textEditor.getLines()).to.deep.equal([
2015+
'foo',
2016+
'| A | B |',
2017+
'| --- | --- |',
2018+
'| 1 | 2 |',
2019+
'| 3 | 4 |',
2020+
'| 5 | 6 |',
2021+
'| 7 | 4 |',
2022+
'<!-- TBLFM: @>$>=@3 -->',
2023+
'<!-- TBLFM: @>$1=(@>$2+3) -->',
2024+
]);
2025+
}
2026+
});
19602027
});
19612028
});

0 commit comments

Comments
 (0)