Skip to content

Commit

Permalink
Fix parsing of multiple formulas in a single line
Browse files Browse the repository at this point in the history
Fixes #12
  • Loading branch information
tgrosinger committed Nov 26, 2021
1 parent 6e90441 commit 94e408c
Show file tree
Hide file tree
Showing 2 changed files with 76 additions and 2 deletions.
15 changes: 13 additions & 2 deletions src/calc/calc.ts
Original file line number Diff line number Diff line change
Expand Up @@ -214,9 +214,20 @@ export const parseFormula = (
return err(lengthError);
}

const formulas = ast.children[0].children;
let unparsedFormulas = ast.children[0].children;
const formulas: Formula[] = [];

try {
return ok(formulas.map((formula) => new Formula(formula, table)));
do {
formulas.push(new Formula(unparsedFormulas[0], table));

if (unparsedFormulas.length > 1 && unparsedFormulas[1].type === 'formula_list') {
unparsedFormulas = unparsedFormulas[1].children;
} else {
unparsedFormulas = [];
}
} while (unparsedFormulas.length > 0);
return ok(formulas);
} catch (error) {
return err(error);
}
Expand Down
63 changes: 63 additions & 0 deletions test/calc.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1894,5 +1894,68 @@ describe('Formulas', () => {
]);
}
});

it('should parse multiple formulas on the same line', () => {
{
const textEditor = new TextEditor([
'foo',
'| A | B |',
'| --- | --- |',
'| 1 | 2 |',
'| 3 | 4 |',
'| 5 | 6 |',
'| | |',
'<!-- TBLFM: @>$>=@3::@>$1=@4::@2$1=5 -->',
]);
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 |',
'| --- | --- |',
'| 5 | 2 |',
'| 3 | 4 |',
'| 5 | 6 |',
'| 5 | 4 |',
'<!-- TBLFM: @>$>=@3::@>$1=@4::@2$1=5 -->',
]);
}
{
const textEditor = new TextEditor([
'foo',
'| A | B |',
'| --- | --- |',
'| 1 | 2 |',
'| 3 | 4 |',
'| 5 | 6 |',
'| | |',
'<!-- TBLFM: @>$>=@3::@>$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::@>$1=@4 -->',
]);
}
});
});
});

0 comments on commit 94e408c

Please sign in to comment.