From f1e12dfa9b050ef35f9aa9317adbce92531b0365 Mon Sep 17 00:00:00 2001 From: Kibana Machine <42973632+kibanamachine@users.noreply.github.com> Date: Thu, 19 Dec 2024 23:09:22 +1100 Subject: [PATCH] [8.x] [ES|QL] Make sure that the commands are not overwritten (#204711) (#204904) # Backport This will backport the following commits from `main` to `8.x`: - [[ES|QL] Make sure that the commands are not overwritten (#204711)](https://github.com/elastic/kibana/pull/204711) ### Questions ? Please refer to the [Backport tool documentation](https://github.com/sqren/backport) Co-authored-by: Stratoula Kalafateli --- .../src/autocomplete/autocomplete.ts | 32 ++++++++++++------- .../src/autocomplete/commands/drop/index.ts | 16 ++++++---- .../src/autocomplete/commands/keep/index.ts | 16 ++++++---- .../src/autocomplete/commands/sort/index.ts | 14 +++++--- 4 files changed, 49 insertions(+), 29 deletions(-) diff --git a/src/platform/packages/shared/kbn-esql-validation-autocomplete/src/autocomplete/autocomplete.ts b/src/platform/packages/shared/kbn-esql-validation-autocomplete/src/autocomplete/autocomplete.ts index 2a37155358e85..e0b5d8c3ffcee 100644 --- a/src/platform/packages/shared/kbn-esql-validation-autocomplete/src/autocomplete/autocomplete.ts +++ b/src/platform/packages/shared/kbn-esql-validation-autocomplete/src/autocomplete/autocomplete.ts @@ -612,22 +612,30 @@ async function getExpressionSuggestionsByType( (fragment) => Boolean(getColumnByName(fragment, references)), (_fragment: string, rangeToReplace?: { start: number; end: number }) => { // COMMAND fie - return fieldSuggestions.map((suggestion) => ({ - ...suggestion, - text: suggestion.text + (['grok', 'dissect'].includes(command.name) ? ' ' : ''), - command: TRIGGER_SUGGESTION_COMMAND, - rangeToReplace, - })); + return fieldSuggestions.map((suggestion) => { + // if there is already a command, we don't want to override it + if (suggestion.command) return suggestion; + return { + ...suggestion, + text: suggestion.text + (['grok', 'dissect'].includes(command.name) ? ' ' : ''), + command: TRIGGER_SUGGESTION_COMMAND, + rangeToReplace, + }; + }); }, (fragment: string, rangeToReplace: { start: number; end: number }) => { // COMMAND field if (['grok', 'dissect'].includes(command.name)) { - return fieldSuggestions.map((suggestion) => ({ - ...suggestion, - text: suggestion.text + ' ', - command: TRIGGER_SUGGESTION_COMMAND, - rangeToReplace, - })); + return fieldSuggestions.map((suggestion) => { + // if there is already a command, we don't want to override it + if (suggestion.command) return suggestion; + return { + ...suggestion, + text: suggestion.text + ' ', + command: TRIGGER_SUGGESTION_COMMAND, + rangeToReplace, + }; + }); } const finalSuggestions = [{ ...pipeCompleteItem, text: ' | ' }]; diff --git a/src/platform/packages/shared/kbn-esql-validation-autocomplete/src/autocomplete/commands/drop/index.ts b/src/platform/packages/shared/kbn-esql-validation-autocomplete/src/autocomplete/commands/drop/index.ts index 43eb272ba203a..dee04f5f8eba6 100644 --- a/src/platform/packages/shared/kbn-esql-validation-autocomplete/src/autocomplete/commands/drop/index.ts +++ b/src/platform/packages/shared/kbn-esql-validation-autocomplete/src/autocomplete/commands/drop/index.ts @@ -41,12 +41,16 @@ export async function suggest( (fragment) => columnExists(fragment), (_fragment: string, rangeToReplace?: { start: number; end: number }) => { // KEEP fie - return fieldSuggestions.map((suggestion) => ({ - ...suggestion, - text: suggestion.text, - command: TRIGGER_SUGGESTION_COMMAND, - rangeToReplace, - })); + return fieldSuggestions.map((suggestion) => { + // if there is already a command, we don't want to override it + if (suggestion.command) return suggestion; + return { + ...suggestion, + text: suggestion.text, + command: TRIGGER_SUGGESTION_COMMAND, + rangeToReplace, + }; + }); }, (fragment: string, rangeToReplace: { start: number; end: number }) => { // KEEP field diff --git a/src/platform/packages/shared/kbn-esql-validation-autocomplete/src/autocomplete/commands/keep/index.ts b/src/platform/packages/shared/kbn-esql-validation-autocomplete/src/autocomplete/commands/keep/index.ts index 85d5c716b20a6..f17f9cf7445ca 100644 --- a/src/platform/packages/shared/kbn-esql-validation-autocomplete/src/autocomplete/commands/keep/index.ts +++ b/src/platform/packages/shared/kbn-esql-validation-autocomplete/src/autocomplete/commands/keep/index.ts @@ -41,12 +41,16 @@ export async function suggest( (fragment) => columnExists(fragment), (_fragment: string, rangeToReplace?: { start: number; end: number }) => { // KEEP fie - return fieldSuggestions.map((suggestion) => ({ - ...suggestion, - text: suggestion.text, - command: TRIGGER_SUGGESTION_COMMAND, - rangeToReplace, - })); + return fieldSuggestions.map((suggestion) => { + // if there is already a command, we don't want to override it + if (suggestion.command) return suggestion; + return { + ...suggestion, + text: suggestion.text, + command: TRIGGER_SUGGESTION_COMMAND, + rangeToReplace, + }; + }); }, (fragment: string, rangeToReplace: { start: number; end: number }) => { // KEEP field diff --git a/src/platform/packages/shared/kbn-esql-validation-autocomplete/src/autocomplete/commands/sort/index.ts b/src/platform/packages/shared/kbn-esql-validation-autocomplete/src/autocomplete/commands/sort/index.ts index 61561dea96b72..8012abc5be6d2 100644 --- a/src/platform/packages/shared/kbn-esql-validation-autocomplete/src/autocomplete/commands/sort/index.ts +++ b/src/platform/packages/shared/kbn-esql-validation-autocomplete/src/autocomplete/commands/sort/index.ts @@ -128,11 +128,15 @@ export async function suggest( // SORT fie return [ ...pushItUpInTheList( - fieldSuggestions.map((suggestion) => ({ - ...suggestion, - command: TRIGGER_SUGGESTION_COMMAND, - rangeToReplace, - })), + fieldSuggestions.map((suggestion) => { + // if there is already a command, we don't want to override it + if (suggestion.command) return suggestion; + return { + ...suggestion, + command: TRIGGER_SUGGESTION_COMMAND, + rangeToReplace, + }; + }), true ), ...functionSuggestions,