Skip to content

Commit e294ee9

Browse files
committed
[IMP] composer: prettify v3
max length before split ~ 60 chars Tofixup ? task: 4735172
1 parent 3c0fb50 commit e294ee9

File tree

4 files changed

+47
-34
lines changed

4 files changed

+47
-34
lines changed

src/components/composer/composer/prettifier_content.ts

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -15,9 +15,9 @@ import {
1515
// allowing dynamic selection of the most suitable layout for the given constraints.
1616

1717
type PrettifyPossibility = string | ChooseBetween | Join | Nest | insertLine;
18-
type ChooseBetween = { type: "chooseBetween"; p1: PrettifyPossibility; p2: PrettifyPossibility };
18+
type ChooseBetween = { type: "chooseBetween"; pp1: PrettifyPossibility; pp2: PrettifyPossibility };
1919
type Join = { type: "join"; rules: PrettifyPossibility[] };
20-
type Nest = { type: "nest"; indentLvl: number; p: PrettifyPossibility };
20+
type Nest = { type: "nest"; indentLvl: number; pp: PrettifyPossibility };
2121
type insertLine = { type: "insertLine" };
2222

2323
/** Useful for indicating where to insert a new line. Placed in a group, it will be used if there is insufficient space*/
@@ -27,8 +27,8 @@ function line(): insertLine {
2727

2828
/** Useful for indicating where to insert a new line with a specific indentation level.
2929
* Should be placed before a line. Placed in a group, it will be used if there is insufficient space*/
30-
function nest(indentLvl: number, p: PrettifyPossibility): Nest {
31-
return { type: "nest", indentLvl, p };
30+
function nest(indentLvl: number, pp: PrettifyPossibility): Nest {
31+
return { type: "nest", indentLvl, pp: pp };
3232
}
3333

3434
/** Useful for join few rules into a single rule.
@@ -45,8 +45,8 @@ function group(pp: PrettifyPossibility): ChooseBetween {
4545
}
4646

4747
/** Used exclusively for `group`, indicating that we can choose between two rules depending on the available space.*/
48-
function chooseBetween(p1: PrettifyPossibility, p2: PrettifyPossibility): ChooseBetween {
49-
return { type: "chooseBetween", p1, p2 };
48+
function chooseBetween(pp1: PrettifyPossibility, pp2: PrettifyPossibility): ChooseBetween {
49+
return { type: "chooseBetween", pp1: pp1, pp2: pp2 };
5050
}
5151

5252
/** Recursive function used exclusively for `group`, to indicate how to flatten the rules.*/
@@ -56,7 +56,7 @@ function flatten(pp: PrettifyPossibility): PrettifyPossibility {
5656
}
5757
if (pp.type === "chooseBetween") {
5858
// normally should be "chooseBetween(flatten(x.a), flatten(x.b))" but compute time is too high
59-
return flatten(pp.p1);
59+
return flatten(pp.pp1);
6060
}
6161
if (pp.type === "join") {
6262
return join(pp.rules.map(flatten));
@@ -65,7 +65,7 @@ function flatten(pp: PrettifyPossibility): PrettifyPossibility {
6565
return {
6666
type: "nest",
6767
indentLvl: pp.indentLvl,
68-
p: flatten(pp.p),
68+
pp: flatten(pp.pp),
6969
};
7070
}
7171
if (pp.type === "insertLine") {
@@ -165,7 +165,7 @@ function _best(width: number, currentIndentLvl: number, head: RestToFitNode | nu
165165
return _best(width, currentIndentLvl, newHead);
166166
}
167167
if (pp.type === "nest") {
168-
return _best(width, currentIndentLvl, { indentLvl: indentLvl + pp.indentLvl, pp: pp.p, next });
168+
return _best(width, currentIndentLvl, { indentLvl: indentLvl + pp.indentLvl, pp: pp.pp, next });
169169
}
170170
if (pp.type === "insertLine") {
171171
return {
@@ -175,13 +175,13 @@ function _best(width: number, currentIndentLvl: number, head: RestToFitNode | nu
175175
};
176176
}
177177
if (pp.type === "chooseBetween") {
178-
const head1 = { indentLvl, pp: pp.p1, next };
178+
const head1 = { indentLvl, pp: pp.pp1, next };
179179
const subRuleA = _best(width, currentIndentLvl, head1);
180180
if (fits(width - currentIndentLvl, subRuleA)) {
181181
return subRuleA;
182182
}
183183

184-
const head2 = { indentLvl, pp: pp.p2, next };
184+
const head2 = { indentLvl, pp: pp.pp2, next };
185185
return _best(width, currentIndentLvl, head2);
186186
}
187187
return null;
@@ -207,7 +207,7 @@ function fits(width: number, x: SubRule): boolean {
207207
// ---------------------------------------
208208

209209
export function prettify(ast: AST) {
210-
return "=" + print(astToPp(ast), 39); // 39 but 40 with the `= ` at the beginning
210+
return "=" + print(astToPp(ast), 59); // 59 but 60 with the `=` at the beginning
211211
}
212212

213213
/** transform an AST composed of sub-ASTs into a PrettifyPossibility composed of sub-PrettifyPossibility.*/

tests/composer/composer_component.test.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -919,7 +919,7 @@ describe("composer", () => {
919919
});
920920

921921
test("Can select a right-to-left range that spans multiple span elements", async () => {
922-
setCellContent(model, "A1", "= A1 + SUM( A2 )");
922+
setCellContent(model, "A1", "=A1+SUM(A2)");
923923
await nextTick();
924924
await simulateClick("div.o-composer");
925925
composerEl = fixture.querySelector<HTMLElement>("div.o-composer")!;

tests/composer/composer_store.test.ts

Lines changed: 32 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -1232,31 +1232,36 @@ describe("edition", () => {
12321232
});
12331233

12341234
test("Split the content in multiple lines when it is too long", () => {
1235-
setCellContent(model, "A1", "=SUM(11111111,22222222,33333333)"); // 41 characters
1235+
setCellContent(model, "A1", "=SUM(11111111,22222222,33333333,44444444,55555555)"); // 41 characters
12361236
composerStore.startEdition();
1237-
expect(composerStore.currentContent).toBe("=SUM(11111111, 22222222, 33333333)");
1237+
expect(composerStore.currentContent).toBe(
1238+
"=SUM(11111111, 22222222, 33333333, 44444444, 55555555)"
1239+
);
12381240

1239-
setCellContent(model, "A1", "=SUM(11111111,22222222,33333333,44444444)"); // 41 characters
1241+
setCellContent(model, "A1", "=SUM(11111111,22222222,33333333,44444444,55555555,66666666)"); // 41 characters
12401242
composerStore.startEdition();
12411243
expect(composerStore.currentContent).toBe(
12421244
// prettier-ignore
12431245
"=SUM(\n" +
12441246
"\t11111111, \n" +
12451247
"\t22222222, \n" +
12461248
"\t33333333, \n" +
1247-
"\t44444444\n" +
1249+
"\t44444444, \n" +
1250+
"\t55555555, \n" +
1251+
"\t66666666\n" +
12481252
")"
12491253
);
12501254
});
12511255

12521256
test("nested functions are properly indented", () => {
1253-
setCellContent(model, "A1", "=SUM(AVERAGE(1,2,3,4), MAX(5,6,7,8))");
1257+
setCellContent(model, "A1", "=SUM(AVERAGE(1,2,3,4), MAX(5,6,7,8), MIN(10,11,12,13))");
12541258
composerStore.startEdition();
12551259
expect(composerStore.currentContent).toBe(
12561260
// prettier-ignore
12571261
"=SUM(\n" +
12581262
"\tAVERAGE(1, 2, 3, 4), \n" +
1259-
"\tMAX(5, 6, 7, 8)\n" +
1263+
"\tMAX(5, 6, 7, 8), \n" +
1264+
"\tMIN(10, 11, 12, 13)\n" +
12601265
")"
12611266
);
12621267
});
@@ -1265,18 +1270,20 @@ describe("edition", () => {
12651270
setCellContent(
12661271
model,
12671272
"A1",
1268-
"=SUM(AVERAGE(COUNT(4,5,6,7),COUNT(10,11,12,13)), MAX(COUNT(4,5,6,7),COUNT(10,11,12,13)))"
1273+
"=SUM(AVERAGE(COUNT(4,5,6,7),COUNT(10,11,12,13),COUNT(14,15,16,17)), MAX(COUNT(4,5,6,7),COUNT(10,11,12,13),COUNT(14,15,16,17)))"
12691274
);
12701275
composerStore.startEdition();
12711276
expect(composerStore.currentContent).toBe(
12721277
"=SUM(\n" +
12731278
"\tAVERAGE(\n" +
12741279
"\t\tCOUNT(4, 5, 6, 7), \n" +
1275-
"\t\tCOUNT(10, 11, 12, 13)\n" +
1280+
"\t\tCOUNT(10, 11, 12, 13), \n" +
1281+
"\t\tCOUNT(14, 15, 16, 17)\n" +
12761282
"\t), \n" +
12771283
"\tMAX(\n" +
12781284
"\t\tCOUNT(4, 5, 6, 7), \n" +
1279-
"\t\tCOUNT(10, 11, 12, 13)\n" +
1285+
"\t\tCOUNT(10, 11, 12, 13), \n" +
1286+
"\t\tCOUNT(14, 15, 16, 17)\n" +
12801287
"\t)\n" +
12811288
")"
12821289
);
@@ -1286,15 +1293,15 @@ describe("edition", () => {
12861293
setCellContent(
12871294
model,
12881295
"A1",
1289-
"=SUM(1111 + 2222 + 3333 + 4444 + 5555 + 6666 + 7777 + 8888 + 9999)"
1296+
"=SUM(1111 + 2222 + 3333 + 4444 + 5555 + 6666 + 7777 + 8888 + 9999 + 11111 + 22222 + 33333 + 44444)"
12901297
);
12911298
composerStore.startEdition();
12921299
expect(composerStore.currentContent).toBe(
12931300
//prettier-ignore
12941301
"=SUM(\n" +
1295-
"\t1111+2222+3333+4444+5555+6666+7777+\n" +
1296-
"\t\t8888+\n" +
1297-
"\t\t9999\n" +
1302+
"\t1111+2222+3333+4444+5555+6666+7777+8888+9999+11111+22222+\n" +
1303+
"\t\t33333+\n" +
1304+
"\t\t44444\n" +
12981305
")"
12991306
);
13001307
});
@@ -1303,29 +1310,33 @@ describe("edition", () => {
13031310
setCellContent(
13041311
model,
13051312
"A1",
1306-
"=SUM(1111 + 2222 + 3333 + 4444 + 5555 + 6666 + 7777 + 8888 * 9999 - 10000 + 20000 / 30000 )"
1313+
"=SUM(1111 + 2222 + 3333 + 4444 + 5555 + 6666 + 7777 + 8888 + 9999 + 11111 + 22222 + 33333 * 44444 - 55555 + 66666 / 77777 )"
13071314
);
13081315
composerStore.startEdition();
13091316
expect(composerStore.currentContent).toBe(
13101317
"=SUM(\n" +
1311-
"\t1111+2222+3333+4444+5555+6666+7777+\n" +
1312-
"\t\t8888*9999-\n" +
1313-
"\t\t10000+\n" +
1314-
"\t\t20000/30000\n" +
1318+
"\t1111+2222+3333+4444+5555+6666+7777+8888+9999+11111+22222+\n" +
1319+
"\t\t33333*44444-\n" +
1320+
"\t\t55555+\n" +
1321+
"\t\t66666/77777\n" +
13151322
")"
13161323
);
13171324
});
13181325

13191326
test("long functions with nested parenthesis for mathematical operation are properly indented with sub-lvls", () => {
1320-
setCellContent(model, "A1", "=1*(2-2-2-2-2-2-2-(3+3+3+3+3+3+3+3+3/(4+5+6+7+5+6+7+8+9)))");
1327+
setCellContent(
1328+
model,
1329+
"A1",
1330+
"=1*(2-2-2-2-2-2-2-(3+3+3+3+3+3+3+3+3-(4+4+4+4+4+4+4+4+4/(4+5+6+7+5+6+7+8+9))))"
1331+
);
13211332
composerStore.startEdition();
13221333
expect(composerStore.currentContent).toBe(
13231334
"=1*\n" +
13241335
"\t(\n" +
13251336
"\t\t2-2-2-2-2-2-2-\n" +
13261337
"\t\t\t(\n" +
1327-
"\t\t\t\t3+3+3+3+3+3+3+3+\n" +
1328-
"\t\t\t\t\t3/(4+5+6+7+5+6+7+8+9)\n" +
1338+
"\t\t\t\t3+3+3+3+3+3+3+3+3-\n" +
1339+
"\t\t\t\t\t(4+4+4+4+4+4+4+4+4/(4+5+6+7+5+6+7+8+9))\n" +
13291340
"\t\t\t)\n" +
13301341
"\t)"
13311342
);

tests/evaluation/evaluation.test.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -352,6 +352,7 @@ describe("evaluateCells", () => {
352352
setCellContent(model, "A5", "= - 1 + - 2 * - 3");
353353
setCellContent(model, "A6", "=1 & 8 + 2");
354354
setCellContent(model, "A7", "=1 & 10 - 2");
355+
setCellContent(model, "A8", "=2^100%");
355356

356357
expect(getEvaluatedCell(model, "A1").value).toBe(7);
357358
expect(getEvaluatedCell(model, "A2").value).toBe(4);
@@ -360,6 +361,7 @@ describe("evaluateCells", () => {
360361
expect(getEvaluatedCell(model, "A5").value).toBe(5);
361362
expect(getEvaluatedCell(model, "A6").value).toBe("110");
362363
expect(getEvaluatedCell(model, "A7").value).toBe("18");
364+
expect(getEvaluatedCell(model, "A8").value).toBe(2);
363365
});
364366

365367
test("& operator", () => {

0 commit comments

Comments
 (0)