Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[FIX] composer: prevent autocomplete on unknown characters in composer #5986

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 6 additions & 3 deletions src/components/composer/composer/abstract_composer_store.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import { isSheetAutocomplete } from "../../../formulas";
import { composerTokenize, EnrichedToken } from "../../../formulas/composer_tokenizer";
import { POSTFIX_UNARY_OPERATORS } from "../../../formulas/tokenizer";
import { functionRegistry } from "../../../functions";
Expand Down Expand Up @@ -713,9 +714,11 @@ export abstract class AbstractComposerStore extends SpreadsheetStore {

get autocompleteProvider(): AutoCompleteProvider | undefined {
const content = this.currentContent;
const tokenAtCursor = isFormula(content)
? this.tokenAtCursor
: { type: "STRING", value: content };
const tokenAtCursor =
(isFormula(content) && !this.currentTokens.some((token) => token.type === "UNKNOWN")) ||
isSheetAutocomplete(this.tokenAtCursor)
? this.tokenAtCursor
: { type: "STRING", value: content };
if (
this.editionMode === "inactive" ||
!tokenAtCursor ||
Expand Down
11 changes: 9 additions & 2 deletions src/components/composer/composer/composer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import { NEWLINE, PRIMARY_BUTTON_BG, SCROLLBAR_WIDTH } from "../../../constants"
import { functionRegistry } from "../../../functions/index";
import { clip, isFormula, setColorAlpha } from "../../../helpers/index";

import { isSheetAutocomplete } from "../../../formulas";
import { EnrichedToken } from "../../../formulas/composer_tokenizer";
import { argTargeting } from "../../../functions/arguments";
import { Store, useLocalStore, useStore } from "../../../store_engine";
Expand Down Expand Up @@ -743,8 +744,14 @@ export class Composer extends Component<CellComposerProps, SpreadsheetChildEnv>
}
const token = this.props.composerStore.tokenAtCursor;

if (isFormula(composerStore.currentContent) && token && token.type !== "SYMBOL") {
const tokenContext = token.functionContext;
if (
(isFormula(composerStore.currentContent) &&
token &&
token.type !== "SYMBOL" &&
!this.props.composerStore.currentTokens.some((t) => t.type === "UNKNOWN")) ||
isSheetAutocomplete(this.props.composerStore.tokenAtCursor)
) {
const tokenContext = token?.functionContext;
const parentFunction = tokenContext?.parent.toUpperCase();
if (
tokenContext &&
Expand Down
8 changes: 8 additions & 0 deletions src/formulas/helpers.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { functionRegistry } from "../functions";
import { EnrichedToken } from "./composer_tokenizer";
import { AST, ASTFuncall, iterateAstNodes, parseTokens } from "./parser";
import { Token } from "./tokenizer";

Expand Down Expand Up @@ -38,3 +39,10 @@ function getFunctionsFromAST(ast: AST, functionNames: string[]) {
args: node.args,
}));
}

export function isSheetAutocomplete(tokenAtCursor: EnrichedToken | undefined): boolean {
return (
(tokenAtCursor?.type === "SYMBOL" || tokenAtCursor?.type === "UNKNOWN") &&
tokenAtCursor?.value.startsWith("'")
);
}
18 changes: 18 additions & 0 deletions tests/composer/auto_complete/function_auto_complete_store.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,4 +15,22 @@ describe("Function auto complete", () => {
autoComplete?.selectProposal(proposals![0].text);
expect(composer.currentContent).toEqual("=SUM(");
});

test("starting with unknown character should stop autocomplete even when adding a valid character", () => {
const { store: composer, model } = makeStore(CellComposerStore);
setCellContent(model, "A1", "=éSUM");
composer.startEdition();
const autoComplete = composer.autocompleteProvider;
const proposals = autoComplete?.proposals;
expect(proposals).toBeUndefined();
});

test("adding an unknown character in the middle of a function should stop autocomplete", () => {
const { store: composer, model } = makeStore(CellComposerStore);
setCellContent(model, "A1", "=SéSUM");
composer.startEdition();
const autoComplete = composer.autocompleteProvider;
const proposals = autoComplete?.proposals;
expect(proposals).toBeUndefined();
});
});