Make Table Formulas a built-in feature
The formula engine, the rendered results, and the formula help now come from src/lib/formulas/. The editor and the renderer call this module directly. The tableFormulas contribution type is removed. - Add the tableFormulas general setting (default on) and a switch in Settings > General. When it is off, formulas show as source text. - Do not load an installed runestone.table-formulas package. Settings > Plug-ins marks it as included with RuneStone and tells the user that it can be removed. The user's files stay in place. - Translate the formula help and the error descriptions into French, Japanese, and Simplified Chinese. - Port the plugin unit tests. Test the setting and use a test-only contribution for the generic plugin lifecycle tests. - Smoke tests load only Terminal. The formula and lifecycle tests use the built-in feature and its setting. - Remove scripts/buildPlugins.mjs, the build:plugins script, and the plugin-packages requirement of the smoke runner. - Remove RuneStone_Table_Formulas from the menu audit scope. It had no menu surfaces. - Update the README, the user guides, and the developer references.
This commit is contained in:
@@ -0,0 +1,68 @@
|
||||
// Ported from the Table Formulas plugin (tests/formulas.test.ts). The engine is now part of the host.
|
||||
import assert from "node:assert/strict";
|
||||
import test from "node:test";
|
||||
import { cellAddress, evaluateTableFormulas } from "../../../src/lib/formulas/engine.ts";
|
||||
|
||||
const calculate = (formula: string) => evaluateTableFormulas([[formula]])[0][0]!.text;
|
||||
test("aggregates distinguish separate cells and ranges from an arithmetic argument", () => {
|
||||
const evaluate = (formula: string) => evaluateTableFormulas([["Name", "B", "C", "Result"], ["First", "9", "10", formula]])[1][3]!;
|
||||
for (const [name, separate, combined] of [["SUM", "19", "19"], ["AVERAGE", "9.5", "19"], ["MIN", "9", "19"], ["MAX", "10", "19"], ["COUNT", "2", "1"]]) {
|
||||
for (const separator of [",", ":"]) {
|
||||
const result = evaluate(`=${name}(B2${separator}C2)`);
|
||||
assert.equal(result.text, separate);
|
||||
}
|
||||
const result = evaluate(`=${name}(B2+C2)`);
|
||||
assert.equal(result.text, combined);
|
||||
}
|
||||
assert.equal(evaluate("=(B2+C2)/2").text, "9.5");
|
||||
assert.equal(evaluate("=ROUND(AVERAGE(B2,C2),0)").text, "10");
|
||||
assert.equal(evaluate("=ABS(B2-C2)").text, "1");
|
||||
assert.equal(evaluate("=AVERAGE((B2+C2))").text, "19");
|
||||
});
|
||||
|
||||
test("arithmetic, precedence, parentheses, percentages and rounding", () => {
|
||||
for (const [formula, result] of [
|
||||
["=SUM(2+3)", "5"], ["=2+3*4", "14"], ["=(2+3)*4", "20"],
|
||||
["=2^3^2", "512"], ["=-2^2", "-4"], ["=2^-2", "0.25"],
|
||||
["=200*10%", "20"], ["=0.1+0.2", "0.3"], ["=1e2 + .5", "100.5"],
|
||||
["=ROUND(-1.25,1)", "-1.3"], ["=ROUND(1.005,2)", "1.01"],
|
||||
["=ROUND(125,-1)", "130"], ["=ABS(-4)", "4"], ["=sum(1,2,3)", "6"],
|
||||
]) assert.equal(calculate(formula), result, formula);
|
||||
});
|
||||
|
||||
test("same-table references, ranges, chained dependencies, and empty/text cells", () => {
|
||||
const results = evaluateTableFormulas([
|
||||
["Name", "Appearance", "Cha", "Combined"],
|
||||
["First", "9", "10", "=AVERAGE(B2:C2)"],
|
||||
["Second", "7", "9", "=SUM(B3,C3)/2"],
|
||||
["Total", "", "=B4+1", "=SUM(D2:D3)"],
|
||||
["Count", "=COUNT(A1:C4)", "=MAX(B2:C3)-MIN(B2:C3)", "=ROUND(D4/3,2)"],
|
||||
["", "=SUM($B$2:$C$3)", "=AVERAGE(B1:B4)", "=SUM(C3:B2)"],
|
||||
]);
|
||||
assert.equal(results[1][3]?.text, "9.5");
|
||||
assert.equal(results[2][3]?.text, "8");
|
||||
assert.equal(results[3][3]?.text, "17.5");
|
||||
assert.equal(results[3][2]?.text, "1");
|
||||
assert.equal(results[4][1]?.text, "5");
|
||||
assert.equal(results[4][2]?.text, "3");
|
||||
assert.equal(results[4][3]?.text, "5.83");
|
||||
assert.equal(results[5][1]?.text, "35");
|
||||
assert.equal(results[5][2]?.text, "8");
|
||||
assert.equal(results[5][3]?.text, "35");
|
||||
assert.equal(results[0][0], null);
|
||||
assert.equal(cellAddress(1, 26), "AA2");
|
||||
assert.equal(evaluateTableFormulas([Array.from({ length: 27 }, (_, c) => c === 0 ? "=AA1" : "3")])[0][0]?.text, "3");
|
||||
});
|
||||
|
||||
test("errors are visible, propagated, bounded, and never executed", () => {
|
||||
for (const [formula, result] of [
|
||||
["=1/0", "#DIV/0!"], ["=AVERAGE()", "#DIV/0!"], ["=A2", "#REF!"],
|
||||
["=B1:B999999999", "#REF!"], ["=A0", "#REF!"], ["=1+", "#ERROR!"],
|
||||
["=1 2", "#ERROR!"], ["=ROUND(1,2,3)", "#ERROR!"], ["=BOGUS(1)", "#NAME?"],
|
||||
["=A1", "#CYCLE!"], ["=1e999", "#NUM!"], ["=9^9999", "#NUM!"],
|
||||
["=globalThis.alert(1)", "#ERROR!"], ["=" + "(".repeat(100) + "1" + ")".repeat(100), "#LIMIT!"],
|
||||
]) assert.equal(calculate(formula), result, formula);
|
||||
const cycle = evaluateTableFormulas([["=B1", "=A1", "=SUM(A1:B1)", "=2+3"]]);
|
||||
assert.deepEqual(cycle[0].map(cell => cell?.text), ["#CYCLE!", "#CYCLE!", "#CYCLE!", "5"]);
|
||||
assert.equal(evaluateTableFormulas([["text", "=A1+1"]])[0][1]?.text, "#VALUE!");
|
||||
});
|
||||
@@ -1,24 +1,14 @@
|
||||
import assert from "node:assert/strict";
|
||||
import test, { before, after } from "node:test";
|
||||
import { ExternalSession } from "../../../src/lib/plugins/externalSession.ts";
|
||||
import { existsSync } from "node:fs";
|
||||
// The Table Formulas package comes from a sibling repository. Run `pnpm build:plugins` to build it.
|
||||
const tableFormulasEntry = new URL("../../../plugin-packages/table-formulas/main.js", import.meta.url);
|
||||
const tableFormulas = existsSync(tableFormulasEntry) ? await import(tableFormulasEntry.href) : undefined;
|
||||
const activate = tableFormulas?.activate;
|
||||
const needsTableFormulas = { skip: tableFormulas ? false : "the Table Formulas package is not built; run pnpm build:plugins" };
|
||||
const session = new ExternalSession("runestone.table-formulas", {
|
||||
workspace: { list: async () => [], read: async () => "", write: async () => {} },
|
||||
settings: { read: async () => null, write: async () => {} },
|
||||
assets: { read: async () => new Uint8Array() },
|
||||
}, () => {}, () => {});
|
||||
before(() => activate && session.start({ activate }));
|
||||
after(() => activate && session.stop());
|
||||
|
||||
import test, { afterEach } from "node:test";
|
||||
import { get } from "svelte/store";
|
||||
import MarkdownIt from "markdown-it";
|
||||
import { activeTableFormulas, renderTableFormulas, setTableFormulasEnabled, tableFormulaHtml, tableFormulas, tableFormulasEnabled } from "../../../src/lib/formulas/index.ts";
|
||||
import { tableCellMarkdown } from "../../../src/lib/editor/tableCellMarkdown.ts";
|
||||
import { renderMarkdownHtml, renderMarkdownSections } from "../../../src/lib/workspace/noteHtml.ts";
|
||||
|
||||
test("Markdown rendering isolates tables and preserves literal, formatted, and non-table text", needsTableFormulas, () => {
|
||||
afterEach(() => setTableFormulasEnabled(true));
|
||||
|
||||
test("Markdown rendering isolates tables and preserves literal, formatted, and non-table text", () => {
|
||||
const source = "| X | Y | Result |\r\n| --- | --- | --- |\r\n| 9 | 10 | =AVERAGE(A2:B2) |\r\n\r\n| X | Y | Result |\r\n| --- | --- | --- |\r\n| 1 | 3 | =A2*B2 |\r\n";
|
||||
const html = renderMarkdownHtml(source, new Map());
|
||||
assert.match(html, /aria-label="=AVERAGE\(A2:B2\) → 9.5">9.5<\/span>/);
|
||||
@@ -34,9 +24,47 @@ test("Markdown rendering isolates tables and preserves literal, formatted, and n
|
||||
assert.match(hostile, /#ERROR!/);
|
||||
});
|
||||
|
||||
test("editing keeps formulas readable and code or explicit escapes literal", needsTableFormulas, () => {
|
||||
test("editing keeps formulas readable and code or explicit escapes literal", () => {
|
||||
const doc = (text: string, marks?: { type: string }[]) => ({ type: "doc", content: [{ type: "paragraph", content: [{ type: "text", text, marks }] }] });
|
||||
assert.equal(tableCellMarkdown(doc("=SUM($B$2:C2)*50%")), "=SUM($B$2:C2)*50%");
|
||||
assert.equal(tableCellMarkdown(doc("\\=1+2")), "\\=1+2");
|
||||
assert.equal(tableCellMarkdown(doc("=1+2", [{ type: "code" }])), "`=1+2`");
|
||||
});
|
||||
|
||||
test("the setting turns formulas off and back on without a change to the source", () => {
|
||||
const source = "| A | B |\n| --- | --- |\n| 2 | =A2*3 |\n";
|
||||
assert.equal(get(tableFormulasEnabled), true, "Formulas are on by default");
|
||||
assert.equal(activeTableFormulas(), tableFormulas);
|
||||
const enabled = renderMarkdownHtml(source, new Map());
|
||||
assert.match(enabled, />6<\/span>/);
|
||||
setTableFormulasEnabled(false);
|
||||
assert.equal(activeTableFormulas(), undefined);
|
||||
for (const html of [renderMarkdownHtml(source, new Map()), ...renderMarkdownSections(source, new Map()).map(section => section.html)]) {
|
||||
assert.doesNotMatch(html, /md-table-formula/);
|
||||
assert.match(html, /=A2\*3/);
|
||||
assert.match(html, /<table>/);
|
||||
}
|
||||
setTableFormulasEnabled(true);
|
||||
assert.equal(renderMarkdownHtml(source, new Map()), enabled);
|
||||
});
|
||||
|
||||
// Ported from the Table Formulas plugin (tests/render.test.ts).
|
||||
test("rendering separates tables and preserves literal expressions", () => {
|
||||
const markdown = new MarkdownIt();
|
||||
const source = "| A | B |\n| --- | --- |\n| 3 | =A2*2 |\n\n| A | B |\n| --- | --- |\n| 4 | =A2+1 |\n\n| Literal |\n| --- |\n| `=1+2` |\n| \\=1+2 |\n";
|
||||
const tokens = markdown.parse(source, {});
|
||||
renderTableFormulas(tokens);
|
||||
const html = markdown.renderer.render(tokens, markdown.options, {});
|
||||
assert.match(html, /aria-label="=A2\*2 → 6">6<\/span>/);
|
||||
assert.match(html, /aria-label="=A2\+1 → 5">5<\/span>/);
|
||||
assert.match(html, /<code>=1\+2<\/code>/);
|
||||
assert.equal((html.match(/class="md-table-formula"/g) || []).length, 2);
|
||||
});
|
||||
|
||||
test("formula labels escape markup and report errors", () => {
|
||||
const html = tableFormulaHtml({ formula: '=SUM("<img>")', text: "#ERROR!", error: "#ERROR!" });
|
||||
assert.doesNotMatch(html, /<img>/);
|
||||
assert.match(html, /"<img>"/);
|
||||
assert.match(html, /md-table-formula is-error/);
|
||||
assert.match(html, /Invalid formula syntax or function arguments/);
|
||||
});
|
||||
|
||||
Reference in New Issue
Block a user