From 4ee373401870dbd857fc87a0ab379a9f8214a78a Mon Sep 17 00:00:00 2001 From: Kibana Machine <42973632+kibanamachine@users.noreply.github.com> Date: Wed, 23 Oct 2024 22:25:16 +1100 Subject: [PATCH] [8.16] [ES|QL] Fix incorrect suggestions after a field is accepted in EVAL (#197139) (#197392) # Backport This will backport the following commits from `main` to `8.16`: - [[ES|QL] Fix incorrect suggestions after a field is accepted in EVAL (#197139)](https://github.com/elastic/kibana/pull/197139) ### Questions ? Please refer to the [Backport tool documentation](https://github.com/sqren/backport) Co-authored-by: Quynh Nguyen (Quinn) <43350163+qn895@users.noreply.github.com> --- .../__tests__/autocomplete.suggest.eval.test.ts | 7 +++++++ .../src/autocomplete/autocomplete.ts | 13 ++++++++++++- 2 files changed, 19 insertions(+), 1 deletion(-) diff --git a/packages/kbn-esql-validation-autocomplete/src/autocomplete/__tests__/autocomplete.suggest.eval.test.ts b/packages/kbn-esql-validation-autocomplete/src/autocomplete/__tests__/autocomplete.suggest.eval.test.ts index dcb6fe76f184..81fd8f7f4390 100644 --- a/packages/kbn-esql-validation-autocomplete/src/autocomplete/__tests__/autocomplete.suggest.eval.test.ts +++ b/packages/kbn-esql-validation-autocomplete/src/autocomplete/__tests__/autocomplete.suggest.eval.test.ts @@ -51,6 +51,13 @@ describe('autocomplete.suggest', () => { ...getFieldNamesByType('any'), ...getFunctionSignaturesByReturnType('eval', 'any', { scalar: true }), ]); + + await assertSuggestions('from a | eval doubleField/', [ + 'doubleField, ', + 'doubleField | ', + 'var0 = ', + ]); + await assertSuggestions('from a | eval doubleField /', [ ...getFunctionSignaturesByReturnType('eval', 'any', { builtin: true, skipAssign: true }, [ 'double', diff --git a/packages/kbn-esql-validation-autocomplete/src/autocomplete/autocomplete.ts b/packages/kbn-esql-validation-autocomplete/src/autocomplete/autocomplete.ts index cbc84232b8eb..98a26b0c8dd4 100644 --- a/packages/kbn-esql-validation-autocomplete/src/autocomplete/autocomplete.ts +++ b/packages/kbn-esql-validation-autocomplete/src/autocomplete/autocomplete.ts @@ -596,6 +596,17 @@ async function getExpressionSuggestionsByType( const suggestions: SuggestionRawDefinition[] = []; + // When user types and accepts autocomplete suggestion, and cursor is placed at the end of a valid field + // we should not show irrelevant functions that might have words matching + const columnWithActiveCursor = commands.find( + (c) => + c.name === command.name && + command.name === 'eval' && + c.args.some((arg) => isColumnItem(arg) && arg.name.includes(EDITOR_MARKER)) + ); + + const shouldShowFunctions = !columnWithActiveCursor; + // in this flow there's a clear plan here from argument definitions so try to follow it if (argDef) { if (argDef.type === 'column' || argDef.type === 'any' || argDef.type === 'function') { @@ -722,7 +733,7 @@ async function getExpressionSuggestionsByType( option?.name, getFieldsByType, { - functions: true, + functions: shouldShowFunctions, fields: false, variables: nodeArg ? undefined : anyVariables, literals: argDef.constantOnly,