From c681890b2c2b78923a5a7cbf11ec613be4b377ed Mon Sep 17 00:00:00 2001 From: Stratoula Kalafateli Date: Wed, 18 Dec 2024 11:37:22 +0100 Subject: [PATCH] [ES|QL] Make sure that the commands are not overwritten --- .../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,