Skip to content

Commit 37cf67c

Browse files
committed
[WIP]
1 parent 3cd5eee commit 37cf67c

File tree

1 file changed

+36
-5
lines changed

1 file changed

+36
-5
lines changed

src/components/composer/composer/abstract_composer_store.ts

Lines changed: 36 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -323,6 +323,11 @@ export abstract class AbstractComposerStore extends SpreadsheetStore {
323323
tokens.push(...Array(missingParenthesis).fill({ value: ")", type: "RIGHT_PAREN" }));
324324
}
325325

326+
const missingBrace = this.getNumberOfMissingBraces(tokens);
327+
if (missingBrace > 0) {
328+
tokens.push(...Array(missingBrace).fill({ value: "}", type: "RIGHT_BRACE" }));
329+
}
330+
326331
let hoveredContextTokens = tokens;
327332
if (tokenIndex !== 0) {
328333
const relatedTokens = this.getRelatedTokens(tokens, tokenIndex);
@@ -470,13 +475,11 @@ export abstract class AbstractComposerStore extends SpreadsheetStore {
470475
}
471476
if (content) {
472477
if (isFormula(content)) {
473-
const missing = this.getNumberOfMissingParenthesis(this.currentTokens);
474-
if (missing > 0) {
475-
content += concat(new Array(missing).fill(")"));
476-
}
478+
const missingSymbols = this.getMissingSymbols(this.currentTokens);
479+
content += concat(missingSymbols);
477480
}
481+
this.confirmEdition(content);
478482
}
479-
this.confirmEdition(content);
480483
}
481484
}
482485

@@ -1016,4 +1019,32 @@ export abstract class AbstractComposerStore extends SpreadsheetStore {
10161019
const right = tokens.filter((t) => t.type === "RIGHT_PAREN").length;
10171020
return left - right;
10181021
}
1022+
1023+
private getNumberOfMissingBraces(tokens: EnrichedToken[]): number {
1024+
const left = tokens.filter((t) => t.type === "LEFT_BRACE").length;
1025+
const right = tokens.filter((t) => t.type === "RIGHT_BRACE").length;
1026+
return left - right;
1027+
}
1028+
1029+
private getMissingSymbols(tokens: EnrichedToken[]): string[] {
1030+
const stack: string[] = [];
1031+
for (const symb of tokens) {
1032+
switch (symb.value) {
1033+
case "(":
1034+
stack.push(")");
1035+
break;
1036+
case "{":
1037+
stack.push("}");
1038+
break;
1039+
case ")":
1040+
case "}":
1041+
if (stack.length > 0 && stack[stack.length - 1] === symb.value) {
1042+
stack.pop();
1043+
}
1044+
break;
1045+
}
1046+
}
1047+
stack.reverse();
1048+
return stack;
1049+
}
10191050
}

0 commit comments

Comments
 (0)